Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What About Data transformers? #36

Closed
Insolita opened this issue Nov 3, 2019 · 2 comments
Closed

What About Data transformers? #36

Insolita opened this issue Nov 3, 2019 · 2 comments

Comments

@Insolita
Copy link

Insolita commented Nov 3, 2019

In yii2 we have 'trim' and 'filter' validators... that really was not validators, but useful.

What about ability to add Transformers. Not sure - should it be in this repo, or as separated...

$transforms = new Transforms([
    'a'=>['trim', new TitleCaseTransformer()],
    'b'=>[function($v){ return rtrim($v, '/').'/';}, 'strtolower']
]);
$transforms->apply($dataSet);

or

$validator = new Validator([    
    'amount' => [
       (new Number())->integer()->max(100),
       (new Transform())->apply([function($value, DataSetInterface $data){
           return $data->getValue('mode') === 'X' ? $value*100: $value*$data->getValue('mul');
       }]),
        function ($value): Result {
            $result = new Result();
            if ($value === 13) {
                $result->addError('Value should not be 13!');
            }
            return $result;
        }
    ],
]);
@samdark
Copy link
Member

samdark commented Nov 3, 2019

@samdark
Copy link
Member

samdark commented Nov 3, 2019

Validators are for validation. Transforming and normalizing should be done elsewhere and I think it really depends on the class that is being transformed. It could be implemented directly in constructor/setters or via special mechanism. Or even outside of the class directly where the class is being filled with data.

@samdark samdark closed this as completed Nov 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants