From 9419f6d681e9c6e68a551dd5573bec9c7f928fd5 Mon Sep 17 00:00:00 2001 From: Romain Canon Date: Wed, 11 Oct 2023 10:32:22 +0200 Subject: [PATCH] fix: handle integer key in path mapping modifier The following now works properly: ```php \CuyZ\Valinor\Mapper\Source\Source::array([0 => 'foo']) ->map([ '0' => 'some_key' ]); ``` --- src/Mapper/Source/Modifier/Mapping.php | 2 +- src/Mapper/Source/Modifier/PathMapping.php | 6 +-- src/Mapper/Source/Source.php | 2 +- .../Source/Modifier/PathMappingTest.php | 44 +++++++++++++++++++ 4 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/Mapper/Source/Modifier/Mapping.php b/src/Mapper/Source/Modifier/Mapping.php index f3ce08cc..aa90a904 100644 --- a/src/Mapper/Source/Modifier/Mapping.php +++ b/src/Mapper/Source/Modifier/Mapping.php @@ -28,7 +28,7 @@ public function matches(int|string $key, int $atDepth): bool { $from = $this->keys[$atDepth] ?? null; - return $from === $key || $from === '*'; + return $from === (string)$key || $from === '*'; } public function findMappedKey(int|string $key, int $atDepth): ?string diff --git a/src/Mapper/Source/Modifier/PathMapping.php b/src/Mapper/Source/Modifier/PathMapping.php index f487b409..71eb36d9 100644 --- a/src/Mapper/Source/Modifier/PathMapping.php +++ b/src/Mapper/Source/Modifier/PathMapping.php @@ -22,7 +22,7 @@ final class PathMapping implements IteratorAggregate /** * @param iterable $source - * @param array $map + * @param array $map */ public function __construct(iterable $source, array $map) { @@ -62,7 +62,7 @@ private function map(iterable $source, array $mappings, int $depth = 0): array } /** - * @param array $map + * @param array $map * @return array */ private function prepareMappings(array $map): array @@ -70,7 +70,7 @@ private function prepareMappings(array $map): array $mappings = []; foreach ($map as $from => $to) { - $mappings[] = new Mapping(explode('.', $from), $to); + $mappings[] = new Mapping(explode('.', (string)$from), $to); } return $mappings; diff --git a/src/Mapper/Source/Source.php b/src/Mapper/Source/Source.php index eb71f0f2..e327a405 100644 --- a/src/Mapper/Source/Source.php +++ b/src/Mapper/Source/Source.php @@ -66,7 +66,7 @@ public function camelCaseKeys(): Source } /** - * @param array $map + * @param array $map */ public function map(array $map): Source { diff --git a/tests/Unit/Mapper/Source/Modifier/PathMappingTest.php b/tests/Unit/Mapper/Source/Modifier/PathMappingTest.php index b76b4d01..b2fa659c 100644 --- a/tests/Unit/Mapper/Source/Modifier/PathMappingTest.php +++ b/tests/Unit/Mapper/Source/Modifier/PathMappingTest.php @@ -19,6 +19,16 @@ public function test_root_path_is_mapped(): void self::assertSame(['new_A' => 'bar'], iterator_to_array($source)); } + public function test_root_integer_path_is_mapped(): void + { + $source = new PathMapping( + [0 => 'bar'], + [0 => 'new_A'] + ); + + self::assertSame(['new_A' => 'bar'], iterator_to_array($source)); + } + public function test_sub_path_is_mapped(): void { $source = new PathMapping( @@ -37,6 +47,24 @@ public function test_sub_path_is_mapped(): void ], iterator_to_array($source)); } + public function test_sub_path_with_integer_is_mapped(): void + { + $source = new PathMapping( + [ + 'A' => [ + 1 => 'foo', + ], + ], + ['A.1' => 'new_B'] + ); + + self::assertSame([ + 'A' => [ + 'new_B' => 'foo', + ], + ], iterator_to_array($source)); + } + public function test_root_iterable_path_is_mapped(): void { $source = new PathMapping( @@ -53,6 +81,22 @@ public function test_root_iterable_path_is_mapped(): void ], iterator_to_array($source)); } + public function test_root_iterable_path_with_integer_is_mapped(): void + { + $source = new PathMapping( + [ + [2 => 'bar'], + [2 => 'buz'], + ], + ['*.2' => 'new_A'] + ); + + self::assertSame([ + ['new_A' => 'bar'], + ['new_A' => 'buz'], + ], iterator_to_array($source)); + } + public function test_sub_iterable_numeric_path_is_mapped(): void { $source = new PathMapping(