Skip to content

Commit 1606edb

Browse files
committed
bc: Pass full key with array item index to ArrayItem/ArrayItemGetter transofmers
BREAKING CHANGE: if you used $key (low chance) then now you will get full key including the looped item key
1 parent dbc559f commit 1606edb

13 files changed

+155
-87
lines changed

src/Transformers/ArrayItemGetterTransformer.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public function beforeValidation(mixed $value, string $key): bool
3434

3535
protected function transformItem(mixed $item, string $key, string|int $index, GetValue $getValue): mixed
3636
{
37-
$data = $getValue->makeData($item, $key);
37+
$fullKey = $key . '.' . $index;
38+
$data = $getValue->makeData($item, $fullKey);
3839

3940
if ($data instanceof AbstractData === false) {
4041
throw new NotSupportedDataException($key . ' at ' . $index);
@@ -43,10 +44,10 @@ protected function transformItem(mixed $item, string $key, string|int $index, Ge
4344
$getItemValue = $getValue->makeInstance($data);
4445

4546
if ($this->onItem instanceof GetValueTransformerContract) {
46-
return $this->onItem->transform($getItemValue, $key);
47+
return $this->onItem->transform($getItemValue, $fullKey);
4748
}
4849

49-
return call_user_func_array($this->onItem, [$getItemValue, $key]);
50+
return call_user_func_array($this->onItem, [$getItemValue, $fullKey]);
5051
}
5152

5253
protected function ignoreNullResult(): bool

src/Transformers/ArrayItemTransformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function beforeValidation(mixed $value, string $key): bool
3131

3232
protected function transformItem(mixed $item, string $key, string|int $index, GetValue $getValue): mixed
3333
{
34-
return call_user_func_array($this->onItem, [$item, $key]);
34+
return call_user_func_array($this->onItem, [$item, $key . '.' . $index]);
3535
}
3636

3737
protected function ignoreNullResult(): bool

tests/Transformers/AbstractTransformerTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function data(): array
5656
*/
5757
public function testTransform(TransformerExpectationEntity $entity): void
5858
{
59-
$transformer = $this->getTransformer();
59+
$transformer = $this->getTransformer($entity);
6060
$this->assertValue($transformer, $entity);
6161
$this->assertEquals($entity->expectBeforeValidation, $transformer->beforeValidation($entity->value, 'test'));
6262
}
@@ -101,5 +101,5 @@ public function assertValue(TransformerContract $transformer, TransformerExpecta
101101
}
102102
}
103103

104-
abstract protected function getTransformer(): TransformerContract;
104+
abstract protected function getTransformer(TransformerExpectationEntity $entity): TransformerContract;
105105
}

tests/Transformers/ArrayItemGetterTransformerTest.php

Lines changed: 66 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
use Wrkflow\GetValue\GetValue;
1414
use Wrkflow\GetValue\Transformers\ArrayItemGetterTransformer;
1515

16-
class ArrayItemGetterTransformerTest extends AbstractTransformerTestCase
16+
final class ArrayItemGetterTransformerTest extends AbstractTransformerTestCase
1717
{
18-
final public const KeyValue = 'key';
18+
public const KeyValue = 'key';
1919

20-
final public const ValueToReturnNull = 'return_null';
20+
public const ValueToReturnNull = 'return_null';
2121

2222
public function testExampleArray(): void
2323
{
@@ -62,14 +62,25 @@ public function testExampleXML(): void
6262
CODE_SAMPLE
6363
)));
6464

65-
$transformer = new ArrayItemGetterTransformer(fn (GetValue $value, string $key): string => implode(' ', [
66-
$value->getRequiredString('name'),
67-
$value->getRequiredString('surname'),
68-
$value->getXMLAttributesGetter(['surname'])->getRequiredInt('number'),
69-
]));
65+
$expectedKeys = ['names.0', 'names.1'];
66+
67+
$transformer = new ArrayItemGetterTransformer(function (GetValue $value, string $key) use (
68+
&$expectedKeys
69+
): string {
70+
$expectedKey = array_shift($expectedKeys);
71+
72+
$this->assertEquals($expectedKey, $key, 'Key does not match up');
73+
74+
return implode(' ', [
75+
$value->getRequiredString('name'),
76+
$value->getRequiredString('surname'),
77+
$value->getXMLAttributesGetter(['surname'])->getRequiredInt('number'),
78+
]);
79+
});
7080

7181
$values = $data->getArray('names', transformers: [$transformer]);
7282
$this->assertEquals(['Marco Polo 3', 'Martin Way 2'], $values);
83+
$this->assertEmpty($expectedKeys, 'Expected key match should be empty - loop did not go through all keys');
7384
}
7485

7586
public function dataToTest(): array
@@ -82,7 +93,7 @@ public function dataToTest(): array
8293
*/
8394
public function testBeforeValidation(TransformerExpectationEntity $entity): void
8495
{
85-
$this->assertValue($this->getBeforeValidationTransformer(), $entity);
96+
$this->assertValue($this->getBeforeValidationTransformer($entity), $entity);
8697
}
8798

8899
public function dataToTestBeforeValidation(): array
@@ -95,7 +106,7 @@ public function dataToTestBeforeValidation(): array
95106
*/
96107
public function testAfterValidationForce(TransformerExpectationEntity $entity): void
97108
{
98-
$this->assertValue($this->getForceAfterValidation(), $entity);
109+
$this->assertValue($this->getForceAfterValidation($entity), $entity);
99110
}
100111

101112
public function dataToAfterValidationForce(): array
@@ -105,8 +116,14 @@ public function dataToAfterValidationForce(): array
105116

106117
public function testSupportsEmptyArray(): void
107118
{
108-
$transformer = new ArrayItemGetterTransformer(onItem: function (GetValue $value, string $key): array {
109-
$this->assertEquals('test', $key, 'Key does not match up');
119+
$expectedKeys = ['test.0', 'test.1'];
120+
121+
$transformer = new ArrayItemGetterTransformer(onItem: function (GetValue $value, string $key) use (
122+
&$expectedKeys
123+
): array {
124+
$expectedKey = array_shift($expectedKeys);
125+
126+
$this->assertEquals($expectedKey, $key, 'Key does not match up');
110127

111128
return [
112129
'original' => $value->data->get(),
@@ -124,8 +141,9 @@ public function testSupportsEmptyArray(): void
124141
], [
125142
'original' => $testValue,
126143
]],
127-
expectedValueBeforeValidation: $value
144+
expectedValueBeforeValidation: $value,
128145
));
146+
$this->assertEmpty($expectedKeys, 'Expected key match should be empty - loop did not go through all keys');
129147
}
130148

131149
/**
@@ -135,7 +153,7 @@ public function testBeforeValidationLeaveNull(TransformerExpectationEntity $enti
135153
{
136154
$this->assertValue(
137155
new ArrayItemGetterTransformer(
138-
onItem: $this->getClosure(),
156+
onItem: $this->getClosure($entity),
139157
beforeValidation: true,
140158
ignoreNullResult: false
141159
),
@@ -155,7 +173,7 @@ public function testAfterValidationForceLeaveNull(TransformerExpectationEntity $
155173
{
156174
$this->assertValue(
157175
new ArrayItemGetterTransformer(
158-
onItem: $this->getClosure(),
176+
onItem: $this->getClosure($entity),
159177
beforeValidation: false,
160178
ignoreNullResult: false
161179
),
@@ -174,7 +192,7 @@ public function dataToAfterValidationForceLeaveNull(): array
174192
public function testTransformLeaveNull(TransformerExpectationEntity $entity): void
175193
{
176194
$this->assertValue(
177-
new ArrayItemGetterTransformer(onItem: $this->getClosure(), ignoreNullResult: false),
195+
new ArrayItemGetterTransformer(onItem: $this->getClosure($entity), ignoreNullResult: false),
178196
$entity
179197
);
180198
}
@@ -189,10 +207,11 @@ protected function dataAfterValidationForTransformer(): array
189207
return $this->createData(true, false);
190208
}
191209

192-
protected function getClosure(): Closure
210+
protected function getClosure(TransformerExpectationEntity $entity): Closure
193211
{
194-
return function (GetValue $value, string $key): ?array {
195-
$this->assertEquals('test', $key, 'Key does not match up');
212+
return function (GetValue $value, string $key) use (&$entity): ?array {
213+
$expectedKey = array_shift($entity->expectedKey);
214+
$this->assertEquals($expectedKey, $key, 'Key does not match up');
196215

197216
$value = $value->getRequiredString(self::KeyValue, transformers: []);
198217

@@ -207,22 +226,22 @@ protected function getClosure(): Closure
207226
};
208227
}
209228

210-
protected function getTransformer(): TransformerContract
229+
protected function getTransformer(TransformerExpectationEntity $entity): TransformerContract
211230
{
212-
return new ArrayItemGetterTransformer(onItem: $this->getClosure());
231+
return new ArrayItemGetterTransformer(onItem: $this->getClosure($entity));
213232
}
214233

215-
protected function getBeforeValidationTransformer(): TransformerContract
234+
private function getBeforeValidationTransformer(TransformerExpectationEntity $entity): TransformerContract
216235
{
217-
return new ArrayItemGetterTransformer(onItem: $this->getClosure(), beforeValidation: true);
236+
return new ArrayItemGetterTransformer(onItem: $this->getClosure($entity), beforeValidation: true);
218237
}
219238

220-
protected function getForceAfterValidation(): TransformerContract
239+
private function getForceAfterValidation(TransformerExpectationEntity $entity): TransformerContract
221240
{
222-
return new ArrayItemGetterTransformer(onItem: $this->getClosure(), beforeValidation: false);
241+
return new ArrayItemGetterTransformer(onItem: $this->getClosure($entity), beforeValidation: false);
223242
}
224243

225-
protected function createData(bool $beforeValueIsSameAsValue, bool $leaveNull): array
244+
private function createData(bool $beforeValueIsSameAsValue, bool $leaveNull): array
226245
{
227246
return [
228247
[
@@ -235,7 +254,8 @@ protected function createData(bool $beforeValueIsSameAsValue, bool $leaveNull):
235254
]],
236255
expectedValueBeforeValidation: $beforeValueIsSameAsValue ? [[
237256
self::KeyValue => '',
238-
]] : null
257+
]] : null,
258+
expectedKey: 'test.0',
239259
),
240260
],
241261
[
@@ -248,7 +268,8 @@ protected function createData(bool $beforeValueIsSameAsValue, bool $leaveNull):
248268
]],
249269
expectedValueBeforeValidation: $beforeValueIsSameAsValue ? [[
250270
self::KeyValue => ' ',
251-
]] : null
271+
]] : null,
272+
expectedKey: 'test.0',
252273
),
253274
],
254275
[
@@ -261,7 +282,8 @@ protected function createData(bool $beforeValueIsSameAsValue, bool $leaveNull):
261282
]],
262283
expectedValueBeforeValidation: $beforeValueIsSameAsValue ? [[
263284
self::KeyValue => ' asd ',
264-
]] : null
285+
]] : null,
286+
expectedKey: 'test.0',
265287
),
266288
],
267289
[
@@ -274,7 +296,8 @@ protected function createData(bool $beforeValueIsSameAsValue, bool $leaveNull):
274296
]],
275297
expectedValueBeforeValidation: $beforeValueIsSameAsValue ? [[
276298
self::KeyValue => 'asd ',
277-
]] : null
299+
]] : null,
300+
expectedKey: 'test.0',
278301
),
279302
],
280303
[
@@ -287,7 +310,8 @@ protected function createData(bool $beforeValueIsSameAsValue, bool $leaveNull):
287310
]],
288311
expectedValueBeforeValidation: $beforeValueIsSameAsValue ? [[
289312
self::KeyValue => 'asd mix',
290-
]] : null
313+
]] : null,
314+
expectedKey: 'test.0',
291315
),
292316
],
293317
[
@@ -306,7 +330,8 @@ protected function createData(bool $beforeValueIsSameAsValue, bool $leaveNull):
306330
'test' => [
307331
self::KeyValue => 'asd mix',
308332
],
309-
] : null
333+
] : null,
334+
expectedKey: 'test.test',
310335
),
311336
],
312337
[
@@ -317,7 +342,8 @@ protected function createData(bool $beforeValueIsSameAsValue, bool $leaveNull):
317342
expectedValue: $leaveNull ? [null] : [],
318343
expectedValueBeforeValidation: $beforeValueIsSameAsValue ? [[
319344
self::KeyValue => self::ValueToReturnNull,
320-
]] : null
345+
]] : null,
346+
expectedKey: 'test.0',
321347
),
322348
],
323349
[
@@ -334,14 +360,17 @@ protected function createData(bool $beforeValueIsSameAsValue, bool $leaveNull):
334360
'test' => [
335361
self::KeyValue => self::ValueToReturnNull,
336362
],
337-
] : null
363+
] : null,
364+
expectedKey: 'test.test',
338365
),
339366
],
340367
[new TransformerExpectationEntity(value: null, expectedValue: null)],
341368
[
342-
new TransformerExpectationEntity(value: [
343-
'test',
344-
], expectedValue: null, expectException: NotSupportedDataException::class),
369+
new TransformerExpectationEntity(
370+
value: ['test'],
371+
expectedValue: null,
372+
expectException: NotSupportedDataException::class,
373+
),
345374
],
346375
];
347376
}

0 commit comments

Comments
 (0)