Skip to content

Commit cdd8cb9

Browse files
authored
Merge pull request #40 from nutgram/refactor-unionresolver
Refactor unionresolver
2 parents 8eb1212 + e0e7e75 commit cdd8cb9

File tree

9 files changed

+27
-28
lines changed

9 files changed

+27
-28
lines changed

src/Annotation/Mutate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Attribute;
66
use InvalidArgumentException;
7-
use SergiX44\Hydrator\Mutator;
7+
use SergiX44\Hydrator\Mutation\Mutator;
88

99
/**
1010
* @Annotation

src/Annotation/UnionResolver.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
namespace SergiX44\Hydrator\Annotation;
44

55
use Attribute;
6+
use ReflectionException;
7+
use ReflectionNamedType;
68
use ReflectionType;
7-
use ReflectionUnionType;
89

910
/**
1011
* @Annotation
@@ -14,12 +15,13 @@
1415
abstract class UnionResolver
1516
{
1617
/**
17-
* @param ReflectionUnionType $type
18-
* @param array $data
18+
* @param string $propertyName
19+
* @param ReflectionNamedType[] $propertyTypes
20+
* @param array $data
1921
*
20-
* @throws \ReflectionException
22+
* @throws ReflectionException
2123
*
2224
* @return ReflectionType
2325
*/
24-
abstract public function resolve(ReflectionUnionType $type, array $data): ReflectionType;
26+
abstract public function resolve(string $propertyName, array $propertyTypes, array $data): ReflectionType;
2527
}

src/Hydrator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ public function hydrate(string|object $object, array|object $data): object
115115
ReflectionAttribute::IS_INSTANCEOF
116116
);
117117
if (isset($resolver)) {
118-
$propertyType = $resolver->resolve($propertyType, is_array($data[$key]) ? $data[$key] : $data);
118+
$propertyType = $resolver->resolve(
119+
$key,
120+
$propertyType->getTypes(),
121+
is_array($data[$key]) ? $data[$key] : $data
122+
);
119123
} else {
120124
throw new Exception\UnsupportedPropertyTypeException(sprintf(
121125
'The %s.%s property cannot be hydrated automatically. Please define an union type resolver attribute or remove the union type.',
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<?php
22

3-
namespace SergiX44\Hydrator\Mutations;
4-
5-
use SergiX44\Hydrator\Mutator;
3+
namespace SergiX44\Hydrator\Mutation;
64

75
class JsonDecodeArray implements Mutator
86
{
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<?php
22

3-
namespace SergiX44\Hydrator\Mutations;
4-
5-
use SergiX44\Hydrator\Mutator;
3+
namespace SergiX44\Hydrator\Mutation;
64

75
class JsonDecodeObject implements Mutator
86
{
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace SergiX44\Hydrator;
3+
namespace SergiX44\Hydrator\Mutation;
44

55
interface Mutator
66
{

src/Resolver/EnumOrScalar.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,16 @@
55
use Attribute;
66
use BackedEnum;
77
use ReflectionType;
8-
use ReflectionUnionType;
98
use SergiX44\Hydrator\Annotation\UnionResolver;
109
use SergiX44\Hydrator\Exception\UnsupportedPropertyTypeException;
1110

1211
#[Attribute(Attribute::TARGET_PROPERTY)]
1312
class EnumOrScalar extends UnionResolver
1413
{
15-
public function resolve(ReflectionUnionType $type, array $data): ReflectionType
14+
public function resolve(string $propertyName, array $propertyTypes, array $data): ReflectionType
1615
{
17-
$types = $type->getTypes();
18-
$enum = array_shift($types);
19-
20-
if (empty($types)) {
16+
$enum = array_shift($propertyTypes);
17+
if (empty($propertyTypes)) {
2118
return $enum;
2219
}
2320

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

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

48-
foreach ($types as $t) {
45+
foreach ($propertyTypes as $t) {
4946
if ($t->getName() === $valueType) {
5047
return $t;
5148
}
5249
}
5350

5451
throw new UnsupportedPropertyTypeException(
5552
sprintf(
56-
'This property can be %s or %s, %s given.',
53+
'The property "%s" can only be %s or %s, %s given.',
54+
$propertyName,
5755
$enumClass,
58-
implode(' or ', $types),
56+
implode(' or ', $propertyTypes),
5957
$valueType
6058
)
6159
);

tests/Fixtures/ObjectWithArrayToDeserialize.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace SergiX44\Hydrator\Tests\Fixtures;
44

55
use SergiX44\Hydrator\Annotation\Mutate;
6-
use SergiX44\Hydrator\Mutations\JsonDecodeObject;
6+
use SergiX44\Hydrator\Mutation\JsonDecodeObject;
77

88
final class ObjectWithArrayToDeserialize
99
{

tests/Fixtures/Resolver/TagPriceResolver.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44

55
use Attribute;
66
use ReflectionType;
7-
use ReflectionUnionType;
87
use SergiX44\Hydrator\Annotation\UnionResolver;
98

109
#[Attribute(Attribute::TARGET_PROPERTY)]
1110
class TagPriceResolver extends UnionResolver
1211
{
13-
public function resolve(ReflectionUnionType $type, array $data): ReflectionType
12+
public function resolve(string $propertyName, array $propertyTypes, array $data): ReflectionType
1413
{
15-
[$tag, $tagPrice] = $type->getTypes();
14+
[$tag, $tagPrice] = $propertyTypes;
1615

1716
if (isset($data['price'])) {
1817
return $tagPrice;

0 commit comments

Comments
 (0)