@@ -221,7 +221,10 @@ $transformer = new ArrayItemTransformer(function (mixed $value, string $key): ?a
221
221
$values = $data->getArray('names', transformers: [$transformer]);
222
222
// Result: [['Marco', 'Polo'], ['Way', 'Point'], null]
223
223
```
224
- ### GetterTransformer (since v0.6.0)
224
+
225
+ ### GetterTransformer
226
+
227
+ > Since v0.6.0
225
228
226
229
Transforms an ** array/xml value** in a closure that receives wrapped GetValue instance.
227
230
@@ -242,6 +245,34 @@ $value = $data->getString('person', transformers: [$transformer]);
242
245
// Result: 'Marco Polo'
243
246
```
244
247
248
+ #### Passing object instead of closure
249
+
250
+ > Since v0.6.1
251
+
252
+ To make transformers more re-usable you can pass an object that implements ` Wrkflow\GetValue\Contracts\GetValueTransformerContract ` interface.
253
+
254
+ ``` php
255
+ use Wrkflow\GetValue\Contracts\GetValueTransformerContract;
256
+ use Wrkflow\GetValue\GetValue;
257
+ use Wrkflow\GetValue\DataHolders\ArrayData;
258
+ use Wrkflow\GetValue\Transformers\GetterTransformer;
259
+
260
+ class GetNameTransformer implements GetValueTransformerContract
261
+ {
262
+ public function transform(GetValue $value, string $key): string
263
+ {
264
+ return implode(' ', [$value->getRequiredString('name'), $value->getRequiredString('surname')]);
265
+ }
266
+ }
267
+
268
+ $data = new GetValue(new ArrayData([
269
+ 'person' => ['name' => 'Marco', 'surname' => 'Polo'],
270
+ ]));
271
+
272
+ $value = $data->getString('person', transformers: [new GetterTransformer(new GetNameTransformer(), true)]);
273
+ // Result: 'Marco Polo'
274
+ ```
275
+
245
276
### ArrayItemGetterTransformer
246
277
247
278
> Can be used only with get\* Array\* methods. Throws NotAnArrayException if array value is not an array.
@@ -257,9 +288,10 @@ $data = new GetValue(new ArrayData([
257
288
'names' => [['name' => 'Marco', 'surname' => 'Polo'], ['name' => 'Martin', 'surname' => 'Way']]
258
289
]));
259
290
260
- $transformer = new ArrayItemGetterTransformer(function (GetValue $value, string $key): string {
261
- return $value->getRequiredString('name') . ' '.$value->getRequiredString('surname');
262
- });
291
+ $transformer = new ArrayItemGetterTransformer(fn (GetValue $value, string $key): string => implode(' ', [
292
+ $value->getRequiredString('name'),
293
+ $value->getRequiredString('surname'),
294
+ ]));
263
295
264
296
$values = $data->getArray('names', transformers: [$transformer]);
265
297
// Result: ['Marco Polo', 'Martin Way']
@@ -268,6 +300,34 @@ $values = $data->getArray('names', transformers: [$transformer]);
268
300
If you return ` null ` in your closure then value is not added to result array. Use ` ignoreNullResult: false ` in same way
269
301
as in [ ArrayItemTransformer] ( #arrayitemtransformer ) `.
270
302
303
+ #### Passing object instead of closure
304
+
305
+ > Since v0.6.1
306
+
307
+ To make transformers more re-usable you can pass an object that implements ` Wrkflow\GetValue\Contracts\GetValueTransformerContract ` interface.
308
+
309
+ ``` php
310
+ use Wrkflow\GetValue\Contracts\GetValueTransformerContract;
311
+ use Wrkflow\GetValue\GetValue;
312
+ use Wrkflow\GetValue\DataHolders\ArrayData;
313
+ use Wrkflow\GetValue\Transformers\ArrayItemGetterTransformer;
314
+
315
+ class GetNameTransformer implements GetValueTransformerContract
316
+ {
317
+ public function transform(GetValue $value, string $key): string
318
+ {
319
+ return implode(' ', [$value->getRequiredString('name'), $value->getRequiredString('surname')]);
320
+ }
321
+ }
322
+
323
+ $data = new GetValue(new ArrayData([
324
+ 'names' => [['name' => 'Marco', 'surname' => 'Polo'], ['name' => 'Martin', 'surname' => 'Way']]
325
+ ]));
326
+
327
+ $values = $data->getArray('names', transformers: [new ArrayItemGetterTransformer(new GetNameTransformer())];
328
+ // Result: ['Marco Polo', 'Martin Way']
329
+ ```
330
+
271
331
## Customization
272
332
273
333
You can create your own transformer by extending ` Wrkflow\GetValue\Contracts\TransformerContract ` class:
0 commit comments