Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/Annotation/OverrideConstructor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace SergiX44\Hydrator\Annotation;

use Attribute;
use Psr\Container\ContainerInterface;
use ReflectionMethod;

/**
* @Annotation
* @Target({"CLASS"})
*/
#[Attribute(Attribute::TARGET_CLASS)]
final class OverrideConstructor
{
public function __construct(public string $method)
{
}

public function getArguments(mixed $object, ContainerInterface $container): array
{
$method = new ReflectionMethod($object, $this->method);

return array_map(
static fn ($parameter) => $container->get($parameter->getType()?->getName()),
$method->getParameters()
);
}
}
10 changes: 10 additions & 0 deletions src/Hydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use SergiX44\Hydrator\Annotation\ArrayType;
use SergiX44\Hydrator\Annotation\ConcreteResolver;
use SergiX44\Hydrator\Annotation\Mutate;
use SergiX44\Hydrator\Annotation\OverrideConstructor;
use SergiX44\Hydrator\Annotation\SkipConstructor;
use SergiX44\Hydrator\Annotation\UnionResolver;
use SergiX44\Hydrator\Exception\InvalidObjectException;
Expand Down Expand Up @@ -266,6 +267,15 @@ private function initializeObject(string|object $object, array|object $data): ob
}

if ($this->container !== null) {
$overrideConstructor = $this->getAttributeInstance($reflectionClass, OverrideConstructor::class);
if ($overrideConstructor !== null) {
$obj = $reflectionClass->newInstanceWithoutConstructor();
$args = $overrideConstructor->getArguments($obj, $this->container);
call_user_func([$obj, $overrideConstructor->method], ...$args);

return $obj;
}

return $this->container->get($object);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/DI/Leaves.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Leaves
{
public int $n;

private Sun|null $sun = null;
private ?Sun $sun = null;

public function __construct(?Sun $sun = null)
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/DI/Tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Tree

public Leaves $leaves;

private Sun|null $sun = null;
private ?Sun $sun = null;

public function __construct(?Sun $sun = null)
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/DI/Wood.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Wood
{
public int $kg;

private Sun|null $sun = null;
private ?Sun $sun = null;

public function __construct(?Sun $sun = null)
{
Expand Down
13 changes: 13 additions & 0 deletions tests/Fixtures/Store/Audi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace SergiX44\Hydrator\Tests\Fixtures\Store;

use SergiX44\Hydrator\Annotation\OverrideConstructor;

#[OverrideConstructor('putKey')]
class Audi extends Car
{
public function __construct(public string $model, public int $year)
{
}
}
17 changes: 17 additions & 0 deletions tests/Fixtures/Store/Car.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace SergiX44\Hydrator\Tests\Fixtures\Store;

abstract class Car
{
public function __construct(public ?Key $key = null)
{
}

public function putKey(Key $key): static
{
$this->key = $key;

return $this;
}
}
8 changes: 8 additions & 0 deletions tests/Fixtures/Store/Key.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace SergiX44\Hydrator\Tests\Fixtures\Store;

class Key
{
public bool $on = true;
}
70 changes: 49 additions & 21 deletions tests/HydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
use SergiX44\Hydrator\Tests\Fixtures\Store\Apple;
use SergiX44\Hydrator\Tests\Fixtures\Store\AppleJack;
use SergiX44\Hydrator\Tests\Fixtures\Store\AppleSauce;
use SergiX44\Hydrator\Tests\Fixtures\Store\Audi;
use SergiX44\Hydrator\Tests\Fixtures\Store\Fruit;
use SergiX44\Hydrator\Tests\Fixtures\Store\Key;
use SergiX44\Hydrator\Tests\Fixtures\Store\Tag;
use SergiX44\Hydrator\Tests\Fixtures\Store\TagPrice;
use TypeError;
Expand Down Expand Up @@ -490,11 +492,13 @@ public function testHydrateDateIntervalPropertyWithInvalidFormat(): void

public function testHydrateArrayOfStringableEnumProperty(): void
{
$object = (new Hydrator())->hydrate(Fixtures\ObjectWithArrayOfStringableEnum::class, ['value' => [
'c1200a7e-136e-4a11-9bc3-cc937046e90f',
'a2b29b37-1c5a-4b36-9981-097ddd25c740',
'c1ea3762-9827-4c0c-808b-53be3febae6d',
]]);
$object = (new Hydrator())->hydrate(Fixtures\ObjectWithArrayOfStringableEnum::class, [
'value' => [
'c1200a7e-136e-4a11-9bc3-cc937046e90f',
'a2b29b37-1c5a-4b36-9981-097ddd25c740',
'c1ea3762-9827-4c0c-808b-53be3febae6d',
],
]);

$this->assertSame([
Fixtures\StringableEnum::foo,
Expand All @@ -505,12 +509,14 @@ public function testHydrateArrayOfStringableEnumProperty(): void

public function testHydrateArrayOfStringableEnumPropertyWithoutMatchingEnum(): void
{
$object = (new Hydrator())->hydrate(Fixtures\ObjectWithArrayOfStringableEnum::class, ['value' => [
'c1200a7e-136e-4a11-9bc3-cc937046e90f',
'a2b29b37-1c5a-4b36-9981-097ddd25c740',
'c1ea3762-9827-4c0c-808b-53be3febae6d',
'bbb',
]]);
$object = (new Hydrator())->hydrate(Fixtures\ObjectWithArrayOfStringableEnum::class, [
'value' => [
'c1200a7e-136e-4a11-9bc3-cc937046e90f',
'a2b29b37-1c5a-4b36-9981-097ddd25c740',
'c1ea3762-9827-4c0c-808b-53be3febae6d',
'bbb',
],
]);

$this->assertSame([
Fixtures\StringableEnum::foo,
Expand Down Expand Up @@ -828,11 +834,13 @@ public function testHydrateAbstractProperty(): void
public function testHydrateArrayAbstractProperty(): void
{
$o = (new Hydrator())->hydrate(new ObjectWithArrayOfAbstracts(), [
'value' => [[
'type' => 'jack',
'sweetness' => null,
'category' => 'brandy',
]],
'value' => [
[
'type' => 'jack',
'sweetness' => null,
'category' => 'brandy',
],
],
]);

$this->assertInstanceOf(ObjectWithArrayOfAbstracts::class, $o);
Expand All @@ -848,11 +856,13 @@ public function testHydrateArrayAbstractProperty(): void
public function testHydrateArrayAbstractPropertyWithObject(): void
{
$o = (new Hydrator())->hydrate(new ObjectWithArrayOfAbstracts(), [
'value' => [(object) [
'type' => 'jack',
'sweetness' => null,
'category' => 'brandy',
]],
'value' => [
(object) [
'type' => 'jack',
'sweetness' => null,
'category' => 'brandy',
],
],
]);

$this->assertInstanceOf(ObjectWithArrayOfAbstracts::class, $o);
Expand Down Expand Up @@ -1016,4 +1026,22 @@ public function testHydrateAdditionalWithMagicMethod()
$this->assertFalse($object->type);
$this->assertSame(42, $object->number);
}

public function testHydrateWithOverrideConstructor()
{
$container = new Container();
$key = new Key();
$container->bind(Key::class, function () use ($key) {
return $key;
});

$object = (new Hydrator($container))->hydrate(Audi::class, [
'model' => 'A4',
'year' => 2021,
]);

$this->assertSame('A4', $object->model);
$this->assertSame(2021, $object->year);
$this->assertSame($key, $object->key);
}
}