Fixed MembershipEntry Cast

This commit is contained in:
Philipp Lang 2023-02-01 23:15:46 +01:00
parent 3939920102
commit c15c210234
9 changed files with 1470 additions and 1030 deletions

View File

@ -13,7 +13,7 @@
"guzzlehttp/guzzle": "^6.3.1|^7.0",
"laravel/framework": "^9.0",
"laravel/ui": "^3.4",
"spatie/laravel-data": "^2.0",
"spatie/laravel-data": "^3.0",
"worksome/request-factories": "^2.5"
},
"autoload": {

2372
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -171,7 +171,7 @@ class Api
'/ica/rest/nami/zugeordnete-taetigkeiten/filtered-for-navigation/gruppierung-mitglied/mitglied/'.$memberId.'/flist',
'Membership fetch failed'
)
->map(fn ($membership) => new MembershipEntry($membership));
->map(fn ($membership) => MembershipEntry::from($membership));
}
public function deleteMembership(int $memberId, Membership $membership): void

View File

@ -0,0 +1,26 @@
<?php
namespace Zoomyboy\LaravelNami\Casters;
use DateTimeInterface;
use Spatie\LaravelData\Casts\Cast;
use Spatie\LaravelData\Casts\DateTimeInterfaceCast;
use Spatie\LaravelData\Casts\Uncastable;
use Spatie\LaravelData\Support\DataProperty;
class CarbonCast implements Cast
{
public function __construct(
protected array|string|null $format = null
) {
}
public function cast(DataProperty $property, mixed $value, array $context): DateTimeInterface|Uncastable|null
{
if ($property->type->isNullable && !$value) {
return null;
}
return (new DateTimeInterfaceCast($this->format, null, null, null))->cast($property, $value, $context);
}
}

View File

@ -1,14 +0,0 @@
<?php
namespace Zoomyboy\LaravelNami\Casters;
use Carbon\Carbon;
use Spatie\DataTransferObject\Caster;
class CarbonCaster implements Caster
{
public function cast(mixed $value): Carbon
{
return Carbon::parse($value);
}
}

View File

@ -1,13 +0,0 @@
<?php
namespace Zoomyboy\LaravelNami\Casters;
use Spatie\DataTransferObject\Caster;
class NullableString implements Caster
{
public function cast(mixed $value): ?string
{
return $value ?: null;
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace Zoomyboy\LaravelNami\Casters;
use Spatie\LaravelData\Casts\Cast;
use Spatie\LaravelData\Support\DataProperty;
class StringCast implements Cast
{
public function __construct()
{
}
public function cast(DataProperty $property, mixed $value, array $context): ?string
{
if ($property->type->isNullable && !$value) {
return null;
}
return $value;
}
}

View File

@ -3,32 +3,35 @@
namespace Zoomyboy\LaravelNami\Data;
use Carbon\Carbon;
use Spatie\DataTransferObject\Attributes\CastWith;
use Spatie\DataTransferObject\Attributes\MapFrom;
use Spatie\DataTransferObject\DataTransferObject;
use Zoomyboy\LaravelNami\Casters\CarbonCaster;
use Zoomyboy\LaravelNami\Casters\NullableCarbonCaster;
use Zoomyboy\LaravelNami\Casters\NullableString;
use Spatie\LaravelData\Attributes\MapInputName;
use Spatie\LaravelData\Attributes\WithCast;
use Spatie\LaravelData\Casts\DateTimeInterfaceCast;
use Spatie\LaravelData\Data;
use Zoomyboy\LaravelNami\Casters\CarbonCast;
use Zoomyboy\LaravelNami\Casters\StringCast;
class MembershipEntry extends DataTransferObject
class MembershipEntry extends Data
{
public ?int $id;
public function __construct(
public ?int $id,
#[MapFrom('entries_gruppierung')]
public string $group;
#[MapInputName('entries_gruppierung')]
public string $group,
#[MapFrom('entries_aktivVon')]
#[CastWith(CarbonCaster::class)]
public Carbon $startsAt;
#[MapInputName('entries_aktivVon')]
#[WithCast(DateTimeInterfaceCast::class, format: 'Y-m-d H:i:s')]
public Carbon $startsAt,
#[MapFrom('entries_aktivBis')]
#[CastWith(NullableCarbonCaster::class)]
public ?Carbon $endsAt;
#[MapInputName('entries_aktivBis')]
#[WithCast(CarbonCast::class, format: 'Y-m-d H:i:s')]
public ?Carbon $endsAt,
#[MapFrom('entries_taetigkeit')]
public string $activity;
#[MapInputName('entries_taetigkeit')]
public ?string $activity,
#[MapFrom('entries_untergliederung')]
#[CastWith(NullableString::class)]
public ?string $subactivity;
#[MapInputName('entries_untergliederung')]
#[WithCast(StringCast::class)]
public ?string $subactivity,
) {
}
}

View File

@ -46,6 +46,10 @@ class MembershipIndexTest extends TestCase
->fetches(6, [[
'entries_aktivBis' => '',
'entries_untergliederung' => '',
'entries_aktivVon' => '2021-02-03 00:00:00',
'entries_taetigkeit' => 'Leiter (6)',
'id' => 55,
'entries_gruppierung' => '::group::',
]]);
$membership = $this->login()->membershipsOf(6)->first();