From d1983dcb643e2e95267cd4bd7397b6d1bd3dedb6 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Tue, 7 Feb 2023 01:01:17 +0100 Subject: [PATCH] Cast region to null --- src/Casters/{GenderCast.php => NullValueCast.php} | 9 +++++++-- src/Data/Member.php | 9 +++++---- tests/Unit/Api/PutMemberTest.php | 3 ++- 3 files changed, 14 insertions(+), 7 deletions(-) rename src/Casters/{GenderCast.php => NullValueCast.php} (72%) diff --git a/src/Casters/GenderCast.php b/src/Casters/NullValueCast.php similarity index 72% rename from src/Casters/GenderCast.php rename to src/Casters/NullValueCast.php index 5ffeb38..fb8896f 100644 --- a/src/Casters/GenderCast.php +++ b/src/Casters/NullValueCast.php @@ -7,11 +7,16 @@ use Spatie\LaravelData\Casts\Cast; use Spatie\LaravelData\Casts\Uncastable; use Spatie\LaravelData\Support\DataProperty; -class GenderCast implements Cast +class NullValueCast implements Cast { + public function __construct( + private int $id + ) { + } + public function cast(DataProperty $property, mixed $value, array $context): DateTimeInterface|Uncastable|null|int { - if (23 === $value) { + if ($this->id === $value) { return null; } diff --git a/src/Data/Member.php b/src/Data/Member.php index 27f59da..f14316d 100644 --- a/src/Data/Member.php +++ b/src/Data/Member.php @@ -7,7 +7,7 @@ use Spatie\LaravelData\Attributes\MapInputName; use Spatie\LaravelData\Attributes\WithCast; use Spatie\LaravelData\Data; use Zoomyboy\LaravelNami\Casters\CarbonCast; -use Zoomyboy\LaravelNami\Casters\GenderCast; +use Zoomyboy\LaravelNami\Casters\NullValueCast; use Zoomyboy\LaravelNami\Casters\StringCast; use Zoomyboy\LaravelNami\Tests\Factories\MemberRequestFactory; @@ -40,7 +40,7 @@ class Member extends Data public string $email, #[MapInputName('geschlechtId')] - #[WithCast(GenderCast::class)] + #[WithCast(NullValueCast::class, id: 23)] public ?int $genderId, #[MapInputName('konfessionId')] @@ -78,7 +78,8 @@ class Member extends Data #[MapInputName('ort')] public ?string $location, - public int $regionId, + #[WithCast(NullValueCast::class, id: 23)] + public ?int $regionId, #[MapInputName('staatsangehoerigkeitId')] public int $nationalityId, @@ -132,7 +133,7 @@ class Member extends Data 'konfessionId' => $this->confessionId, 'landId' => $this->countryId, 'wiederverwendenFlag' => $this->keepdata, - 'regionId' => $this->regionId, + 'regionId' => $this->regionId ?: 23, 'staatsangehoerigkeitId' => $this->nationalityId, 'zeitschriftenversand' => $this->sendNewspaper, 'emailVertretungsberechtigter' => $this->emailParents, diff --git a/tests/Unit/Api/PutMemberTest.php b/tests/Unit/Api/PutMemberTest.php index 8ca1e25..fd7089d 100644 --- a/tests/Unit/Api/PutMemberTest.php +++ b/tests/Unit/Api/PutMemberTest.php @@ -147,12 +147,13 @@ class PutMemberTest extends TestCase 'foreign' => 'fff', 'kontoverbindung' => ['a' => 'b'], ]); - $response = $this->login()->putMember(Member::factory()->inNami(103, 16)->toMember(['genderId' => null])); + $response = $this->login()->putMember(Member::factory()->inNami(103, 16)->toMember(['genderId' => null, 'regionId' => null])); $this->assertEquals(16, $response); app(MemberFake::class)->assertUpdated(103, 16, [ 'geschlechtId' => 23, + 'regionId' => 23, ]); } }