diff --git a/src/Api.php b/src/Api.php index 9e457c3..d81bbc8 100644 --- a/src/Api.php +++ b/src/Api.php @@ -207,7 +207,7 @@ class Api "Fails fetching membership {$membershipId} for {$memberId}", ); - return $membership ? new Membership($membership) : null; + return $membership ? Membership::from($membership) : null; } public function courses(): Collection diff --git a/src/Data/Membership.php b/src/Data/Membership.php index 31e4c3d..0137693 100644 --- a/src/Data/Membership.php +++ b/src/Data/Membership.php @@ -3,35 +3,37 @@ 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 Spatie\LaravelData\Attributes\MapInputName; +use Spatie\LaravelData\Attributes\WithCast; +use Spatie\LaravelData\Data; +use Zoomyboy\LaravelNami\Casters\CarbonCast; -class Membership extends DataTransferObject +class Membership extends Data { - public ?int $id; + public function __construct( + public ?int $id, - #[MapFrom('gruppierungId')] - public int $groupId; + #[MapInputName('gruppierungId')] + public int $groupId, - #[MapFrom('aktivVon')] - #[CastWith(CarbonCaster::class)] - public Carbon $startsAt; + #[MapInputName('aktivVon')] + #[WithCast(CarbonCast::class, format: 'Y-m-d H:i:s')] + public Carbon $startsAt, - #[MapFrom('aktivBis')] - #[CastWith(NullableCarbonCaster::class)] - public ?Carbon $endsAt; + #[MapInputName('aktivBis')] + #[WithCast(CarbonCast::class, format: 'Y-m-d H:i:s')] + public ?Carbon $endsAt, - #[MapFrom('taetigkeitId')] - public int $activityId; + #[MapInputName('taetigkeitId')] + public int $activityId, - #[MapFrom('untergliederungId')] - public ?int $subactivityId; + #[MapInputName('untergliederungId')] + public ?int $subactivityId, - #[MapFrom('gruppierung')] - public ?string $group; + #[MapInputName('gruppierung')] + public ?string $group, + ) { + } public function toNami(): array { @@ -44,16 +46,4 @@ class Membership extends DataTransferObject 'untergliederungId' => $this->subactivityId, ]; } - - public static function fromArray(array $data): self - { - return new self([ - 'gruppierungId' => data_get($data, 'groupId'), - 'id' => data_get($data, 'id'), - 'aktivVon' => data_get($data, 'startsAt'), - 'aktivBis' => data_get($data, 'endsAt'), - 'taetigkeitId' => data_get($data, 'activityId'), - 'untergliederungId' => data_get($data, 'subactivityId'), - ]); - } } diff --git a/src/Data/MembershipEntry.php b/src/Data/MembershipEntry.php index 247d421..f37736a 100644 --- a/src/Data/MembershipEntry.php +++ b/src/Data/MembershipEntry.php @@ -5,7 +5,6 @@ namespace Zoomyboy\LaravelNami\Data; use Carbon\Carbon; 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; @@ -19,7 +18,7 @@ class MembershipEntry extends Data public string $group, #[MapInputName('entries_aktivVon')] - #[WithCast(DateTimeInterfaceCast::class, format: 'Y-m-d H:i:s')] + #[WithCast(CarbonCast::class, format: 'Y-m-d H:i:s')] public Carbon $startsAt, #[MapInputName('entries_aktivBis')] diff --git a/tests/Unit/Api/MembershipTest.php b/tests/Unit/Api/MembershipTest.php index ad5c449..34812f6 100644 --- a/tests/Unit/Api/MembershipTest.php +++ b/tests/Unit/Api/MembershipTest.php @@ -78,7 +78,7 @@ class MembershipTest extends TestCase Carbon::setTestNow(Carbon::parse('2022-02-03 03:00:00')); app(MembershipFake::class)->createsSuccessfully(6, 133); - $membershipId = $this->login()->putMembership(6, Membership::fromArray([ + $membershipId = $this->login()->putMembership(6, Membership::from([ 'id' => null, 'subactivityId' => 3, 'activityId' => 2, @@ -102,7 +102,7 @@ class MembershipTest extends TestCase Carbon::setTestNow(Carbon::parse('2022-02-03 03:00:00')); app(MembershipFake::class)->destroysSuccessfully(6, 133); - $this->login()->deleteMembership(6, Membership::fromArray([ + $this->login()->deleteMembership(6, Membership::from([ 'id' => 133, 'subactivityId' => 3, 'activityId' => 2, @@ -120,7 +120,7 @@ class MembershipTest extends TestCase app(MembershipFake::class)->failsDeleting(6, 133); app(MembershipFake::class)->updatesSuccessfully(6, 133); - $this->login()->deleteMembership(6, Membership::fromArray([ + $this->login()->deleteMembership(6, Membership::from([ 'id' => 133, 'subactivityId' => 3, 'activityId' => 2, @@ -142,7 +142,7 @@ class MembershipTest extends TestCase app(MembershipFake::class)->failsDeleting(6, null); app(MembershipFake::class)->updatesSuccessfully(6, null); - $this->login()->deleteMembership(6, Membership::fromArray([ + $this->login()->deleteMembership(6, Membership::from([ 'id' => null, 'subactivityId' => 3, 'activityId' => 2,