@@ -138,6 +138,30 @@ $transformer = new ArrayTransformer(function (array $value, string $key): array
138
138
});
139
139
140
140
$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
141
165
```
142
166
143
167
### ArrayItemTransformer
@@ -153,80 +177,88 @@ use Wrkflow\GetValue\Transformers\ArrayItemTransformer;
153
177
use Wrkflow\GetValue\Exceptions\ValidationFailedException;
154
178
155
179
$data = new GetValue(new ArrayData([
156
- 'key ' => ['Marco', 'Polo']
180
+ 'names ' => [[ 'Marco', 'Polo'], ['Way', 'Point'], [] ]
157
181
]));
158
182
$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);
164
192
});
165
193
166
- $values = $data->getArray('key', transformers: [$transformer]);
194
+ $values = $data->getArray('names', transformers: [$transformer]);
195
+ // Result: ['Marco Polo', 'Way Point']
167
196
```
168
197
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.
174
199
175
200
``` php
176
201
use Wrkflow\GetValue\GetValue;
177
202
use Wrkflow\GetValue\DataHolders\ArrayData;
178
- use Wrkflow\GetValue\Transformers\ArrayItemGetterTransformer;
203
+ use Wrkflow\GetValue\Transformers\ArrayItemTransformer;
204
+ use Wrkflow\GetValue\Exceptions\ValidationFailedException;
179
205
180
206
$data = new GetValue(new ArrayData([
181
- 'key ' => [['test' => 'Marco'], ['test' => 'Polo']]
207
+ 'names ' => ['Marco Polo', 'Way Point', ''],
182
208
]));
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
+ }
188
213
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]
190
223
```
191
224
192
225
### ArrayItemGetterTransformer
193
226
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.
195
228
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.
197
230
198
231
``` php
199
232
use Wrkflow\GetValue\GetValue;
200
233
use Wrkflow\GetValue\DataHolders\ArrayData;
201
- use Wrkflow\GetValue\Transformers\ArrayGetterTransformer ;
234
+ use Wrkflow\GetValue\Transformers\ArrayItemGetterTransformer ;
202
235
203
236
$data = new GetValue(new ArrayData([
204
- 'key ' => ['test ' => 'Value!' ]
237
+ 'names' => [['name ' => 'Marco', 'surname' => 'Polo'], ['name ' => 'Martin', 'surname' => 'Way'] ]
205
238
]));
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');
210
242
});
211
243
212
- $values = $data->getArray('key', transformers: [$transformer]);
244
+ $values = $data->getArray('names', transformers: [$transformer]);
245
+ // Result: ['Marco Polo', 'Martin Way']
213
246
```
214
247
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 ) `.
218
250
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
225
252
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:
228
254
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:
230
262
231
263
``` php
232
264
use Wrkflow\GetValue\GetValue;
0 commit comments