Fixed: Transform member to json
This commit is contained in:
parent
42a303d16c
commit
6e69f985ce
|
@ -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')]
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue