Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/normalizer.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,11 @@ final class Page
:::note
Auto detection of the concrete type is not possible here. You have to specify the
map yourself.

The map is only used for properties. Extracting a mapped class directly, e.g.
`$hydrator->extract($contentBlock)`, returns its plain fields without the type
field, and hydrating the interface itself at the top level, e.g.
`$hydrator->hydrate(Block::class, $data)`, is not supported.
:::

## Inline
Expand Down
52 changes: 13 additions & 39 deletions src/Metadata/AttributeMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Patchlevel\Hydrator\Normalizer\ArrayNormalizer;
use Patchlevel\Hydrator\Normalizer\ArrayShapeNormalizer;
use Patchlevel\Hydrator\Normalizer\Normalizer;
use Patchlevel\Hydrator\Normalizer\ObjectMapNormalizer;
use Patchlevel\Hydrator\Normalizer\ObjectNormalizer;
use Patchlevel\Hydrator\Normalizer\TypeAwareNormalizer;
use ReflectionAttribute;
Expand All @@ -28,7 +29,6 @@
use Symfony\Component\TypeInfo\TypeResolver\TypeResolver;

use function array_key_exists;
use function array_merge;
use function array_values;

final class AttributeMetadataFactory implements MetadataFactory
Expand Down Expand Up @@ -166,38 +166,37 @@ private function hasIgnore(ReflectionProperty $reflectionProperty): bool
}

/**
* @param ClassMetadata<T> $parent
* @param ClassMetadata<T> $child
*
* @return ClassMetadata<T>
*
* @template T of object
*/
private function mergeMetadata(ClassMetadata $parent, ClassMetadata $child): ClassMetadata
private function mergeMetadata(ClassMetadata $child, ClassMetadata $parent): ClassMetadata
{
$properties = [];

foreach ($parent->properties as $property) {
foreach ($child->properties as $property) {
$properties[$property->fieldName] = $property;
}

foreach ($child->properties as $property) {
foreach ($parent->properties as $property) {
if (array_key_exists($property->fieldName, $properties)) {
throw DuplicatedFieldNameInMetadata::byInheritance(
$property->fieldName,
$parent->className,
$child->className,
$parent->className,
);
}

$properties[$property->fieldName] = $property;
}

return new ClassMetadata(
$parent->reflection,
$child->reflection,
$child->normalizer ?? $parent->normalizer,
array_values($properties),
$child->lazy ?? $parent->lazy,
array_merge($parent->extras, $child->extras),
);
}

Expand Down Expand Up @@ -225,7 +224,10 @@ private function getNormalizerOnClass(ReflectionClass $reflectionClass): Normali
$type = $reflectionClass->isEnum() ? Type::enum($className) : Type::object($className);
$normalizer = $this->inferNormalizerByType($type);

if ($normalizer instanceof ObjectNormalizer) {
// Both hand the object back to the hydrator. As class normalizer of a mapped class they
// would end in a recursion, and a map is only meant for properties typed as the
// interface or parent class that declares it.
if ($normalizer instanceof ObjectNormalizer || $normalizer instanceof ObjectMapNormalizer) {
return null;
}

Expand Down Expand Up @@ -281,21 +283,7 @@ private function inferNormalizerByType(Type $type): Normalizer|null
$normalizers = [];

foreach ($shape as $field => $fieldInfo) {
$valueType = $fieldInfo['type'];

if ($valueType instanceof NullableType) {
$valueType = $valueType->getWrappedType();
}

$normalizer = null;

if ($valueType instanceof ObjectType) {
$normalizer = $this->findNormalizerOnClass($valueType->getClassName());
}

if ($normalizer === null) {
$normalizer = $this->inferNormalizerByType($valueType);
}
$normalizer = $this->inferNormalizerByType($fieldInfo['type']);

if ($normalizer === null) {
continue;
Expand All @@ -312,21 +300,7 @@ private function inferNormalizerByType(Type $type): Normalizer|null
}

if ($type instanceof CollectionType) {
$valueType = $type->getCollectionValueType();

if ($valueType instanceof NullableType) {
$valueType = $type->getWrappedType();
}

$normalizer = null;

if ($valueType instanceof ObjectType) {
$normalizer = $this->findNormalizerOnClass($valueType->getClassName());
}

if ($normalizer === null) {
$normalizer = $this->inferNormalizerByType($valueType);
}
$normalizer = $this->inferNormalizerByType($type->getCollectionValueType());

if ($normalizer) {
return new ArrayNormalizer($normalizer);
Expand Down
2 changes: 1 addition & 1 deletion src/Normalizer/ArrayShapeNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function handleType(Type|null $type): void
$shape = $type->getShape();

foreach ($this->normalizerMap as $field => $normalizer) {
if (!$normalizer instanceof TypeAwareNormalizer) {
if (!$normalizer instanceof TypeAwareNormalizer || !array_key_exists($field, $shape)) {
continue;
}

Expand Down
23 changes: 23 additions & 0 deletions tests/Unit/Fixture/BaseId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Patchlevel\Hydrator\Tests\Unit\Fixture;

abstract class BaseId implements Id
{
final private function __construct(
private readonly string $id,
) {
}

public static function fromString(string $id): static
{
return new static($id);
}

public function toString(): string
{
return $this->id;
}
}
12 changes: 12 additions & 0 deletions tests/Unit/Fixture/BaseLazyDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Patchlevel\Hydrator\Tests\Unit\Fixture;

use Patchlevel\Hydrator\Attribute\Lazy;

#[Lazy]
abstract class BaseLazyDto
{
}
12 changes: 12 additions & 0 deletions tests/Unit/Fixture/Block.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Patchlevel\Hydrator\Tests\Unit\Fixture;

use Patchlevel\Hydrator\Normalizer\ObjectMapNormalizer;

#[ObjectMapNormalizer([ContentBlock::class => 'content', CodeBlock::class => 'code'])]
interface Block
{
}
13 changes: 13 additions & 0 deletions tests/Unit/Fixture/Circle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Patchlevel\Hydrator\Tests\Unit\Fixture;

final class Circle extends Shape
{
public function __construct(
public int $radius,
) {
}
}
13 changes: 13 additions & 0 deletions tests/Unit/Fixture/CodeBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Patchlevel\Hydrator\Tests\Unit\Fixture;

final class CodeBlock implements Block
{
public function __construct(
public string $code,
) {
}
}
13 changes: 13 additions & 0 deletions tests/Unit/Fixture/ContentBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Patchlevel\Hydrator\Tests\Unit\Fixture;

final class ContentBlock implements Block
{
public function __construct(
public string $text,
) {
}
}
22 changes: 22 additions & 0 deletions tests/Unit/Fixture/ExplicitArrayShapeNormalizerDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Patchlevel\Hydrator\Tests\Unit\Fixture;

use Patchlevel\Hydrator\Normalizer\ArrayShapeNormalizer;
use Patchlevel\Hydrator\Normalizer\EnumNormalizer;
use Patchlevel\Hydrator\Normalizer\ObjectNormalizer;

final class ExplicitArrayShapeNormalizerDto
{
/** @param array{status: Status}|null $shape */
public function __construct(
#[ArrayShapeNormalizer([
'status' => new EnumNormalizer(),
'skill' => new ObjectNormalizer(Skill::class),
])]
public array|null $shape = null,
) {
}
}
24 changes: 24 additions & 0 deletions tests/Unit/Fixture/InferNormalizerWithNullableItemsDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Patchlevel\Hydrator\Tests\Unit\Fixture;

use DateTimeImmutable;

final class InferNormalizerWithNullableItemsDto
{
/**
* @param list<?Skill> $skills
* @param array<string, ?Status> $statuses
* @param list<DateTimeImmutable|null> $dates
* @param array<string, list<?Status>>|null $nested
*/
public function __construct(
public array $skills = [],
public array $statuses = [],
public array $dates = [],
public array|null $nested = null,
) {
}
}
14 changes: 14 additions & 0 deletions tests/Unit/Fixture/InferNormalizerWithNullableShapeItemsDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Patchlevel\Hydrator\Tests\Unit\Fixture;

final class InferNormalizerWithNullableShapeItemsDto
{
/** @param array{skill: ?Skill, status: Status} $shape */
public function __construct(
public array $shape,
) {
}
}
17 changes: 17 additions & 0 deletions tests/Unit/Fixture/ObjectMapDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Patchlevel\Hydrator\Tests\Unit\Fixture;

final class ObjectMapDto
{
/** @param list<Block> $blocks */
public function __construct(
public Block $block,
public ContentBlock $contentBlock,
public array $blocks,
public Shape $shape,
) {
}
}
12 changes: 12 additions & 0 deletions tests/Unit/Fixture/Shape.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Patchlevel\Hydrator\Tests\Unit\Fixture;

use Patchlevel\Hydrator\Normalizer\ObjectMapNormalizer;

#[ObjectMapNormalizer([Circle::class => 'circle', Square::class => 'square'])]
abstract class Shape
{
}
13 changes: 13 additions & 0 deletions tests/Unit/Fixture/Square.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Patchlevel\Hydrator\Tests\Unit\Fixture;

final class Square extends Shape
{
public function __construct(
public int $size,
) {
}
}
9 changes: 9 additions & 0 deletions tests/Unit/Fixture/SubId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Patchlevel\Hydrator\Tests\Unit\Fixture;

final class SubId extends BaseId
{
}
Loading
Loading