Skip to content

Commit b5b73be

Browse files
committed
null returned if the location is null in LocationCast.
1 parent 999c065 commit b5b73be

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

src/Casts/LocationCast.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313

1414
class LocationCast implements CastsAttributes, SerializesCastableAttributes
1515
{
16-
public function get($model, string $key, $value, array $attributes): Point
16+
public function get($model, string $key, $value, array $attributes): ?Point
1717
{
18+
if (is_null($value)) {
19+
return null;
20+
}
21+
1822
$coordinates = explode(',', $value);
1923

2024
if (count($coordinates) > 1) {

tests/LocationCastTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@ public function it_can_get_a_casted_attribute(): void
7373
$this->assertEquals($point->getSrid(), $address->location->getSrid());
7474
}
7575

76+
/** @test */
77+
public function it_returns_null_if_the_value_of_the_casted_column_is_null(): void
78+
{
79+
// 1. Arrange
80+
$address = new Address();
81+
82+
// 2. Act
83+
$address->save();
84+
85+
// 3. Assert
86+
$this->assertNull($address->location);
87+
}
88+
7689
/** @test */
7790
public function it_can_serialize_a_casted_attribute(): void
7891
{

tests/database/migrations/0000_00_00_000000_create_addresses_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public function up(): void
1010
{
1111
Schema::create('addresses', function (Blueprint $table) {
1212
$table->id();
13-
$table->point('location');
13+
$table->point('location')->nullable();
1414
$table->timestamps();
1515
});
1616
}

0 commit comments

Comments
 (0)