['value' => true], 'genericWithFloat' => ['value' => 42.404], 'genericWithInteger' => ['value' => 1337], 'genericWithIntegerValue' => ['value' => 1337], 'genericWithString' => ['value' => 'foo'], 'genericWithSingleQuoteStringValue' => ['value' => 'foo'], 'genericWithDoubleQuoteStringValue' => ['value' => 'foo'], 'genericWithArrayOfStrings' => ['value' => ['foo', 'bar', 'baz']], 'genericWithSimpleArrayOfStrings' => ['value' => ['foo', 'bar', 'baz']], 'genericWithUnionOfScalar' => ['value' => 'foo'], 'genericWithObject' => ['value' => ['value' => 'foo']], 'genericWithObjectAlias' => ['value' => ['value' => 'foo']], 'genericWithTwoTemplates' => [ 'valueA' => 'foo', 'valueB' => 42, ], 'genericWithTwoTemplatesOnSeveralLines' => [ 'valueA' => 'foo', 'valueB' => 42, ], 'genericWithSpecifiedTypeWithString' => ['value' => 'foo'], 'genericWithSpecifiedTypeWithObject' => ['value' => ['value' => 'foo']], 'genericWithSpecifiedTypeWithIntegerValue' => ['value' => 42], 'genericWithSpecifiedTypeWithStringValue' => ['value' => 'foo'], ]; foreach ([GenericValues::class, GenericValuesWithConstructor::class] as $class) { try { $result = $this->mapperBuilder->mapper()->map($class, $source); } catch (MappingError $error) { $this->mappingFail($error); } self::assertSame(true, $result->genericWithBoolean->value); self::assertSame(42.404, $result->genericWithFloat->value); self::assertSame(1337, $result->genericWithInteger->value); self::assertSame(1337, $result->genericWithIntegerValue->value); // @phpstan-ignore-line self::assertSame('foo', $result->genericWithString->value); self::assertSame('foo', $result->genericWithSingleQuoteStringValue->value); // @phpstan-ignore-line self::assertSame('foo', $result->genericWithDoubleQuoteStringValue->value); // @phpstan-ignore-line self::assertSame(['foo', 'bar', 'baz'], $result->genericWithArrayOfStrings->value); self::assertSame(['foo', 'bar', 'baz'], $result->genericWithSimpleArrayOfStrings->value); self::assertSame('foo', $result->genericWithUnionOfScalar->value); self::assertSame('foo', $result->genericWithObject->value->value); self::assertSame('foo', $result->genericWithObjectAlias->value->value); self::assertSame('foo', $result->genericWithTwoTemplates->valueA); self::assertSame(42, $result->genericWithTwoTemplates->valueB); self::assertSame('foo', $result->genericWithTwoTemplatesOnSeveralLines->valueA); self::assertSame(42, $result->genericWithTwoTemplatesOnSeveralLines->valueB); self::assertSame('foo', $result->genericWithSpecifiedTypeWithString->value); self::assertSame('foo', $result->genericWithSpecifiedTypeWithObject->value->value); self::assertSame(42, $result->genericWithSpecifiedTypeWithIntegerValue->value); // @phpstan-ignore-line self::assertSame('foo', $result->genericWithSpecifiedTypeWithStringValue->value); // @phpstan-ignore-line } } } /** * @template T */ final class GenericObjectWithOneTemplate { /** @var T */ public $value; } /** * @template TemplateA * @template TemplateB */ final class GenericObjectWithTwoTemplates { /** @var TemplateA */ public $valueA; /** @var TemplateB */ public $valueB; } /** * @template T of string|object|42|'foo' */ final class GenericObjectWithSpecifiedType { /** @var T */ public $value; } class GenericValues { /** @var GenericObjectWithOneTemplate */ public GenericObjectWithOneTemplate $genericWithBoolean; /** @var GenericObjectWithOneTemplate */ public GenericObjectWithOneTemplate $genericWithFloat; /** @var GenericObjectWithOneTemplate */ public GenericObjectWithOneTemplate $genericWithInteger; /** @var GenericObjectWithOneTemplate<1337> */ public GenericObjectWithOneTemplate $genericWithIntegerValue; /** @var GenericObjectWithOneTemplate */ public GenericObjectWithOneTemplate $genericWithString; /** @var GenericObjectWithOneTemplate<'foo'> */ public GenericObjectWithOneTemplate $genericWithSingleQuoteStringValue; /** @var GenericObjectWithOneTemplate<"foo"> */ public GenericObjectWithOneTemplate $genericWithDoubleQuoteStringValue; /** @var GenericObjectWithOneTemplate> */ public GenericObjectWithOneTemplate $genericWithArrayOfStrings; /** @var GenericObjectWithOneTemplate */ public GenericObjectWithOneTemplate $genericWithSimpleArrayOfStrings; /** @var GenericObjectWithOneTemplate */ public GenericObjectWithOneTemplate $genericWithUnionOfScalar; /** @var GenericObjectWithOneTemplate */ public GenericObjectWithOneTemplate $genericWithObject; /** @var GenericObjectWithOneTemplate */ public GenericObjectWithOneTemplate $genericWithObjectAlias; /** @var GenericObjectWithTwoTemplates */ public GenericObjectWithTwoTemplates $genericWithTwoTemplates; /** * @var GenericObjectWithTwoTemplates< * string, * int * > */ public GenericObjectWithTwoTemplates $genericWithTwoTemplatesOnSeveralLines; /** @var GenericObjectWithSpecifiedType */ public GenericObjectWithSpecifiedType $genericWithSpecifiedTypeWithString; /** @var GenericObjectWithSpecifiedType */ public GenericObjectWithSpecifiedType $genericWithSpecifiedTypeWithObject; /** @var GenericObjectWithSpecifiedType<42> */ public GenericObjectWithSpecifiedType $genericWithSpecifiedTypeWithIntegerValue; /** @var GenericObjectWithSpecifiedType<'foo'> */ public GenericObjectWithSpecifiedType $genericWithSpecifiedTypeWithStringValue; } class GenericValuesWithConstructor extends GenericValues { /** * @param GenericObjectWithOneTemplate $genericWithBoolean * @param GenericObjectWithOneTemplate $genericWithFloat * @param GenericObjectWithOneTemplate $genericWithInteger * @param GenericObjectWithOneTemplate<1337> $genericWithIntegerValue * @param GenericObjectWithOneTemplate $genericWithString * @param GenericObjectWithOneTemplate<'foo'> $genericWithSingleQuoteStringValue * @param GenericObjectWithOneTemplate<"foo"> $genericWithDoubleQuoteStringValue * @param GenericObjectWithOneTemplate> $genericWithArrayOfStrings * @param GenericObjectWithOneTemplate $genericWithSimpleArrayOfStrings * @param GenericObjectWithOneTemplate $genericWithUnionOfScalar * @param GenericObjectWithOneTemplate $genericWithObject * @param GenericObjectWithOneTemplate $genericWithObjectAlias * @param GenericObjectWithTwoTemplates $genericWithTwoTemplates * @param GenericObjectWithTwoTemplates< * string, * int * > $genericWithTwoTemplatesOnSeveralLines * @param GenericObjectWithSpecifiedType $genericWithSpecifiedTypeWithString * @param GenericObjectWithSpecifiedType $genericWithSpecifiedTypeWithObject * @param GenericObjectWithSpecifiedType<42> $genericWithSpecifiedTypeWithIntegerValue * @param GenericObjectWithSpecifiedType<'foo'> $genericWithSpecifiedTypeWithStringValue */ public function __construct( GenericObjectWithOneTemplate $genericWithBoolean, GenericObjectWithOneTemplate $genericWithFloat, GenericObjectWithOneTemplate $genericWithInteger, GenericObjectWithOneTemplate $genericWithIntegerValue, GenericObjectWithOneTemplate $genericWithString, GenericObjectWithOneTemplate $genericWithSingleQuoteStringValue, GenericObjectWithOneTemplate $genericWithDoubleQuoteStringValue, GenericObjectWithOneTemplate $genericWithArrayOfStrings, GenericObjectWithOneTemplate $genericWithSimpleArrayOfStrings, GenericObjectWithOneTemplate $genericWithUnionOfScalar, GenericObjectWithOneTemplate $genericWithObject, GenericObjectWithOneTemplate $genericWithObjectAlias, GenericObjectWithTwoTemplates $genericWithTwoTemplates, GenericObjectWithTwoTemplates $genericWithTwoTemplatesOnSeveralLines, GenericObjectWithSpecifiedType $genericWithSpecifiedTypeWithString, GenericObjectWithSpecifiedType $genericWithSpecifiedTypeWithObject, GenericObjectWithSpecifiedType $genericWithSpecifiedTypeWithIntegerValue, GenericObjectWithSpecifiedType $genericWithSpecifiedTypeWithStringValue ) { $this->genericWithBoolean = $genericWithBoolean; $this->genericWithFloat = $genericWithFloat; $this->genericWithInteger = $genericWithInteger; $this->genericWithIntegerValue = $genericWithIntegerValue; $this->genericWithString = $genericWithString; $this->genericWithSingleQuoteStringValue = $genericWithSingleQuoteStringValue; $this->genericWithDoubleQuoteStringValue = $genericWithDoubleQuoteStringValue; $this->genericWithArrayOfStrings = $genericWithArrayOfStrings; $this->genericWithSimpleArrayOfStrings = $genericWithSimpleArrayOfStrings; $this->genericWithUnionOfScalar = $genericWithUnionOfScalar; $this->genericWithObject = $genericWithObject; $this->genericWithObjectAlias = $genericWithObjectAlias; $this->genericWithTwoTemplates = $genericWithTwoTemplates; $this->genericWithTwoTemplatesOnSeveralLines = $genericWithTwoTemplatesOnSeveralLines; $this->genericWithSpecifiedTypeWithString = $genericWithSpecifiedTypeWithString; $this->genericWithSpecifiedTypeWithObject = $genericWithSpecifiedTypeWithObject; $this->genericWithSpecifiedTypeWithIntegerValue = $genericWithSpecifiedTypeWithIntegerValue; $this->genericWithSpecifiedTypeWithStringValue = $genericWithSpecifiedTypeWithStringValue; } }