diff --git a/src/Data/Member.php b/src/Data/Member.php index 9565a2e..8776391 100644 --- a/src/Data/Member.php +++ b/src/Data/Member.php @@ -5,11 +5,13 @@ namespace Zoomyboy\LaravelNami\Data; use Carbon\Carbon; use Spatie\LaravelData\Attributes\MapInputName; use Spatie\LaravelData\Attributes\WithCast; +use Spatie\LaravelData\Attributes\WithTransformer; use Spatie\LaravelData\Data; use Zoomyboy\LaravelNami\Casters\CarbonCast; use Zoomyboy\LaravelNami\Casters\NullValueCast; use Zoomyboy\LaravelNami\Casters\StringCast; use Zoomyboy\LaravelNami\Tests\Factories\MemberRequestFactory; +use Zoomyboy\LaravelNami\Transformers\CarbonTransformer; class Member extends Data { @@ -31,10 +33,12 @@ class Member extends Data #[MapInputName('eintrittsdatum')] #[WithCast(CarbonCast::class, format: 'Y-m-d H:i:s')] + #[WithTransformer(CarbonTransformer::class, format: 'Y-m-d H:i:s')] public Carbon $joinedAt, #[MapInputName('geburtsDatum')] #[WithCast(CarbonCast::class, format: 'Y-m-d H:i:s')] + #[WithTransformer(CarbonTransformer::class, format: 'Y-m-d H:i:s')] public Carbon $birthday, public string $email, @@ -51,6 +55,7 @@ class Member extends Data #[MapInputName('lastUpdated')] #[WithCast(CarbonCast::class, format: 'Y-m-d H:i:s')] + #[WithTransformer(CarbonTransformer::class, format: 'Y-m-d H:i:s')] public ?Carbon $updatedAt, #[MapInputName('nameZusatz')] diff --git a/src/Transformers/CarbonTransformer.php b/src/Transformers/CarbonTransformer.php new file mode 100644 index 0000000..f308d1e --- /dev/null +++ b/src/Transformers/CarbonTransformer.php @@ -0,0 +1,23 @@ +<?php + +namespace Zoomyboy\LaravelNami\Transformers; + +use Spatie\LaravelData\Support\DataProperty; +use Spatie\LaravelData\Transformers\Transformer; + +class CarbonTransformer implements Transformer +{ + public function __construct( + protected array|string|null $format = null + ) { + } + + public function transform(DataProperty $property, mixed $value): mixed + { + if (is_null($value)) { + return null; + } + + return $value->format($this->format); + } +} diff --git a/tests/Unit/MemberFactoryTest.php b/tests/Unit/MemberFactoryTest.php new file mode 100644 index 0000000..5042d47 --- /dev/null +++ b/tests/Unit/MemberFactoryTest.php @@ -0,0 +1,33 @@ +<?php + +namespace Zoomyboy\LaravelNami\Tests\Unit; + +use Carbon\Carbon; +use ReflectionClass; +use Zoomyboy\LaravelNami\Data\Member; +use Zoomyboy\LaravelNami\Tests\TestCase; + +class MemberFactoryTest extends TestCase +{ + public function testItCanRenderMemberAsJson(): void + { + $member = Member::factory()->toMember(); + + $json = $member->toArray(); + $data = json_decode(json_encode($json)); + + $newMember = Member::from($data); + + foreach ((new ReflectionClass(Member::class))->getMethod('__construct')->getParameters() as $parameter) { + $name = $parameter->getName(); + + if (!is_object($member->{$name})) { + $this->assertSame($newMember->{$name}, $member->{$name}); + } + + if (is_a($member->{$name}, Carbon::class)) { + $this->assertSame($member->{$name}->toDateTimeString(), $newMember->{$name}->toDateTimeString()); + } + } + } +}