Skip to content

Commit

Permalink
Merge pull request #40 from nutgram/refactor-unionresolver
Browse files Browse the repository at this point in the history
Refactor unionresolver
  • Loading branch information
sergix44 authored Jan 7, 2024
2 parents 8eb1212 + e0e7e75 commit cdd8cb9
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/Annotation/Mutate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Attribute;
use InvalidArgumentException;
use SergiX44\Hydrator\Mutator;
use SergiX44\Hydrator\Mutation\Mutator;

/**
* @Annotation
Expand Down
12 changes: 7 additions & 5 deletions src/Annotation/UnionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace SergiX44\Hydrator\Annotation;

use Attribute;
use ReflectionException;
use ReflectionNamedType;
use ReflectionType;
use ReflectionUnionType;

/**
* @Annotation
Expand All @@ -14,12 +15,13 @@
abstract class UnionResolver
{
/**
* @param ReflectionUnionType $type
* @param array $data
* @param string $propertyName
* @param ReflectionNamedType[] $propertyTypes
* @param array $data
*
* @throws \ReflectionException
* @throws ReflectionException
*
* @return ReflectionType
*/
abstract public function resolve(ReflectionUnionType $type, array $data): ReflectionType;
abstract public function resolve(string $propertyName, array $propertyTypes, array $data): ReflectionType;
}
6 changes: 5 additions & 1 deletion src/Hydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ public function hydrate(string|object $object, array|object $data): object
ReflectionAttribute::IS_INSTANCEOF
);
if (isset($resolver)) {
$propertyType = $resolver->resolve($propertyType, is_array($data[$key]) ? $data[$key] : $data);
$propertyType = $resolver->resolve(
$key,
$propertyType->getTypes(),
is_array($data[$key]) ? $data[$key] : $data
);
} else {
throw new Exception\UnsupportedPropertyTypeException(sprintf(
'The %s.%s property cannot be hydrated automatically. Please define an union type resolver attribute or remove the union type.',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php

namespace SergiX44\Hydrator\Mutations;

use SergiX44\Hydrator\Mutator;
namespace SergiX44\Hydrator\Mutation;

class JsonDecodeArray implements Mutator
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php

namespace SergiX44\Hydrator\Mutations;

use SergiX44\Hydrator\Mutator;
namespace SergiX44\Hydrator\Mutation;

class JsonDecodeObject implements Mutator
{
Expand Down
2 changes: 1 addition & 1 deletion src/Mutator.php → src/Mutation/Mutator.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace SergiX44\Hydrator;
namespace SergiX44\Hydrator\Mutation;

interface Mutator
{
Expand Down
18 changes: 8 additions & 10 deletions src/Resolver/EnumOrScalar.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@
use Attribute;
use BackedEnum;
use ReflectionType;
use ReflectionUnionType;
use SergiX44\Hydrator\Annotation\UnionResolver;
use SergiX44\Hydrator\Exception\UnsupportedPropertyTypeException;

#[Attribute(Attribute::TARGET_PROPERTY)]
class EnumOrScalar extends UnionResolver
{
public function resolve(ReflectionUnionType $type, array $data): ReflectionType
public function resolve(string $propertyName, array $propertyTypes, array $data): ReflectionType
{
$types = $type->getTypes();
$enum = array_shift($types);

if (empty($types)) {
$enum = array_shift($propertyTypes);
if (empty($propertyTypes)) {
return $enum;
}

Expand All @@ -32,7 +29,7 @@ public function resolve(ReflectionUnionType $type, array $data): ReflectionType
);
}

$value = array_shift($data);
$value = $data[$propertyName] ?? array_shift($data);
if ((is_string($value) || is_int($value)) && $enumClass::tryFrom($value) !== null) {
return $enum;
}
Expand All @@ -45,17 +42,18 @@ public function resolve(ReflectionUnionType $type, array $data): ReflectionType
default => $valueType,
};

foreach ($types as $t) {
foreach ($propertyTypes as $t) {
if ($t->getName() === $valueType) {
return $t;
}
}

throw new UnsupportedPropertyTypeException(
sprintf(
'This property can be %s or %s, %s given.',
'The property "%s" can only be %s or %s, %s given.',
$propertyName,
$enumClass,
implode(' or ', $types),
implode(' or ', $propertyTypes),
$valueType
)
);
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/ObjectWithArrayToDeserialize.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace SergiX44\Hydrator\Tests\Fixtures;

use SergiX44\Hydrator\Annotation\Mutate;
use SergiX44\Hydrator\Mutations\JsonDecodeObject;
use SergiX44\Hydrator\Mutation\JsonDecodeObject;

final class ObjectWithArrayToDeserialize
{
Expand Down
5 changes: 2 additions & 3 deletions tests/Fixtures/Resolver/TagPriceResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@

use Attribute;
use ReflectionType;
use ReflectionUnionType;
use SergiX44\Hydrator\Annotation\UnionResolver;

#[Attribute(Attribute::TARGET_PROPERTY)]
class TagPriceResolver extends UnionResolver
{
public function resolve(ReflectionUnionType $type, array $data): ReflectionType
public function resolve(string $propertyName, array $propertyTypes, array $data): ReflectionType
{
[$tag, $tagPrice] = $type->getTypes();
[$tag, $tagPrice] = $propertyTypes;

if (isset($data['price'])) {
return $tagPrice;
Expand Down

0 comments on commit cdd8cb9

Please sign in to comment.