Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

misc: extract array path mapping to a dedicated class #289

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 2 additions & 62 deletions src/Mapper/Source/Modifier/PathMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

namespace CuyZ\Valinor\Mapper\Source\Modifier;

use CuyZ\Valinor\Utility\Path\PathMapper;
use IteratorAggregate;
use Traversable;

use function explode;
use function is_array;

/**
* @api
*
Expand All @@ -26,69 +24,11 @@ final class PathMapping implements IteratorAggregate
*/
public function __construct(iterable $source, array $map)
{
$this->source = $this->map($source, $this->prepareMappings($map));
$this->source = (new PathMapper())->map($source, $map);
}

public function getIterator(): Traversable
{
yield from $this->source;
}

/**
* @param iterable<mixed> $source
* @param array<Mapping> $mappings
* @return array<mixed>
*/
private function map(iterable $source, array $mappings, int $depth = 0): array
{
$out = [];

foreach ($source as $key => $value) {
/** @var int|string $key */
$newMappings = array_filter($mappings, fn (Mapping $mapping) => $mapping->matches($key, $depth));

$newKey = $this->findMapping($newMappings, $depth, $key);

if (is_array($value)) {
$out[$newKey] = $this->map($value, $newMappings, $depth + 1);

continue;
}

$out[$newKey] = $value;
}

return $out;
}

/**
* @param array<string, string> $map
* @return array<Mapping>
*/
private function prepareMappings(array $map): array
{
$mappings = [];

foreach ($map as $from => $to) {
$mappings[] = new Mapping(explode('.', $from), $to);
}

return $mappings;
}

/**
* @param array<Mapping> $mappings
*/
private function findMapping(array $mappings, int $atDepth, int|string $key): int|string
{
foreach ($mappings as $mapping) {
$mappedKey = $mapping->findMappedKey($key, $atDepth);

if (null !== $mappedKey) {
return $mappedKey;
}
}

return $key;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace CuyZ\Valinor\Mapper\Source\Modifier;
namespace CuyZ\Valinor\Utility\Path;

/** @internal */
final class Mapping
Expand Down
77 changes: 77 additions & 0 deletions src/Utility/Path/PathMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace CuyZ\Valinor\Utility\Path;

/** @internal */
final class PathMapper
{
/**
* @param iterable<mixed> $source
* @param array<string, string> $map
* @return array<mixed>
*/
public function map(iterable $source, array $map): array
{
return $this->doMap($source, $this->prepareMappings($map));
}

/**
* @param iterable<mixed> $source
* @param array<Mapping> $mappings
* @return array<mixed>
*/
private function doMap(iterable $source, array $mappings, int $depth = 0): array
{
$out = [];

foreach ($source as $key => $value) {
/** @var int|string $key */
$newMappings = array_filter($mappings, static fn (Mapping $mapping) => $mapping->matches($key, $depth));

$newKey = $this->findMapping($newMappings, $depth, $key);

if (is_array($value)) {
$out[$newKey] = $this->doMap($value, $newMappings, $depth + 1);

continue;
}

$out[$newKey] = $value;
}

return $out;
}

/**
* @param array<string, string> $map
* @return array<Mapping>
*/
private function prepareMappings(array $map): array
{
$mappings = [];

foreach ($map as $from => $to) {
$mappings[] = new Mapping(explode('.', $from), $to);
}

return $mappings;
}

/**
* @param array<Mapping> $mappings
*/
private function findMapping(array $mappings, int $atDepth, int|string $key): int|string
{
foreach ($mappings as $mapping) {
$mappedKey = $mapping->findMappedKey($key, $atDepth);

if (null !== $mappedKey) {
return $mappedKey;
}
}

return $key;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace CuyZ\Valinor\Tests\Unit\Mapper\Source\Modifier;
namespace CuyZ\Valinor\Tests\Unit\Utility\Path;

use CuyZ\Valinor\Mapper\Source\Modifier\Mapping;
use CuyZ\Valinor\Utility\Path\Mapping;
use PHPUnit\Framework\TestCase;

final class MappingTest extends TestCase
Expand Down
204 changes: 204 additions & 0 deletions tests/Unit/Utility/Path/PathMapperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
<?php

declare(strict_types=1);

namespace CuyZ\Valinor\Tests\Unit\Utility\Path;

use CuyZ\Valinor\Utility\Path\PathMapper;
use PHPUnit\Framework\TestCase;

final class PathMapperTest extends TestCase
{
public function test_root_path_is_mapped(): void
{
$result = (new PathMapper())->map(
['A' => 'bar'],
['A' => 'new_A']
);

self::assertSame(['new_A' => 'bar'], $result);
}

public function test_sub_path_is_mapped(): void
{
$result = (new PathMapper())->map(
[
'A' => [
'B' => 'foo',
],
],
['A.B' => 'new_B']
);

self::assertSame([
'A' => [
'new_B' => 'foo',
],
], $result);
}

public function test_root_iterable_path_is_mapped(): void
{
$result = (new PathMapper())->map(
[
['A' => 'bar'],
['A' => 'buz'],
],
['*.A' => 'new_A']
);

self::assertSame([
['new_A' => 'bar'],
['new_A' => 'buz'],
], $result);
}

public function test_sub_iterable_numeric_path_is_mapped(): void
{
$result = (new PathMapper())->map(
[
'A' => [
['C' => 'bar'],
['C' => 'buz'],
],
],
['A.*.C' => 'new_C']
);

self::assertSame([
'A' => [
['new_C' => 'bar'],
['new_C' => 'buz'],
],
], $result);
}

public function test_sub_iterable_string_path_is_mapped(): void
{
$result = (new PathMapper())->map(
[
'A' => [
'B1' => ['C' => 'bar'],
'B2' => ['C' => 'buz'],
],
],
['A.*.C' => 'new_C'],
);

self::assertSame([
'A' => [
'B1' => ['new_C' => 'bar'],
'B2' => ['new_C' => 'buz'],
],
], $result);
}

public function test_path_with_sub_paths_are_mapped(): void
{
$result = (new PathMapper())->map(
[
'A' => [
['B' => 'bar'],
['B' => 'buz'],
],
],
[
'A' => 'new_A',
'A.*.B' => 'new_B',
]
);

self::assertSame([
'new_A' => [
['new_B' => 'bar'],
['new_B' => 'buz'],
],
], $result);
}

public function test_conflicting_paths_are_mapped(): void
{
$result = (new PathMapper())->map(
[
'A1' => [
'B' => 'foo',
],
'A2' => [
'B' => 'bar',
],
],
[
'A1.B' => 'new_B1',
]
);

self::assertSame([
'A1' => [
'new_B1' => 'foo',
],
'A2' => [
'B' => 'bar',
],
], $result);
}

public function test_sub_iterable_numeric_path_with_sub_key_is_mapped(): void
{
$result = (new PathMapper())->map(
[
'A' => [
[
'B' => ['C' => 'bar'],
],
[
'B' => ['C' => 'buz'],
],
],
],
[
'A.*.B.C' => 'new_C',
]
);

self::assertSame([
'A' => [
[
'B' => ['new_C' => 'bar'],
],
[
'B' => ['new_C' => 'buz'],
],
],
], $result);
}

public function test_sub_iterable_string_path_with_sub_key_is_mapped(): void
{
$result = (new PathMapper())->map(
[
'A' => [
'B1' => [
'C' => ['D' => 'bar'],
],
'B2' => [
'C' => ['D' => 'buz'],
],
],
],
[
'A.*.C.D' => 'new_D',
]
);

self::assertSame([
'A' => [
'B1' => [
'C' => ['new_D' => 'bar'],
],
'B2' => [
'C' => ['new_D' => 'buz'],
],
],
], $result);
}
}