Skip to content

Commit

Permalink
fix: handle integer key in path mapping modifier
Browse files Browse the repository at this point in the history
The following now works properly:

```php
\CuyZ\Valinor\Mapper\Source\Source::array([0 => 'foo'])
    ->map([
        '0' => 'some_key'
    ]);
```
  • Loading branch information
romm committed Oct 11, 2023
1 parent 5445774 commit 9419f6d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Mapper/Source/Modifier/Mapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/Mapper/Source/Modifier/PathMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class PathMapping implements IteratorAggregate

/**
* @param iterable<mixed> $source
* @param array<string, string> $map
* @param array<string> $map
*/
public function __construct(iterable $source, array $map)
{
Expand Down Expand Up @@ -62,15 +62,15 @@ private function map(iterable $source, array $mappings, int $depth = 0): array
}

/**
* @param array<string, string> $map
* @param array<string> $map
* @return array<Mapping>
*/
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;
Expand Down
2 changes: 1 addition & 1 deletion src/Mapper/Source/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function camelCaseKeys(): Source
}

/**
* @param array<string, string> $map
* @param array<string> $map
*/
public function map(array $map): Source
{
Expand Down
44 changes: 44 additions & 0 deletions tests/Unit/Mapper/Source/Modifier/PathMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand Down

0 comments on commit 9419f6d

Please sign in to comment.