diff --git a/docs/normalizer.md b/docs/normalizer.md index 5e0d27fe..023c702c 100644 --- a/docs/normalizer.md +++ b/docs/normalizer.md @@ -78,6 +78,13 @@ final class Profile public array $explicitMeta; } ``` +:::note +Only the keys with a normalizer are converted. All other keys, including keys +that are not part of the shape, are kept as they are, so they must already hold +values that your storage can handle. A `null` value is passed to the normalizer +of its key, like for any other property. +::: + ## DateTimeImmutable With the `DateTimeImmutableNormalizer` you can convert `DateTimeImmutable` diff --git a/src/Normalizer/ArrayShapeNormalizer.php b/src/Normalizer/ArrayShapeNormalizer.php index 23d99619..3a67a084 100644 --- a/src/Normalizer/ArrayShapeNormalizer.php +++ b/src/Normalizer/ArrayShapeNormalizer.php @@ -10,6 +10,7 @@ use Symfony\Component\TypeInfo\Type\ArrayShapeType; use Symfony\Component\TypeInfo\Type\NullableType; +use function array_key_exists; use function is_array; #[Attribute(Attribute::TARGET_PROPERTY)] @@ -36,17 +37,15 @@ public function normalize(mixed $value, array $context): array|null throw InvalidArgument::withWrongType('array|null', $value); } - $result = []; - foreach ($this->normalizerMap as $field => $normalizer) { - if (!isset($value[$field])) { + if (!array_key_exists($field, $value)) { continue; } - $result[$field] = $normalizer->normalize($value[$field], $context); + $value[$field] = $normalizer->normalize($value[$field], $context); } - return $result; + return $value; } /** @@ -64,17 +63,15 @@ public function denormalize(mixed $value, array $context): array|null throw InvalidArgument::withWrongType('array|null', $value); } - $result = []; - foreach ($this->normalizerMap as $field => $normalizer) { - if (!isset($value[$field])) { + if (!array_key_exists($field, $value)) { continue; } - $result[$field] = $normalizer->denormalize($value[$field], $context); + $value[$field] = $normalizer->denormalize($value[$field], $context); } - return $result; + return $value; } public function setHydrator(Hydrator $hydrator): void diff --git a/tests/Unit/Fixture/InferNormalizerWithArrayShapeDto.php b/tests/Unit/Fixture/InferNormalizerWithArrayShapeDto.php new file mode 100644 index 00000000..d96cc256 --- /dev/null +++ b/tests/Unit/Fixture/InferNormalizerWithArrayShapeDto.php @@ -0,0 +1,16 @@ +, status: Status|null, createdAt: DateTimeImmutable} $profile */ + public function __construct( + public array $profile, + ) { + } +} diff --git a/tests/Unit/Normalizer/ArrayShapeNormalizerTest.php b/tests/Unit/Normalizer/ArrayShapeNormalizerTest.php index 78be01dc..156a89a5 100644 --- a/tests/Unit/Normalizer/ArrayShapeNormalizerTest.php +++ b/tests/Unit/Normalizer/ArrayShapeNormalizerTest.php @@ -93,6 +93,72 @@ public function denormalize(mixed $value, array $context): int $this->assertEquals(['foo' => 1], $normalizer->denormalize(['foo' => '1'], [])); } + public function testNormalizeKeepsKeysWithoutNormalizer(): void + { + $innerNormalizer = $this->createMock(Normalizer::class); + $innerNormalizer->expects($this->once())->method('normalize')->with(1, [])->willReturn('1'); + + $normalizer = new ArrayShapeNormalizer(['foo' => $innerNormalizer]); + + $this->assertSame( + ['height' => 180, 'foo' => '1', 'note' => null, 'unknown' => ['x']], + $normalizer->normalize(['height' => 180, 'foo' => 1, 'note' => null, 'unknown' => ['x']], []), + ); + } + + public function testDenormalizeKeepsKeysWithoutNormalizer(): void + { + $innerNormalizer = $this->createMock(Normalizer::class); + $innerNormalizer->expects($this->once())->method('denormalize')->with('1', [])->willReturn(1); + + $normalizer = new ArrayShapeNormalizer(['foo' => $innerNormalizer]); + + $this->assertSame( + ['height' => 180, 'foo' => 1, 'note' => null, 'unknown' => ['x']], + $normalizer->denormalize(['height' => 180, 'foo' => '1', 'note' => null, 'unknown' => ['x']], []), + ); + } + + public function testNormalizePassesNullToInnerNormalizer(): void + { + $innerNormalizer = $this->createMock(Normalizer::class); + $innerNormalizer->expects($this->once())->method('normalize')->with(null, [])->willReturn(null); + + $normalizer = new ArrayShapeNormalizer(['foo' => $innerNormalizer]); + + $this->assertSame(['foo' => null], $normalizer->normalize(['foo' => null], [])); + } + + public function testDenormalizePassesNullToInnerNormalizer(): void + { + $innerNormalizer = $this->createMock(Normalizer::class); + $innerNormalizer->expects($this->once())->method('denormalize')->with(null, [])->willReturn(null); + + $normalizer = new ArrayShapeNormalizer(['foo' => $innerNormalizer]); + + $this->assertSame(['foo' => null], $normalizer->denormalize(['foo' => null], [])); + } + + public function testNormalizeSkipsMissingKeys(): void + { + $innerNormalizer = $this->createMock(Normalizer::class); + $innerNormalizer->expects($this->never())->method('normalize'); + + $normalizer = new ArrayShapeNormalizer(['foo' => $innerNormalizer]); + + $this->assertSame(['bar' => 1], $normalizer->normalize(['bar' => 1], [])); + } + + public function testDenormalizeSkipsMissingKeys(): void + { + $innerNormalizer = $this->createMock(Normalizer::class); + $innerNormalizer->expects($this->never())->method('denormalize'); + + $normalizer = new ArrayShapeNormalizer(['foo' => $innerNormalizer]); + + $this->assertSame(['bar' => 1], $normalizer->denormalize(['bar' => 1], [])); + } + public function testNormalizePassesContextToInnerNormalizer(): void { $context = ['key' => 'value']; diff --git a/tests/Unit/StackHydratorTest.php b/tests/Unit/StackHydratorTest.php index 8b8e3195..61312367 100644 --- a/tests/Unit/StackHydratorTest.php +++ b/tests/Unit/StackHydratorTest.php @@ -33,6 +33,7 @@ use Patchlevel\Hydrator\Tests\Unit\Fixture\DummyMiddleware; use Patchlevel\Hydrator\Tests\Unit\Fixture\Email; use Patchlevel\Hydrator\Tests\Unit\Fixture\InferNormalizerDto; +use Patchlevel\Hydrator\Tests\Unit\Fixture\InferNormalizerWithArrayShapeDto; use Patchlevel\Hydrator\Tests\Unit\Fixture\InferNormalizerWithIterablesDto; use Patchlevel\Hydrator\Tests\Unit\Fixture\InferNormalizerWithNullableDto; use Patchlevel\Hydrator\Tests\Unit\Fixture\LazyProfileCreated; @@ -557,6 +558,30 @@ public function testHydrateWithInferNormalizerWitIterables(): void self::assertEquals($expected, $event); } + public function testRoundTripWithArrayShape(): void + { + $object = new InferNormalizerWithArrayShapeDto([ + 'height' => 180, + 'nickname' => null, + 'skills' => [new Skill('php'), new Skill('go')], + 'status' => null, + 'createdAt' => new DateTimeImmutable('2015-02-13 22:34:32+01:00'), + ]); + + $data = [ + 'profile' => [ + 'height' => 180, + 'nickname' => null, + 'skills' => [['name' => 'php'], ['name' => 'go']], + 'status' => null, + 'createdAt' => '2015-02-13T22:34:32+01:00', + ], + ]; + + self::assertSame($data, $this->hydrator->extract($object)); + self::assertEquals($object, $this->hydrator->hydrate(InferNormalizerWithArrayShapeDto::class, $data)); + } + public function testHydrateWithClassNormalizer(): void { $object = $this->hydrator->hydrate(