mirror of
https://github.com/danog/Valinor.git
synced 2025-01-10 06:38:24 +01:00
90dc586018
The mapper is now more type-sensitive and will fail in the following situations: - When a value does not match exactly the awaited scalar type, for instance a string `"42"` given to a node that awaits an integer. - When unnecessary array keys are present, for instance mapping an array `['foo' => …, 'bar' => …, 'baz' => …]` to an object that needs only `foo` and `bar`. - When permissive types like `mixed` or `object` are encountered. These limitations can be bypassed by enabling the flexible mode: ```php (new \CuyZ\Valinor\MapperBuilder()) ->flexible() ->mapper(); ->map('array{foo: int, bar: bool}', [ 'foo' => '42', // Will be cast from `string` to `int` 'bar' => 'true', // Will be cast from `string` to `bool` 'baz' => '…', // Will be ignored ]); ``` When using this library for a provider application — for instance an API endpoint that can be called with a JSON payload — it is recommended to use the strict mode. This ensures that the consumers of the API provide the exact awaited data structure, and prevents unknown values to be passed. When using this library as a consumer of an external source, it can make sense to enable the flexible mode. This allows for instance to convert string numeric values to integers or to ignore data that is present in the source but not needed in the application. --- All these changes led to a new check that runs on all registered object constructors. If a collision is found between several constructors that have the same signature (the same parameter names), an exception will be thrown. ```php final class SomeClass { public static function constructorA(string $foo, string $bar): self { // … } public static function constructorB(string $foo, string $bar): self { // … } } (new \CuyZ\Valinor\MapperBuilder()) ->registerConstructor( SomeClass::constructorA(...), SomeClass::constructorB(...), ) ->mapper(); ->map(SomeClass::class, [ 'foo' => 'foo', 'bar' => 'bar', ]); // Exception: A collision was detected […] ```
143 lines
4.0 KiB
PHP
143 lines
4.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace CuyZ\Valinor\Tests\Integration\Mapping\Object;
|
|
|
|
use CuyZ\Valinor\Mapper\MappingError;
|
|
use CuyZ\Valinor\MapperBuilder;
|
|
use CuyZ\Valinor\Tests\Integration\IntegrationTest;
|
|
use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\NativeUnionOfObjects;
|
|
|
|
final class UnionOfObjectsMappingTest extends IntegrationTest
|
|
{
|
|
/**
|
|
* @requires PHP >= 8
|
|
*/
|
|
public function test_object_type_is_narrowed_correctly_for_simple_case(): void
|
|
{
|
|
try {
|
|
$resultFoo = (new MapperBuilder())->mapper()->map(NativeUnionOfObjects::class, [
|
|
'foo' => 'foo',
|
|
]);
|
|
$resultBar = (new MapperBuilder())->mapper()->map(NativeUnionOfObjects::class, [
|
|
'bar' => 'bar',
|
|
]);
|
|
} catch (MappingError $error) {
|
|
$this->mappingFail($error);
|
|
}
|
|
|
|
self::assertInstanceOf(\CuyZ\Valinor\Tests\Integration\Mapping\Fixture\SomeFooObject::class, $resultFoo->object);
|
|
self::assertInstanceOf(\CuyZ\Valinor\Tests\Integration\Mapping\Fixture\SomeBarObject::class, $resultBar->object);
|
|
}
|
|
|
|
public function test_object_type_is_narrowed_correctly_for_simple_array_case(): void
|
|
{
|
|
try {
|
|
$result = (new MapperBuilder())->mapper()->map(UnionOfFooAndBar::class, [
|
|
'foo' => ['foo' => 'foo'],
|
|
'bar' => ['bar' => 'bar'],
|
|
]);
|
|
} catch (MappingError $error) {
|
|
$this->mappingFail($error);
|
|
}
|
|
|
|
self::assertInstanceOf(SomeFooObject::class, $result->objects['foo']);
|
|
self::assertInstanceOf(SomeBarObject::class, $result->objects['bar']);
|
|
}
|
|
|
|
public function test_objects_sharing_one_property_are_resolved_correctly(): void
|
|
{
|
|
try {
|
|
$result = (new MapperBuilder())->mapper()->map(UnionOfFooAndBarAndFoo::class, [
|
|
['foo' => 'foo'],
|
|
['foo' => 'foo', 'bar' => 'bar'],
|
|
]);
|
|
} catch (MappingError $error) {
|
|
$this->mappingFail($error);
|
|
}
|
|
|
|
self::assertInstanceOf(SomeFooObject::class, $result->objects[0]);
|
|
self::assertInstanceOf(SomeFooAndBarObject::class, $result->objects[1]);
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @dataProvider mapping_error_when_cannot_resolve_union_data_provider
|
|
*
|
|
* @param class-string $className
|
|
* @param mixed[] $source
|
|
*/
|
|
public function test_mapping_error_when_cannot_resolve_union(string $className, array $source): void
|
|
{
|
|
try {
|
|
(new MapperBuilder())->mapper()->map($className, $source);
|
|
|
|
self::fail('No mapping error when one was expected');
|
|
} catch (MappingError $exception) {
|
|
$error = $exception->node()->children()['objects']->children()[0]->messages()[0];
|
|
|
|
self::assertSame('1642787246', $error->code());
|
|
}
|
|
}
|
|
|
|
public function mapping_error_when_cannot_resolve_union_data_provider(): iterable
|
|
{
|
|
yield [
|
|
'className' => UnionOfFooAndBar::class,
|
|
'source' => [['foo' => 'foo', 'bar' => 'bar']],
|
|
];
|
|
yield [
|
|
'className' => UnionOfFooAndAnotherFoo::class,
|
|
'source' => [['foo' => 'foo']],
|
|
];
|
|
}
|
|
}
|
|
|
|
// @PHP8.1 Readonly properties
|
|
final class UnionOfFooAndBar
|
|
{
|
|
/** @var array<SomeFooObject|SomeBarObject> */
|
|
public array $objects;
|
|
}
|
|
|
|
// @PHP8.1 Readonly properties
|
|
final class UnionOfFooAndAnotherFoo
|
|
{
|
|
/** @var array<SomeFooObject|SomeOtherFooObject> */
|
|
public array $objects;
|
|
}
|
|
|
|
// @PHP8.1 Readonly properties
|
|
final class UnionOfFooAndBarAndFoo
|
|
{
|
|
/** @var array<SomeFooAndBarObject|SomeFooObject> */
|
|
public array $objects;
|
|
}
|
|
|
|
// @PHP8.1 Readonly properties
|
|
final class SomeFooObject
|
|
{
|
|
public string $foo;
|
|
}
|
|
|
|
// @PHP8.1 Readonly properties
|
|
final class SomeOtherFooObject
|
|
{
|
|
public string $foo;
|
|
}
|
|
|
|
// @PHP8.1 Readonly properties
|
|
final class SomeBarObject
|
|
{
|
|
public string $bar;
|
|
}
|
|
|
|
// @PHP8.1 Readonly properties
|
|
final class SomeFooAndBarObject
|
|
{
|
|
public string $foo;
|
|
|
|
public string $bar;
|
|
}
|