mirror of
https://github.com/danog/Valinor.git
synced 2025-01-10 22:59:04 +01:00
ad51039cc3
The `Source` class is a new entry point for sources that are not plain array or iterable. It allows accessing other features like camel-case keys or custom paths mapping in a convenient way. It should be used as follows: ```php $source = \CuyZ\Valinor\Mapper\Source\Source::json($jsonString) ->camelCaseKeys() ->map([ 'towns' => 'cities', 'towns.*.label' => 'name', ]); $result = (new \CuyZ\Valinor\MapperBuilder()) ->mapper() ->map(SomeClass::class, $source); ```
110 lines
2.9 KiB
PHP
110 lines
2.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace CuyZ\Valinor\Tests\Integration\Mapping\Source\Modifier;
|
|
|
|
use CuyZ\Valinor\Mapper\MappingError;
|
|
use CuyZ\Valinor\Mapper\Source\Modifier\PathMapping;
|
|
use CuyZ\Valinor\Tests\Integration\IntegrationTest;
|
|
|
|
final class PathMappingTest extends IntegrationTest
|
|
{
|
|
public function test_path_with_sub_paths_are_mapped(): void
|
|
{
|
|
$map = [
|
|
'A1' => 'newA1',
|
|
'A1.B1' => 'value',
|
|
'A2' => 'newA2',
|
|
'A2.*.B1' => 'value',
|
|
'A3' => 'newA3',
|
|
'A3.B1' => 'newB1',
|
|
'A3.B2' => 'newB2',
|
|
'A3.*.C' => 'value',
|
|
'A4' => 'newA4',
|
|
'A4.B' => 'newB',
|
|
'A4.B.*.B1' => 'value',
|
|
];
|
|
|
|
$keys = array_keys($map);
|
|
shuffle($keys);
|
|
$randomMap = [];
|
|
|
|
foreach ($keys as $key) {
|
|
$randomMap[$key] = $map[$key];
|
|
}
|
|
|
|
try {
|
|
$object = $this->mapperBuilder->mapper()->map(
|
|
SomeRootClass::class,
|
|
new PathMapping(
|
|
[
|
|
'A1' => [
|
|
'B1' => 'foo',
|
|
],
|
|
'A2' => [
|
|
['B1' => 'bar'],
|
|
['B1' => 'buz'],
|
|
],
|
|
'A3' => [
|
|
'B1' => ['C' => 'biz'],
|
|
'B2' => ['C' => 'boz'],
|
|
],
|
|
'A4' => [
|
|
'B' => [
|
|
['B1' => 'faz'],
|
|
['B1' => 'fyz'],
|
|
],
|
|
],
|
|
],
|
|
$randomMap
|
|
)
|
|
);
|
|
} catch (MappingError $error) {
|
|
$this->mappingFail($error);
|
|
}
|
|
|
|
self::assertSame('foo', $object->newA1->value);
|
|
|
|
self::assertCount(2, $object->newA2);
|
|
self::assertSame('bar', $object->newA2[0]->value);
|
|
self::assertSame('buz', $object->newA2[1]->value);
|
|
|
|
self::assertSame('biz', $object->newA3->newB1->value);
|
|
self::assertSame('boz', $object->newA3->newB2->value);
|
|
|
|
self::assertCount(2, $object->newA4->newB);
|
|
self::assertSame('faz', $object->newA4->newB[0]->value);
|
|
self::assertSame('fyz', $object->newA4->newB[1]->value);
|
|
}
|
|
}
|
|
|
|
class SomeRootClass
|
|
{
|
|
public SomeClassWithOneProperty $newA1;
|
|
|
|
/** @var array<SomeClassWithOneProperty> */
|
|
public array $newA2;
|
|
|
|
public SomeClassWithTwoProperties $newA3;
|
|
|
|
public SomeClassWithArrayProperty $newA4;
|
|
}
|
|
|
|
class SomeClassWithOneProperty
|
|
{
|
|
public string $value;
|
|
}
|
|
|
|
class SomeClassWithTwoProperties
|
|
{
|
|
public SomeClassWithOneProperty $newB1;
|
|
public SomeClassWithOneProperty $newB2;
|
|
}
|
|
|
|
class SomeClassWithArrayProperty
|
|
{
|
|
/** @var array<SomeClassWithOneProperty> */
|
|
public array $newB;
|
|
}
|