Skip to content

Commit

Permalink
refactor unionresolver
Browse files Browse the repository at this point in the history
  • Loading branch information
sergix44 committed Jan 7, 2024
1 parent 8eb1212 commit 9aa4a0d
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 32 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
13 changes: 7 additions & 6 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,12 @@
abstract class UnionResolver
{
/**
* @param ReflectionUnionType $type
* @param array $data
*
* @throws \ReflectionException
* @param string $propertyName
* @param ReflectionNamedType[] $propertyTypes
* @param array $data
*
* @return ReflectionType
* @throws ReflectionException
*/
abstract public function resolve(ReflectionUnionType $type, array $data): ReflectionType;
abstract public function resolve(string $propertyName, array $propertyTypes, array $data): ReflectionType;
}
8 changes: 5 additions & 3 deletions src/Hydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use SergiX44\Hydrator\Annotation\SkipConstructor;
use SergiX44\Hydrator\Annotation\UnionResolver;
use SergiX44\Hydrator\Exception\InvalidObjectException;

use function array_key_exists;
use function class_exists;
use function ctype_digit;
Expand All @@ -38,7 +37,6 @@
use function is_subclass_of;
use function sprintf;
use function strtotime;

use const FILTER_NULL_ON_FAILURE;
use const FILTER_VALIDATE_BOOLEAN;
use const FILTER_VALIDATE_FLOAT;
Expand Down Expand Up @@ -115,7 +113,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
19 changes: 8 additions & 11 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,20 +29,20 @@ 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;
}

$valueType = gettype($value);
$valueType = match ($valueType) {
'integer' => 'int',
'double' => 'float',
'double' => 'float',
'boolean' => 'bool',
default => $valueType,
default => $valueType,
};

foreach ($types as $t) {
foreach ($propertyTypes as $t) {
if ($t->getName() === $valueType) {
return $t;
}
Expand All @@ -55,7 +52,7 @@ public function resolve(ReflectionUnionType $type, array $data): ReflectionType
sprintf(
'This property can be %s or %s, %s given.',
$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 9aa4a0d

Please sign in to comment.