|
| 1 | +--- |
| 2 | +title: Transformers |
| 3 | +subtitle: 'Transform data to expected state' |
| 4 | +position: 3 |
| 5 | +--- |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- All transformers can be combined. |
| 10 | +- All `get` methods has `transformers` argument |
| 11 | +- You are free to add more transformers to this package. |
| 12 | + |
| 13 | +## Strategy |
| 14 | + |
| 15 | +I've chosen most used combination while working with external services and created `DefaultTransformerStrategy`. |
| 16 | + |
| 17 | +You can disable or [change](#customization) this strategy while constructing `GetValue` instance. |
| 18 | + |
| 19 | +```php |
| 20 | +$data = new GetValue(data: $array, transformerStrategy: new NoTransformerStrategy()); |
| 21 | +``` |
| 22 | + |
| 23 | +## Transformers |
| 24 | + |
| 25 | +> Transformers parameter in get methods overrides transformers from strategy. |
| 26 | +
|
| 27 | +To disable default transformers set `transformers` argument to empty array. |
| 28 | + |
| 29 | +```php |
| 30 | +$transformer = new ClosureTransformer(function (mixed $value, string $key): ?string { |
| 31 | + if ($value === null) { |
| 32 | + return null; |
| 33 | + } |
| 34 | + |
| 35 | + return md5($value); |
| 36 | + }); |
| 37 | +$md5 = $getValue->getString('key', [$transformer]); |
| 38 | +``` |
| 39 | + |
| 40 | +### ClosureTransformer |
| 41 | + |
| 42 | +Transforms the value using closure after validation has been done (can be changed with `$beforeValidation` argument). |
| 43 | + |
| 44 | + |
| 45 | +```php |
| 46 | +$getValue->getBool('key', [new \Wrkflow\GetValue\Transformers\TransformToBool()]); |
| 47 | +``` |
| 48 | + |
| 49 | +### TransformToBool |
| 50 | + |
| 51 | +Transforms most used representations of boolean in string or number ('yes','no',1,0,'1','0','true','false') and converts |
| 52 | +it to bool **before** validation starts. |
| 53 | + |
| 54 | +```php |
| 55 | +$getValue->getBool('key', [new \Wrkflow\GetValue\Transformers\TransformToBool()]); |
| 56 | +``` |
| 57 | + |
| 58 | +### TrimAndEmptyStringToNull |
| 59 | + |
| 60 | +> Used in DefaultTransformerStrategy |
| 61 | +
|
| 62 | +Ensures that string is trimmed and transformed to null (if empty string is provided) **before** validation starts. |
| 63 | + |
| 64 | +```php |
| 65 | +$getValue->getString('key', [new \Wrkflow\GetValue\Transformers\TrimAndEmptyStringToNull()]); |
| 66 | +``` |
| 67 | + |
| 68 | +### TrimString |
| 69 | + |
| 70 | +Ensures that string is trimmed **before** validation starts. |
| 71 | + |
| 72 | +```php |
| 73 | +// Get trimmed string (no '' to null transformation) |
| 74 | +$getValue->getString('key', [new \Wrkflow\GetValue\Transformers\TrimString()]); |
| 75 | +``` |
| 76 | + |
| 77 | +## Customization |
| 78 | + |
| 79 | +You can create your own transformer by extending: |
| 80 | + |
| 81 | +- For array `Wrkflow\GetValue\Contracts\TransformerArrayContract` |
| 82 | +- Reset of values `Wrkflow\GetValue\Contracts\TransformerContract` |
| 83 | + |
| 84 | +Then implement `public function transform(mixed $value, string $key): mixed;`. Expect invalid value and make do not |
| 85 | +transform the value if it is invalid. Just return it. |
| 86 | + |
| 87 | +Then implement `public function beforeValidation(mixed $value, string $key): bool;` which ensures that transformation |
| 88 | +is not done before validation. |
| 89 | + |
| 90 | +Then change the strategy: |
| 91 | + |
| 92 | +```php |
| 93 | +$data = new GetValue(data: $array, transformerStrategy: new MyTransformerStrategy()); |
| 94 | +``` |
0 commit comments