Skip to content

Commit 2c4d862

Browse files
authored
Merge pull request #7 from tarfin-labs/nullable-cast-bugfix
Nullable cast bugfix
2 parents 999c065 + bbca525 commit 2c4d862

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

CHANGELOG.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
All notable changes to `laravel-spatial` will be documented in this file
44

5-
## 1.0.0 - 201X-XX-XX
5+
# 1.1.2 - 2022-03-08
6+
- Bug fixed for casting location columns that is null.
67

8+
# 1.1.1 - 2022-01-20
9+
- `axis-order` added to setter of `LocationCast`.
10+
- `axis-order` added to query in `HasSpatial` trait.
11+
12+
# 1.1.0 - 2022-01-20
13+
- `Point` type added to Doctrine mapped types in the service provider.
14+
- SRID support added to setter of `LocationCast`.
15+
16+
## 1.0.0 - 2022-01-18
717
- initial release

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)