Skip to content

Commit ab1734f

Browse files
authored
Merge pull request #20 from nutgram/fix-instance
2 parents 20ca4fe + 1ed3dc9 commit ab1734f

File tree

6 files changed

+101
-28
lines changed

6 files changed

+101
-28
lines changed

src/Annotation/Alias.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* @NamedArgumentConstructor
1313
*
1414
* @Attributes({
15+
*
1516
* @Attribute("value", type="string", required=true),
1617
* })
1718
*/

src/Annotation/ArrayType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* @NamedArgumentConstructor
1414
*
1515
* @Attributes({
16+
*
1617
* @Attribute("class", type="class-string", required=true),
1718
* })
1819
*/

src/Exception/InvalidValueException.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
namespace SergiX44\Hydrator\Exception;
44

55
use ReflectionProperty;
6-
use function sprintf;
76
use Throwable;
87

8+
use function sprintf;
9+
910
class InvalidValueException extends HydrationException
1011
{
1112
/**

src/Hydrator.php

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,11 @@
22

33
namespace SergiX44\Hydrator;
44

5-
use function array_key_exists;
65
use BackedEnum;
7-
use function class_exists;
8-
use function ctype_digit;
96
use DateInterval;
107
use DateTime;
118
use DateTimeImmutable;
12-
use const FILTER_NULL_ON_FAILURE;
13-
use const FILTER_VALIDATE_BOOLEAN;
14-
use const FILTER_VALIDATE_FLOAT;
15-
use const FILTER_VALIDATE_INT;
16-
use function filter_var;
17-
use function get_object_vars;
18-
use function implode;
199
use InvalidArgumentException;
20-
use function is_array;
21-
use function is_bool;
22-
use function is_float;
23-
use function is_int;
24-
use function is_object;
25-
use function is_string;
26-
use function is_subclass_of;
2710
use Psr\Container\ContainerExceptionInterface;
2811
use Psr\Container\ContainerInterface;
2912
use ReflectionAttribute;
@@ -37,9 +20,28 @@
3720
use SergiX44\Hydrator\Annotation\ConcreteResolver;
3821
use SergiX44\Hydrator\Annotation\UnionResolver;
3922
use SergiX44\Hydrator\Exception\InvalidObjectException;
23+
24+
use function array_key_exists;
25+
use function class_exists;
26+
use function ctype_digit;
27+
use function filter_var;
28+
use function get_object_vars;
29+
use function implode;
30+
use function is_array;
31+
use function is_bool;
32+
use function is_float;
33+
use function is_int;
34+
use function is_object;
35+
use function is_string;
36+
use function is_subclass_of;
4037
use function sprintf;
4138
use function strtotime;
4239

40+
use const FILTER_NULL_ON_FAILURE;
41+
use const FILTER_VALIDATE_BOOLEAN;
42+
use const FILTER_VALIDATE_FLOAT;
43+
use const FILTER_VALIDATE_INT;
44+
4345
class Hydrator implements HydratorInterface
4446
{
4547
protected ?ContainerInterface $container = null;
@@ -70,12 +72,6 @@ public function __construct(?ContainerInterface $container = null)
7072
*
7173
* @return T
7274
*
73-
*
74-
*
75-
*
76-
*
77-
*
78-
*
7975
* @template T
8076
*/
8177
public function hydrate(string|object $object, array|object $data): object
@@ -157,8 +153,6 @@ public function hydrate(string|object $object, array|object $data): object
157153
*
158154
* @return T
159155
*
160-
*
161-
*
162156
* @template T
163157
*/
164158
public function hydrateWithJson(string|object $object, string $json, ?int $flags = null): object
@@ -202,7 +196,6 @@ public function getConcreteResolverFor(string|object $object): ?ConcreteResolver
202196
*
203197
* @return T
204198
*
205-
*
206199
* @template T
207200
*/
208201
private function initializeObject(string|object $object, array|object $data): object
@@ -569,7 +562,9 @@ private function hydrateObjectsInArray(array $array, ArrayType $arrayType, int $
569562
}
570563

571564
return array_map(function ($object) use ($arrayType) {
572-
return $this->hydrate($arrayType->getInstance(), $object);
565+
$newInstance = $this->container?->get($arrayType->class) ?? $arrayType->getInstance();
566+
567+
return $this->hydrate($newInstance, $object);
573568
}, $array);
574569
}
575570

tests/Fixtures/DI/Forest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace SergiX44\Hydrator\Tests\Fixtures\DI;
4+
5+
use SergiX44\Hydrator\Annotation\ArrayType;
6+
7+
class Forest
8+
{
9+
#[ArrayType(Tree::class)]
10+
public array $trees;
11+
}

tests/HydratorTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
use SergiX44\Hydrator\Exception\InvalidObjectException;
1111
use SergiX44\Hydrator\Hydrator;
1212
use SergiX44\Hydrator\HydratorInterface;
13+
use SergiX44\Hydrator\Tests\Fixtures\DI\Forest;
14+
use SergiX44\Hydrator\Tests\Fixtures\DI\Leaves;
1315
use SergiX44\Hydrator\Tests\Fixtures\DI\Sun;
1416
use SergiX44\Hydrator\Tests\Fixtures\DI\Tree;
17+
use SergiX44\Hydrator\Tests\Fixtures\DI\Wood;
1518
use SergiX44\Hydrator\Tests\Fixtures\ObjectWithAbstract;
1619
use SergiX44\Hydrator\Tests\Fixtures\ObjectWithInvalidAbstract;
1720
use SergiX44\Hydrator\Tests\Fixtures\Resolver\AppleResolver;
@@ -830,4 +833,65 @@ public function testHydrateWithContainer(): void
830833
$this->assertSame($sun, $o->leaves->getSun());
831834
$this->assertSame($sun, $o->wood->getSun());
832835
}
836+
837+
public function testHydrateWithContainerWithNestedInstances(): void
838+
{
839+
$sun = new Sun('andromeda');
840+
841+
$container = new Container();
842+
$container->delegate(new ReflectionContainer());
843+
$container->addShared(Sun::class, $sun);
844+
845+
$hydrator = new Hydrator($container);
846+
847+
$o = $hydrator->hydrate(Forest::class, [
848+
'trees' => [
849+
[
850+
'name' => 'foo',
851+
'leaves' => [
852+
'n' => 100,
853+
],
854+
'wood' => [
855+
'kg' => 120,
856+
],
857+
],
858+
[
859+
'name' => 'foo2',
860+
'leaves' => [
861+
'n' => 200,
862+
],
863+
'wood' => [
864+
'kg' => 220,
865+
],
866+
],
867+
],
868+
]);
869+
870+
$this->assertIsArray($o->trees);
871+
$this->assertcount(2, $o->trees);
872+
873+
$this->assertSame('foo', $o->trees[0]->name);
874+
$this->assertInstanceOf(Leaves::class, $o->trees[0]->leaves);
875+
$this->assertSame(100, $o->trees[0]->leaves->n);
876+
$this->assertInstanceOf(Sun::class, $o->trees[0]->leaves->getSun());
877+
$this->assertSame('andromeda', $o->trees[0]->leaves->getSun()->getFrom());
878+
$this->assertInstanceOf(Wood::class, $o->trees[0]->wood);
879+
$this->assertSame(120, $o->trees[0]->wood->kg);
880+
$this->assertInstanceOf(Sun::class, $o->trees[0]->wood->getSun());
881+
$this->assertSame('andromeda', $o->trees[0]->wood->getSun()->getFrom());
882+
$this->assertInstanceOf(Sun::class, $o->trees[0]->getSun());
883+
$this->assertSame('andromeda', $o->trees[0]->getSun()->getFrom());
884+
885+
$this->assertSame('foo2', $o->trees[1]->name);
886+
$this->assertInstanceOf(Leaves::class, $o->trees[1]->leaves);
887+
$this->assertSame(200, $o->trees[1]->leaves->n);
888+
$this->assertInstanceOf(Sun::class, $o->trees[1]->leaves->getSun());
889+
$this->assertSame('andromeda', $o->trees[1]->leaves->getSun()->getFrom());
890+
$this->assertInstanceOf(Wood::class, $o->trees[1]->wood);
891+
$this->assertSame(220, $o->trees[1]->wood->kg);
892+
$this->assertInstanceOf(Sun::class, $o->trees[1]->wood->getSun());
893+
$this->assertSame('andromeda', $o->trees[1]->wood->getSun()->getFrom());
894+
$this->assertInstanceOf(Sun::class, $o->trees[1]->getSun());
895+
$this->assertSame('andromeda', $o->trees[1]->getSun()->getFrom());
896+
}
833897
}

0 commit comments

Comments
 (0)