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
2 changes: 1 addition & 1 deletion packages/mapper/src/CasterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function forProperty(PropertyReflector $property): ?Caster
$type = $property->getType();
$castWith = $property->getAttribute(CastWith::class);

if ($castWith === null && $type->isClass()) {
if ($castWith === null && ($type->isClass() || $type->isInterface())) {
$castWith = $type->asClass()->getAttribute(CastWith::class, recursive: true);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/mapper/src/SerializerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function forProperty(PropertyReflector $property): ?Serializer
$type = $property->getType();
$serializeWith = $property->getAttribute(SerializeWith::class);

if ($serializeWith === null && $type->isClass()) {
if ($serializeWith === null && ($type->isClass() || $type->isInterface())) {
$serializeWith = $type->asClass()->getAttribute(SerializeWith::class, recursive: true);
}

Expand Down
10 changes: 10 additions & 0 deletions tests/Integration/Mapper/CasterFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Tempest\Mapper\Casters\IntegerCaster;
use Tempest\Mapper\Casters\NativeDateTimeCaster;
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
use Tests\Tempest\Integration\Mapper\Fixtures\InterfaceValueCaster;
use Tests\Tempest\Integration\Mapper\Fixtures\ObjectWithInterfaceTypedProperties;
use Tests\Tempest\Integration\Mapper\Fixtures\ObjectWithSerializerProperties;

use function Tempest\Reflection\reflect;
Expand All @@ -31,4 +33,12 @@ public function test_for_property(): void
$this->assertInstanceOf(EnumCaster::class, $factory->forProperty($class->getProperty('unitEnum')));
$this->assertInstanceOf(EnumCaster::class, $factory->forProperty($class->getProperty('backedEnum')));
}

public function test_caster_from_interface_attribute(): void
{
$factory = $this->container->get(CasterFactory::class);
$class = reflect(ObjectWithInterfaceTypedProperties::class);

$this->assertInstanceOf(InterfaceValueCaster::class, $factory->forProperty($class->getProperty('castable')));
}
}
17 changes: 17 additions & 0 deletions tests/Integration/Mapper/Fixtures/ConcreteInterfaceValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Tests\Tempest\Integration\Mapper\Fixtures;

final class ConcreteInterfaceValue implements InterfaceWithCastWith, InterfaceWithSerializeWith
{
public function __construct(
private string $value,
) {}

public function getValue(): string
{
return $this->value;
}
}
20 changes: 20 additions & 0 deletions tests/Integration/Mapper/Fixtures/InterfaceValueCaster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Tests\Tempest\Integration\Mapper\Fixtures;

use Tempest\Mapper\Caster;

final class InterfaceValueCaster implements Caster
{
public static function for(): string
{
return InterfaceWithCastWith::class;
}

public function cast(mixed $input): InterfaceWithCastWith
{
return new ConcreteInterfaceValue('casted:' . $input);
}
}
20 changes: 20 additions & 0 deletions tests/Integration/Mapper/Fixtures/InterfaceValueSerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Tests\Tempest\Integration\Mapper\Fixtures;

use Tempest\Mapper\Serializer;

final class InterfaceValueSerializer implements Serializer
{
public static function for(): string
{
return InterfaceWithSerializeWith::class;
}

public function serialize(mixed $input): string
{
return 'serialized:' . $input->getValue();
}
}
15 changes: 15 additions & 0 deletions tests/Integration/Mapper/Fixtures/InterfaceWithCastWith.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Tests\Tempest\Integration\Mapper\Fixtures;

use Tempest\Mapper\CastWith;
use Tempest\Mapper\SerializeWith;

#[CastWith(InterfaceValueCaster::class)]
#[SerializeWith(InterfaceValueSerializer::class)]
interface InterfaceWithCastWith
{
public function getValue(): string;
}
15 changes: 15 additions & 0 deletions tests/Integration/Mapper/Fixtures/InterfaceWithSerializeWith.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Tests\Tempest\Integration\Mapper\Fixtures;

use Tempest\Mapper\CastWith;
use Tempest\Mapper\SerializeWith;

#[CastWith(InterfaceValueCaster::class)]
#[SerializeWith(InterfaceValueSerializer::class)]
interface InterfaceWithSerializeWith
{
public function getValue(): string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Tests\Tempest\Integration\Mapper\Fixtures;

final class ObjectWithInterfaceTypedProperties
{
public function __construct(
public InterfaceWithCastWith $castable,
public InterfaceWithSerializeWith $serializable,
) {}
}
17 changes: 17 additions & 0 deletions tests/Integration/Mapper/MapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Tests\Tempest\Integration\Mapper\Fixtures\ObjectA;
use Tests\Tempest\Integration\Mapper\Fixtures\ObjectFactoryA;
use Tests\Tempest\Integration\Mapper\Fixtures\ObjectThatShouldUseCasters;
use Tests\Tempest\Integration\Mapper\Fixtures\ObjectWithInterfaceTypedProperties;
use Tests\Tempest\Integration\Mapper\Fixtures\ObjectWithMapFromAttribute;
use Tests\Tempest\Integration\Mapper\Fixtures\ObjectWithMapToAttribute;
use Tests\Tempest\Integration\Mapper\Fixtures\ObjectWithMapToCollisions;
Expand Down Expand Up @@ -388,4 +389,20 @@ public function test_array_of_objects_to_array(): void
actual: $array,
);
}

public function test_cast_with_and_serialize_with_from_interface(): void
{
$object = make(ObjectWithInterfaceTypedProperties::class)->from([
'castable' => 'test-value',
'serializable' => 'another-value',
]);

$this->assertSame('casted:test-value', $object->castable->getValue());
$this->assertSame('casted:another-value', $object->serializable->getValue());

$array = map($object)->toArray();

$this->assertSame('serialized:casted:test-value', $array['castable']);
$this->assertSame('serialized:casted:another-value', $array['serializable']);
}
}
10 changes: 10 additions & 0 deletions tests/Integration/Mapper/SerializerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
use Tempest\Mapper\Serializers\StringSerializer;
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
use Tests\Tempest\Integration\Mapper\Fixtures\DoubleStringSerializer;
use Tests\Tempest\Integration\Mapper\Fixtures\InterfaceValueSerializer;
use Tests\Tempest\Integration\Mapper\Fixtures\JsonSerializableObject;
use Tests\Tempest\Integration\Mapper\Fixtures\NestedObjectB;
use Tests\Tempest\Integration\Mapper\Fixtures\ObjectWithInterfaceTypedProperties;
use Tests\Tempest\Integration\Mapper\Fixtures\ObjectWithSerializerProperties;
use Tests\Tempest\Integration\Mapper\Fixtures\SerializableObject;

Expand Down Expand Up @@ -66,4 +68,12 @@ public function test_for_property(): void
$this->assertInstanceOf(NativeDateTimeSerializer::class, $factory->forProperty($class->getProperty('nativeDateTimeInterfaceProp')));
$this->assertInstanceOf(DateTimeSerializer::class, $factory->forProperty($class->getProperty('dateTimeProp')));
}

public function test_serializer_from_interface_attribute(): void
{
$factory = $this->container->get(SerializerFactory::class);
$class = reflect(ObjectWithInterfaceTypedProperties::class);

$this->assertInstanceOf(InterfaceValueSerializer::class, $factory->forProperty($class->getProperty('serializable')));
}
}