Cast region to null

This commit is contained in:
philipp lang 2023-02-07 01:01:17 +01:00
parent 603b300fb0
commit d1983dcb64
3 changed files with 14 additions and 7 deletions

View File

@ -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;
}

View File

@ -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,

View File

@ -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,
]);
}
}