mirror of
https://github.com/danog/Valinor.git
synced 2025-01-10 14:48:20 +01:00
b2e810e3ce
Previously, the method `TreeMapper::map` would allow mapping only to an object. It is now possible to map to any type handled by the library. It is for instance possible to map to an array of objects: ```php $objects = (new \CuyZ\Valinor\MapperBuilder())->mapper()->map( 'array<' . SomeClass::class . '>', [/* … */] ); ``` For simple use-cases, an array shape can be used: ```php $array = (new \CuyZ\Valinor\MapperBuilder())->mapper()->map( 'array{foo: string, bar: int}', [/* … */] ); echo strtolower($array['foo']); echo $array['bar'] * 2; ``` This new feature changes the possible behaviour of the mapper, meaning static analysis tools need help to understand the types correctly. An extension for PHPStan and a plugin for Psalm are now provided and can be included in a project to automatically increase the type coverage.
149 lines
5.5 KiB
PHP
149 lines
5.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace CuyZ\Valinor\Tests\Integration\Mapping\Object;
|
|
|
|
use CuyZ\Valinor\Mapper\MappingError;
|
|
use CuyZ\Valinor\Mapper\Tree\Exception\CannotCastToScalarValue;
|
|
use CuyZ\Valinor\Tests\Integration\IntegrationTest;
|
|
use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\SimpleObject;
|
|
use stdClass;
|
|
use Throwable;
|
|
|
|
final class ShapedArrayValuesMappingTest extends IntegrationTest
|
|
{
|
|
public function test_values_are_mapped_properly(): void
|
|
{
|
|
$source = [
|
|
'basicShapedArrayWithExcessiveKey' => [
|
|
'foo' => 'foo',
|
|
'bar' => 42,
|
|
],
|
|
'basicShapedArrayWithStringKeys' => [
|
|
'foo' => 'fiz',
|
|
'bar' => 42,
|
|
],
|
|
'basicShapedArrayWithIntegerKeys' => [
|
|
0 => 'fiz',
|
|
1 => 42.404,
|
|
],
|
|
'shapedArrayWithObject' => [
|
|
'foo' => ['value' => 'bar'],
|
|
],
|
|
'shapedArrayWithOptionalValue' => [
|
|
'optionalString' => 'some value',
|
|
],
|
|
'shapedArrayOnSeveralLines' => [
|
|
'foo' => 'fiz',
|
|
'bar' => 42,
|
|
],
|
|
'advancedShapedArray' => [
|
|
'mandatoryString' => 'bar',
|
|
1337,
|
|
'42.404',
|
|
],
|
|
];
|
|
|
|
foreach ([ShapedArrayValues::class, ShapedArrayValuesWithConstructor::class] as $class) {
|
|
try {
|
|
$result = $this->mapperBuilder->mapper()->map($class, $source);
|
|
} catch (MappingError $error) {
|
|
$this->mappingFail($error);
|
|
}
|
|
|
|
self::assertSame(['foo' => 'foo'], $result->basicShapedArrayWithExcessiveKey);
|
|
self::assertSame($source['basicShapedArrayWithStringKeys'], $result->basicShapedArrayWithStringKeys);
|
|
self::assertSame($source['basicShapedArrayWithIntegerKeys'], $result->basicShapedArrayWithIntegerKeys);
|
|
self::assertInstanceOf(SimpleObject::class, $result->shapedArrayWithObject['foo']);
|
|
self::assertSame($source['shapedArrayWithOptionalValue'], $result->shapedArrayWithOptionalValue);
|
|
self::assertSame($source['shapedArrayOnSeveralLines'], $result->shapedArrayOnSeveralLines);
|
|
self::assertSame('bar', $result->advancedShapedArray['mandatoryString']);
|
|
self::assertSame(1337, $result->advancedShapedArray[0]);
|
|
self::assertSame(42.404, $result->advancedShapedArray[1]);
|
|
}
|
|
}
|
|
|
|
public function test_value_that_cannot_be_casted_throws_exception(): void
|
|
{
|
|
try {
|
|
$this->mapperBuilder->mapper()->map(ShapedArrayValues::class, [
|
|
'basicShapedArrayWithStringKeys' => [
|
|
'foo' => new stdClass(),
|
|
'bar' => 42,
|
|
],
|
|
]);
|
|
} catch (MappingError $exception) {
|
|
$error = $exception->node()->children()['basicShapedArrayWithStringKeys']->children()['foo']->messages()[0];
|
|
|
|
assert($error instanceof Throwable);
|
|
|
|
self::assertInstanceOf(CannotCastToScalarValue::class, $error);
|
|
self::assertSame(1618736242, $error->getCode());
|
|
self::assertSame('Cannot cast value of type `stdClass` to `string`.', $error->getMessage());
|
|
}
|
|
}
|
|
}
|
|
|
|
class ShapedArrayValues
|
|
{
|
|
/** @var array{foo: string} */
|
|
public array $basicShapedArrayWithExcessiveKey;
|
|
|
|
/** @var array{foo: string, bar: int} */
|
|
public array $basicShapedArrayWithStringKeys;
|
|
|
|
/** @var array{0: string, 1: float} */
|
|
public array $basicShapedArrayWithIntegerKeys;
|
|
|
|
/** @var array{foo: SimpleObject} */
|
|
public array $shapedArrayWithObject;
|
|
|
|
/** @var array{optionalString?: string} */
|
|
public array $shapedArrayWithOptionalValue;
|
|
|
|
/**
|
|
* @var array{
|
|
* foo: string,
|
|
* bar: int
|
|
* }
|
|
*/
|
|
public array $shapedArrayOnSeveralLines;
|
|
|
|
/** @var array{0: int, float, optionalString?: string, mandatoryString: string} */
|
|
public array $advancedShapedArray;
|
|
}
|
|
|
|
class ShapedArrayValuesWithConstructor extends ShapedArrayValues
|
|
{
|
|
/**
|
|
* @param array{foo: string} $basicShapedArrayWithExcessiveKey
|
|
* @param array{foo: string, bar: int} $basicShapedArrayWithStringKeys
|
|
* @param array{0: string, 1: float} $basicShapedArrayWithIntegerKeys
|
|
* @param array{foo: SimpleObject} $shapedArrayWithObject
|
|
* @param array{optionalString?: string} $shapedArrayWithOptionalValue
|
|
* @param array{
|
|
* foo: string,
|
|
* bar: int
|
|
* } $shapedArrayOnSeveralLines
|
|
* @param array{0: int, float, optionalString?: string, mandatoryString: string} $advancedShapedArray
|
|
*/
|
|
public function __construct(
|
|
array $basicShapedArrayWithExcessiveKey,
|
|
array $basicShapedArrayWithStringKeys,
|
|
array $basicShapedArrayWithIntegerKeys,
|
|
array $shapedArrayWithObject,
|
|
array $shapedArrayWithOptionalValue,
|
|
array $shapedArrayOnSeveralLines,
|
|
array $advancedShapedArray
|
|
) {
|
|
$this->basicShapedArrayWithExcessiveKey = $basicShapedArrayWithExcessiveKey;
|
|
$this->basicShapedArrayWithStringKeys = $basicShapedArrayWithStringKeys;
|
|
$this->basicShapedArrayWithIntegerKeys = $basicShapedArrayWithIntegerKeys;
|
|
$this->shapedArrayWithObject = $shapedArrayWithObject;
|
|
$this->shapedArrayWithOptionalValue = $shapedArrayWithOptionalValue;
|
|
$this->shapedArrayOnSeveralLines = $shapedArrayOnSeveralLines;
|
|
$this->advancedShapedArray = $advancedShapedArray;
|
|
}
|
|
}
|