Skip to content

Commit

Permalink
bc: Pass full key with array item index to ArrayItem/ArrayItemGetter …
Browse files Browse the repository at this point in the history
…transofmers

BREAKING CHANGE: if you used $key (low chance) then now you will get full key including the looped item key
  • Loading branch information
pionl committed Mar 27, 2024
1 parent dbc559f commit 1606edb
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 87 deletions.
7 changes: 4 additions & 3 deletions src/Transformers/ArrayItemGetterTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public function beforeValidation(mixed $value, string $key): bool

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

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

if ($this->onItem instanceof GetValueTransformerContract) {
return $this->onItem->transform($getItemValue, $key);
return $this->onItem->transform($getItemValue, $fullKey);
}

return call_user_func_array($this->onItem, [$getItemValue, $key]);
return call_user_func_array($this->onItem, [$getItemValue, $fullKey]);
}

protected function ignoreNullResult(): bool
Expand Down
2 changes: 1 addition & 1 deletion src/Transformers/ArrayItemTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function beforeValidation(mixed $value, string $key): bool

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

protected function ignoreNullResult(): bool
Expand Down
4 changes: 2 additions & 2 deletions tests/Transformers/AbstractTransformerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function data(): array
*/
public function testTransform(TransformerExpectationEntity $entity): void
{
$transformer = $this->getTransformer();
$transformer = $this->getTransformer($entity);
$this->assertValue($transformer, $entity);
$this->assertEquals($entity->expectBeforeValidation, $transformer->beforeValidation($entity->value, 'test'));
}
Expand Down Expand Up @@ -101,5 +101,5 @@ public function assertValue(TransformerContract $transformer, TransformerExpecta
}
}

abstract protected function getTransformer(): TransformerContract;
abstract protected function getTransformer(TransformerExpectationEntity $entity): TransformerContract;
}
103 changes: 66 additions & 37 deletions tests/Transformers/ArrayItemGetterTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
use Wrkflow\GetValue\GetValue;
use Wrkflow\GetValue\Transformers\ArrayItemGetterTransformer;

class ArrayItemGetterTransformerTest extends AbstractTransformerTestCase
final class ArrayItemGetterTransformerTest extends AbstractTransformerTestCase
{
final public const KeyValue = 'key';
public const KeyValue = 'key';

final public const ValueToReturnNull = 'return_null';
public const ValueToReturnNull = 'return_null';

public function testExampleArray(): void
{
Expand Down Expand Up @@ -62,14 +62,25 @@ public function testExampleXML(): void
CODE_SAMPLE
)));

$transformer = new ArrayItemGetterTransformer(fn (GetValue $value, string $key): string => implode(' ', [
$value->getRequiredString('name'),
$value->getRequiredString('surname'),
$value->getXMLAttributesGetter(['surname'])->getRequiredInt('number'),
]));
$expectedKeys = ['names.0', 'names.1'];

$transformer = new ArrayItemGetterTransformer(function (GetValue $value, string $key) use (
&$expectedKeys
): string {
$expectedKey = array_shift($expectedKeys);

$this->assertEquals($expectedKey, $key, 'Key does not match up');

return implode(' ', [
$value->getRequiredString('name'),
$value->getRequiredString('surname'),
$value->getXMLAttributesGetter(['surname'])->getRequiredInt('number'),
]);
});

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

public function dataToTest(): array
Expand All @@ -82,7 +93,7 @@ public function dataToTest(): array
*/
public function testBeforeValidation(TransformerExpectationEntity $entity): void
{
$this->assertValue($this->getBeforeValidationTransformer(), $entity);
$this->assertValue($this->getBeforeValidationTransformer($entity), $entity);
}

public function dataToTestBeforeValidation(): array
Expand All @@ -95,7 +106,7 @@ public function dataToTestBeforeValidation(): array
*/
public function testAfterValidationForce(TransformerExpectationEntity $entity): void
{
$this->assertValue($this->getForceAfterValidation(), $entity);
$this->assertValue($this->getForceAfterValidation($entity), $entity);
}

public function dataToAfterValidationForce(): array
Expand All @@ -105,8 +116,14 @@ public function dataToAfterValidationForce(): array

public function testSupportsEmptyArray(): void
{
$transformer = new ArrayItemGetterTransformer(onItem: function (GetValue $value, string $key): array {
$this->assertEquals('test', $key, 'Key does not match up');
$expectedKeys = ['test.0', 'test.1'];

$transformer = new ArrayItemGetterTransformer(onItem: function (GetValue $value, string $key) use (
&$expectedKeys
): array {
$expectedKey = array_shift($expectedKeys);

$this->assertEquals($expectedKey, $key, 'Key does not match up');

return [
'original' => $value->data->get(),
Expand All @@ -124,8 +141,9 @@ public function testSupportsEmptyArray(): void
], [
'original' => $testValue,
]],
expectedValueBeforeValidation: $value
expectedValueBeforeValidation: $value,
));
$this->assertEmpty($expectedKeys, 'Expected key match should be empty - loop did not go through all keys');
}

/**
Expand All @@ -135,7 +153,7 @@ public function testBeforeValidationLeaveNull(TransformerExpectationEntity $enti
{
$this->assertValue(
new ArrayItemGetterTransformer(
onItem: $this->getClosure(),
onItem: $this->getClosure($entity),
beforeValidation: true,
ignoreNullResult: false
),
Expand All @@ -155,7 +173,7 @@ public function testAfterValidationForceLeaveNull(TransformerExpectationEntity $
{
$this->assertValue(
new ArrayItemGetterTransformer(
onItem: $this->getClosure(),
onItem: $this->getClosure($entity),
beforeValidation: false,
ignoreNullResult: false
),
Expand All @@ -174,7 +192,7 @@ public function dataToAfterValidationForceLeaveNull(): array
public function testTransformLeaveNull(TransformerExpectationEntity $entity): void
{
$this->assertValue(
new ArrayItemGetterTransformer(onItem: $this->getClosure(), ignoreNullResult: false),
new ArrayItemGetterTransformer(onItem: $this->getClosure($entity), ignoreNullResult: false),
$entity
);
}
Expand All @@ -189,10 +207,11 @@ protected function dataAfterValidationForTransformer(): array
return $this->createData(true, false);
}

protected function getClosure(): Closure
protected function getClosure(TransformerExpectationEntity $entity): Closure
{
return function (GetValue $value, string $key): ?array {
$this->assertEquals('test', $key, 'Key does not match up');
return function (GetValue $value, string $key) use (&$entity): ?array {
$expectedKey = array_shift($entity->expectedKey);
$this->assertEquals($expectedKey, $key, 'Key does not match up');

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

Expand All @@ -207,22 +226,22 @@ protected function getClosure(): Closure
};
}

protected function getTransformer(): TransformerContract
protected function getTransformer(TransformerExpectationEntity $entity): TransformerContract
{
return new ArrayItemGetterTransformer(onItem: $this->getClosure());
return new ArrayItemGetterTransformer(onItem: $this->getClosure($entity));
}

protected function getBeforeValidationTransformer(): TransformerContract
private function getBeforeValidationTransformer(TransformerExpectationEntity $entity): TransformerContract
{
return new ArrayItemGetterTransformer(onItem: $this->getClosure(), beforeValidation: true);
return new ArrayItemGetterTransformer(onItem: $this->getClosure($entity), beforeValidation: true);
}

protected function getForceAfterValidation(): TransformerContract
private function getForceAfterValidation(TransformerExpectationEntity $entity): TransformerContract
{
return new ArrayItemGetterTransformer(onItem: $this->getClosure(), beforeValidation: false);
return new ArrayItemGetterTransformer(onItem: $this->getClosure($entity), beforeValidation: false);
}

protected function createData(bool $beforeValueIsSameAsValue, bool $leaveNull): array
private function createData(bool $beforeValueIsSameAsValue, bool $leaveNull): array
{
return [
[
Expand All @@ -235,7 +254,8 @@ protected function createData(bool $beforeValueIsSameAsValue, bool $leaveNull):
]],
expectedValueBeforeValidation: $beforeValueIsSameAsValue ? [[
self::KeyValue => '',
]] : null
]] : null,
expectedKey: 'test.0',
),
],
[
Expand All @@ -248,7 +268,8 @@ protected function createData(bool $beforeValueIsSameAsValue, bool $leaveNull):
]],
expectedValueBeforeValidation: $beforeValueIsSameAsValue ? [[
self::KeyValue => ' ',
]] : null
]] : null,
expectedKey: 'test.0',
),
],
[
Expand All @@ -261,7 +282,8 @@ protected function createData(bool $beforeValueIsSameAsValue, bool $leaveNull):
]],
expectedValueBeforeValidation: $beforeValueIsSameAsValue ? [[
self::KeyValue => ' asd ',
]] : null
]] : null,
expectedKey: 'test.0',
),
],
[
Expand All @@ -274,7 +296,8 @@ protected function createData(bool $beforeValueIsSameAsValue, bool $leaveNull):
]],
expectedValueBeforeValidation: $beforeValueIsSameAsValue ? [[
self::KeyValue => 'asd ',
]] : null
]] : null,
expectedKey: 'test.0',
),
],
[
Expand All @@ -287,7 +310,8 @@ protected function createData(bool $beforeValueIsSameAsValue, bool $leaveNull):
]],
expectedValueBeforeValidation: $beforeValueIsSameAsValue ? [[
self::KeyValue => 'asd mix',
]] : null
]] : null,
expectedKey: 'test.0',
),
],
[
Expand All @@ -306,7 +330,8 @@ protected function createData(bool $beforeValueIsSameAsValue, bool $leaveNull):
'test' => [
self::KeyValue => 'asd mix',
],
] : null
] : null,
expectedKey: 'test.test',
),
],
[
Expand All @@ -317,7 +342,8 @@ protected function createData(bool $beforeValueIsSameAsValue, bool $leaveNull):
expectedValue: $leaveNull ? [null] : [],
expectedValueBeforeValidation: $beforeValueIsSameAsValue ? [[
self::KeyValue => self::ValueToReturnNull,
]] : null
]] : null,
expectedKey: 'test.0',
),
],
[
Expand All @@ -334,14 +360,17 @@ protected function createData(bool $beforeValueIsSameAsValue, bool $leaveNull):
'test' => [
self::KeyValue => self::ValueToReturnNull,
],
] : null
] : null,
expectedKey: 'test.test',
),
],
[new TransformerExpectationEntity(value: null, expectedValue: null)],
[
new TransformerExpectationEntity(value: [
'test',
], expectedValue: null, expectException: NotSupportedDataException::class),
new TransformerExpectationEntity(
value: ['test'],
expectedValue: null,
expectException: NotSupportedDataException::class,
),
],
];
}
Expand Down
Loading

0 comments on commit 1606edb

Please sign in to comment.