Skip to content

Commit

Permalink
Merge pull request #20 from nutgram/fix-instance
Browse files Browse the repository at this point in the history
  • Loading branch information
sergix44 authored May 7, 2023
2 parents 20ca4fe + 1ed3dc9 commit ab1734f
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/Annotation/Alias.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* @NamedArgumentConstructor
*
* @Attributes({
*
* @Attribute("value", type="string", required=true),
* })
*/
Expand Down
1 change: 1 addition & 0 deletions src/Annotation/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* @NamedArgumentConstructor
*
* @Attributes({
*
* @Attribute("class", type="class-string", required=true),
* })
*/
Expand Down
3 changes: 2 additions & 1 deletion src/Exception/InvalidValueException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
namespace SergiX44\Hydrator\Exception;

use ReflectionProperty;
use function sprintf;
use Throwable;

use function sprintf;

class InvalidValueException extends HydrationException
{
/**
Expand Down
49 changes: 22 additions & 27 deletions src/Hydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,11 @@

namespace SergiX44\Hydrator;

use function array_key_exists;
use BackedEnum;
use function class_exists;
use function ctype_digit;
use DateInterval;
use DateTime;
use DateTimeImmutable;
use const FILTER_NULL_ON_FAILURE;
use const FILTER_VALIDATE_BOOLEAN;
use const FILTER_VALIDATE_FLOAT;
use const FILTER_VALIDATE_INT;
use function filter_var;
use function get_object_vars;
use function implode;
use InvalidArgumentException;
use function is_array;
use function is_bool;
use function is_float;
use function is_int;
use function is_object;
use function is_string;
use function is_subclass_of;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use ReflectionAttribute;
Expand All @@ -37,9 +20,28 @@
use SergiX44\Hydrator\Annotation\ConcreteResolver;
use SergiX44\Hydrator\Annotation\UnionResolver;
use SergiX44\Hydrator\Exception\InvalidObjectException;

use function array_key_exists;
use function class_exists;
use function ctype_digit;
use function filter_var;
use function get_object_vars;
use function implode;
use function is_array;
use function is_bool;
use function is_float;
use function is_int;
use function is_object;
use function is_string;
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;
use const FILTER_VALIDATE_INT;

class Hydrator implements HydratorInterface
{
protected ?ContainerInterface $container = null;
Expand Down Expand Up @@ -70,12 +72,6 @@ public function __construct(?ContainerInterface $container = null)
*
* @return T
*
*
*
*
*
*
*
* @template T
*/
public function hydrate(string|object $object, array|object $data): object
Expand Down Expand Up @@ -157,8 +153,6 @@ public function hydrate(string|object $object, array|object $data): object
*
* @return T
*
*
*
* @template T
*/
public function hydrateWithJson(string|object $object, string $json, ?int $flags = null): object
Expand Down Expand Up @@ -202,7 +196,6 @@ public function getConcreteResolverFor(string|object $object): ?ConcreteResolver
*
* @return T
*
*
* @template T
*/
private function initializeObject(string|object $object, array|object $data): object
Expand Down Expand Up @@ -569,7 +562,9 @@ private function hydrateObjectsInArray(array $array, ArrayType $arrayType, int $
}

return array_map(function ($object) use ($arrayType) {
return $this->hydrate($arrayType->getInstance(), $object);
$newInstance = $this->container?->get($arrayType->class) ?? $arrayType->getInstance();

return $this->hydrate($newInstance, $object);
}, $array);
}

Expand Down
11 changes: 11 additions & 0 deletions tests/Fixtures/DI/Forest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace SergiX44\Hydrator\Tests\Fixtures\DI;

use SergiX44\Hydrator\Annotation\ArrayType;

class Forest
{
#[ArrayType(Tree::class)]
public array $trees;
}
64 changes: 64 additions & 0 deletions tests/HydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
use SergiX44\Hydrator\Exception\InvalidObjectException;
use SergiX44\Hydrator\Hydrator;
use SergiX44\Hydrator\HydratorInterface;
use SergiX44\Hydrator\Tests\Fixtures\DI\Forest;
use SergiX44\Hydrator\Tests\Fixtures\DI\Leaves;
use SergiX44\Hydrator\Tests\Fixtures\DI\Sun;
use SergiX44\Hydrator\Tests\Fixtures\DI\Tree;
use SergiX44\Hydrator\Tests\Fixtures\DI\Wood;
use SergiX44\Hydrator\Tests\Fixtures\ObjectWithAbstract;
use SergiX44\Hydrator\Tests\Fixtures\ObjectWithInvalidAbstract;
use SergiX44\Hydrator\Tests\Fixtures\Resolver\AppleResolver;
Expand Down Expand Up @@ -830,4 +833,65 @@ public function testHydrateWithContainer(): void
$this->assertSame($sun, $o->leaves->getSun());
$this->assertSame($sun, $o->wood->getSun());
}

public function testHydrateWithContainerWithNestedInstances(): void
{
$sun = new Sun('andromeda');

$container = new Container();
$container->delegate(new ReflectionContainer());
$container->addShared(Sun::class, $sun);

$hydrator = new Hydrator($container);

$o = $hydrator->hydrate(Forest::class, [
'trees' => [
[
'name' => 'foo',
'leaves' => [
'n' => 100,
],
'wood' => [
'kg' => 120,
],
],
[
'name' => 'foo2',
'leaves' => [
'n' => 200,
],
'wood' => [
'kg' => 220,
],
],
],
]);

$this->assertIsArray($o->trees);
$this->assertcount(2, $o->trees);

$this->assertSame('foo', $o->trees[0]->name);
$this->assertInstanceOf(Leaves::class, $o->trees[0]->leaves);
$this->assertSame(100, $o->trees[0]->leaves->n);
$this->assertInstanceOf(Sun::class, $o->trees[0]->leaves->getSun());
$this->assertSame('andromeda', $o->trees[0]->leaves->getSun()->getFrom());
$this->assertInstanceOf(Wood::class, $o->trees[0]->wood);
$this->assertSame(120, $o->trees[0]->wood->kg);
$this->assertInstanceOf(Sun::class, $o->trees[0]->wood->getSun());
$this->assertSame('andromeda', $o->trees[0]->wood->getSun()->getFrom());
$this->assertInstanceOf(Sun::class, $o->trees[0]->getSun());
$this->assertSame('andromeda', $o->trees[0]->getSun()->getFrom());

$this->assertSame('foo2', $o->trees[1]->name);
$this->assertInstanceOf(Leaves::class, $o->trees[1]->leaves);
$this->assertSame(200, $o->trees[1]->leaves->n);
$this->assertInstanceOf(Sun::class, $o->trees[1]->leaves->getSun());
$this->assertSame('andromeda', $o->trees[1]->leaves->getSun()->getFrom());
$this->assertInstanceOf(Wood::class, $o->trees[1]->wood);
$this->assertSame(220, $o->trees[1]->wood->kg);
$this->assertInstanceOf(Sun::class, $o->trees[1]->wood->getSun());
$this->assertSame('andromeda', $o->trees[1]->wood->getSun()->getFrom());
$this->assertInstanceOf(Sun::class, $o->trees[1]->getSun());
$this->assertSame('andromeda', $o->trees[1]->getSun()->getFrom());
}
}

0 comments on commit ab1734f

Please sign in to comment.