@@ -138,6 +138,30 @@ $transformer = new ArrayTransformer(function (array $value, string $key): array
138138});
139139
140140$values = $data->getArray('key', transformers: [$transformer]);
141+ // Result: ['2231d0878e1f14976c498ad49de37ef6', 'edc23e3209134c89922592669e09cb65']
142+ ```
143+
144+ If you want to use this transformer with getString and other non-array method then you need to run the
145+ transformer before validation by setting ` beforeValidation: true ` .
146+
147+
148+ ``` php
149+ use Wrkflow\GetValue\GetValue;
150+ use Wrkflow\GetValue\DataHolders\ArrayData;
151+ use Wrkflow\GetValue\Transformers\ArrayTransformer;
152+
153+ $data = new GetValue(new ArrayData([
154+ 'key' => ['Marco', 'Polo']
155+ ]));
156+
157+ $transformer = new ArrayTransformer(
158+ closure: fn (array $value, string $key): string => implode(' ', $value),
159+ beforeValidation: true
160+ );
161+
162+ $name = $data->getString('key', transformers: [$transformer]);
163+ $this->assertEquals('Marco Polo', $name);
164+ // Result: Marco Polo
141165```
142166
143167### ArrayItemTransformer
@@ -153,80 +177,88 @@ use Wrkflow\GetValue\Transformers\ArrayItemTransformer;
153177use Wrkflow\GetValue\Exceptions\ValidationFailedException;
154178
155179$data = new GetValue(new ArrayData([
156- 'key ' => ['Marco', 'Polo']
180+ 'names ' => [[ 'Marco', 'Polo'], ['Way', 'Point'], [] ]
157181]));
158182$transformer = new ArrayItemTransformer( function (mixed $value, string $key): string {
159- if (is_string($value) !== null) {
160- throw new ValidationFailedException($key, 'array value not a string');
161- }
162-
163- return md5($value);
183+ if (is_array($value) !== true) {
184+ throw new ValidationFailedException($key, 'expecting an array');
185+ }
186+
187+ if ($value === []) {
188+ return null;
189+ }
190+
191+ return implode(' ', $value);
164192});
165193
166- $values = $data->getArray('key', transformers: [$transformer]);
194+ $values = $data->getArray('names', transformers: [$transformer]);
195+ // Result: ['Marco Polo', 'Way Point']
167196```
168197
169- ### ArrayItemGetterTransformer
170-
171- > Can be used only with get\* Array\* methods. Throws NotAnArrayException if array value is not an array.
172-
173- Transforms an ** array that contains array values** in a closure that receives wrapped array in GetValue.
198+ If you return ` null ` in your closure then value is not added to result array. Use ` ignoreNullResult: false ` in constructor.
174199
175200``` php
176201use Wrkflow\GetValue\GetValue;
177202use Wrkflow\GetValue\DataHolders\ArrayData;
178- use Wrkflow\GetValue\Transformers\ArrayItemGetterTransformer;
203+ use Wrkflow\GetValue\Transformers\ArrayItemTransformer;
204+ use Wrkflow\GetValue\Exceptions\ValidationFailedException;
179205
180206$data = new GetValue(new ArrayData([
181- 'key ' => [['test' => 'Marco'], ['test' => 'Polo']]
207+ 'names ' => ['Marco Polo', 'Way Point', ''],
182208]));
183- $transformer = new ArrayItemGetterTransformer( function (GetValue $value, string $key): string {
184- return [
185- 'test' => $value->getRequiredString('test'),
186- ];
187- });
209+ $transformer = new ArrayItemTransformer(function (mixed $value, string $key): ?array {
210+ if (is_string($value) === false) {
211+ throw new ValidationFailedException($key, 'expecting string');
212+ }
188213
189- $values = $data->getArray('key', transformers: [$transformer]);
214+ if ($value === '') {
215+ return null;
216+ }
217+
218+ return explode(' ', $value);
219+ }, ignoreNullResult: false);
220+
221+ $values = $data->getArray('names', transformers: [$transformer]);
222+ // Result: [['Marco', 'Polo'], ['Way', 'Point'], null]
190223```
191224
192225### ArrayItemGetterTransformer
193226
194- > Can be used only with get\* Array\* methods.
227+ > Can be used only with get\* Array\* methods. Throws NotAnArrayException if array value is not an array.
195228
196- Transforms an ** array** in a closure that receives wrapped array in GetValue.
229+ Transforms an ** array that contains array values ** in a closure that receives wrapped array in GetValue.
197230
198231``` php
199232use Wrkflow\GetValue\GetValue;
200233use Wrkflow\GetValue\DataHolders\ArrayData;
201- use Wrkflow\GetValue\Transformers\ArrayGetterTransformer ;
234+ use Wrkflow\GetValue\Transformers\ArrayItemGetterTransformer ;
202235
203236$data = new GetValue(new ArrayData([
204- 'key ' => ['test ' => 'Value!' ]
237+ 'names' => [['name ' => 'Marco', 'surname' => 'Polo'], ['name ' => 'Martin', 'surname' => 'Way'] ]
205238]));
206- $transformer = new ArrayGetterTransformer(function (GetValue $value, string $key): string {
207- return [
208- 'test' => $value->getRequiredString('test'),
209- ];
239+
240+ $transformer = new ArrayItemGetterTransformer(function (GetValue $value, string $key): string {
241+ return $value->getRequiredString('name') . ' '.$value->getRequiredString('surname');
210242});
211243
212- $values = $data->getArray('key', transformers: [$transformer]);
244+ $values = $data->getArray('names', transformers: [$transformer]);
245+ // Result: ['Marco Polo', 'Martin Way']
213246```
214247
215- ## Customization
216-
217- You can create your own transformer by extending:
248+ If you return ` null ` in your closure then value is not added to result array. Use ` ignoreNullResult: false ` in same way
249+ as in [ ArrayItemTransformer] ( #arrayitemtransformer ) `.
218250
219- - For array ` Wrkflow\GetValue\Contracts\TransformerArrayContract `
220- - Rest of the value types use ` Wrkflow\GetValue\Contracts\TransformerContract `
221- - ` $key ` contains full path key from the root data. Array notation is converted to dot notation.
222-
223- Then implement ` public function transform(mixed $value, string $key): mixed; ` . Expect that you can receive an invalid
224- value. Return original ` $value ` if transformation can't be done.
251+ ## Customization
225252
226- Then implement ` public function beforeValidation(mixed $value, string $key): bool; ` which ensures that transformation
227- is not done before validation.
253+ You can create your own transformer by extending ` Wrkflow\GetValue\Contracts\TransformerContract ` class:
228254
229- Then change the strategy:
255+ 1 . Then implement ` public function transform(mixed $value, string $key): mixed; ` . Expect that you can receive an invalid
256+ value. Return original ` $value ` if transformation can't be done.
257+ 2 . Then implement ` public function beforeValidation(mixed $value, string $key): bool; ` which tells if we can transform
258+ the value after or before validation.
259+ - Use before validation for transforming value to a type that ` get ` method expects.
260+ 3 . You can use ` $key ` which contains full path key from the root data. Array notation is converted to dot notation.
261+ 4 . Then change the strategy:
230262
231263``` php
232264use Wrkflow\GetValue\GetValue;
0 commit comments