Skip to content

Commit 8151f3b

Browse files
authored
Merge pull request #41 from tarfin-labs/handling-expression-in-location-cast
Handling expression in location cast
2 parents 25c513e + 34175a5 commit 8151f3b

File tree

5 files changed

+63
-22
lines changed

5 files changed

+63
-22
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

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

5+
## 3.0.1 - 2025-05-14
6+
- Expression support added to `get()` method in `LocationCast`.
7+
- Replaced `@test` annotations with `#[Test]` attributes across the test suite for modern PHPUnit syntax.
8+
59
## 3.0.0 - 2025-02-20
610
- Laravel 12 and PHP 8.4 support added.
711
- Laravel 10 and below versions are not supported anymore.

src/Casts/LocationCast.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function get($model, string $key, $value, array $attributes): ?Point
1919
return null;
2020
}
2121

22-
$coordinates = explode(',', $value);
22+
$coordinates = $this->getCoordinates($model, $value);
2323

2424
if (count($coordinates) > 1) {
2525
$location = explode(',', str_replace(['POINT(', ')', ' '], ['', '', ','], $coordinates[0]));
@@ -51,4 +51,22 @@ public function serialize($model, string $key, $value, array $attributes): array
5151
{
5252
return $value->toArray();
5353
}
54+
55+
private function getCoordinates($model, $value): array
56+
{
57+
if ($value instanceof Expression) {
58+
preg_match(
59+
pattern: "/ST_GeomFromText\(\s*'([^']+)'\s*(?:,\s*(\d+))?\s*(?:,\s*'([^']+)')?\s*\)/",
60+
subject: (string) $value->getValue($model->getConnection()->getQueryGrammar()),
61+
matches: $matches,
62+
);
63+
64+
return [
65+
$matches[1],
66+
(int) ($matches[2] ?? 0),
67+
];
68+
}
69+
70+
return explode(',', $value);
71+
}
5472
}

tests/HasSpatialTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
namespace TarfinLabs\LaravelSpatial\Tests;
44

5+
use PHPUnit\Framework\Attributes\Test;
56
use TarfinLabs\LaravelSpatial\Tests\TestModels\Address;
67
use TarfinLabs\LaravelSpatial\Types\Point;
78

89
class HasSpatialTest extends TestCase
910
{
10-
/** @test */
11+
#[Test]
1112
public function it_generates_sql_query_for_selectDistanceTo_scope(): void
1213
{
1314
// Arrange
@@ -24,7 +25,7 @@ public function it_generates_sql_query_for_selectDistanceTo_scope(): void
2425
);
2526
}
2627

27-
/** @test */
28+
#[Test]
2829
public function it_generates_sql_query_for_withinDistanceTo_scope(): void
2930
{
3031
// 1. Arrange
@@ -41,7 +42,7 @@ public function it_generates_sql_query_for_withinDistanceTo_scope(): void
4142
);
4243
}
4344

44-
/** @test */
45+
#[Test]
4546
public function it_generates_sql_query_for_orderByDistanceTo_scope(): void
4647
{
4748
// 1. Arrange
@@ -64,7 +65,7 @@ public function it_generates_sql_query_for_orderByDistanceTo_scope(): void
6465
);
6566
}
6667

67-
/** @test */
68+
#[Test]
6869
public function it_generates_sql_query_for_location_casted_attributes(): void
6970
{
7071
// 1. Arrange
@@ -78,7 +79,7 @@ public function it_generates_sql_query_for_location_casted_attributes(): void
7879
);
7980
}
8081

81-
/** @test */
82+
#[Test]
8283
public function it_returns_location_casted_attributes(): void
8384
{
8485
// 1. Arrange

tests/LocationCastTest.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
namespace TarfinLabs\LaravelSpatial\Tests;
44

5+
use Illuminate\Database\Query\Expression;
56
use InvalidArgumentException;
67
use Illuminate\Support\Facades\DB;
8+
use PHPUnit\Framework\Attributes\Test;
79
use TarfinLabs\LaravelSpatial\Casts\LocationCast;
810
use TarfinLabs\LaravelSpatial\Tests\TestModels\Address;
911
use TarfinLabs\LaravelSpatial\Types\Point;
1012

1113
class LocationCastTest extends TestCase
1214
{
13-
/** @test */
15+
#[Test]
1416
public function it_throws_an_exception_if_casted_attribute_set_to_a_non_point_value(): void
1517
{
1618
// 1. Arrange
@@ -23,7 +25,7 @@ public function it_throws_an_exception_if_casted_attribute_set_to_a_non_point_va
2325
$address->location = 'dummy';
2426
}
2527

26-
/** @test */
28+
#[Test]
2729
public function it_can_set_the_casted_attribute_to_a_point(): void
2830
{
2931
// 1. Arrange
@@ -39,7 +41,7 @@ public function it_can_set_the_casted_attribute_to_a_point(): void
3941
$this->assertEquals(DB::raw("ST_GeomFromText('{$point->toWkt()}', 4326, 'axis-order=long-lat')"), $response);
4042
}
4143

42-
/** @test */
44+
#[Test]
4345
public function it_can_set_the_casted_attribute_to_a_point_with_srid(): void
4446
{
4547
// 1. Arrange
@@ -55,7 +57,7 @@ public function it_can_set_the_casted_attribute_to_a_point_with_srid(): void
5557
$this->assertEquals(DB::raw("ST_GeomFromText('{$point->toWkt()}', {$point->getSrid()}, 'axis-order=long-lat')"), $response);
5658
}
5759

58-
/** @test */
60+
#[Test]
5961
public function it_can_get_a_casted_attribute(): void
6062
{
6163
// 1. Arrange
@@ -73,7 +75,25 @@ public function it_can_get_a_casted_attribute(): void
7375
$this->assertEquals($point->getSrid(), $address->location->getSrid());
7476
}
7577

76-
/** @test */
78+
#[Test]
79+
public function it_can_get_a_casted_attribute_using_expression(): void
80+
{
81+
// 1. Arrange
82+
$address = new Address();
83+
$point = new Point(27.1234, 39.1234);
84+
85+
// 2. Act
86+
$cast = new LocationCast();
87+
$result = $cast->get($address, 'location', new Expression($point->toGeomFromText()), $address->getAttributes());
88+
89+
// 3. Assert
90+
$this->assertInstanceOf(Point::class, $result);
91+
$this->assertEquals($point->getLat(), $result->getLat());
92+
$this->assertEquals($point->getLng(), $result->getLng());
93+
$this->assertEquals($point->getSrid(), $result->getSrid());
94+
}
95+
96+
#[Test]
7797
public function it_returns_null_if_the_value_of_the_casted_column_is_null(): void
7898
{
7999
// 1. Arrange
@@ -86,7 +106,7 @@ public function it_returns_null_if_the_value_of_the_casted_column_is_null(): voi
86106
$this->assertNull($address->location);
87107
}
88108

89-
/** @test */
109+
#[Test]
90110
public function it_can_serialize_a_casted_attribute(): void
91111
{
92112
// 1. Arrange

tests/PointTest.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
namespace TarfinLabs\LaravelSpatial\Tests;
66

77
use Illuminate\Support\Facades\Config;
8+
use PHPUnit\Framework\Attributes\Test;
89
use TarfinLabs\LaravelSpatial\Types\Point;
910

1011
class PointTest extends TestCase
1112
{
12-
/** @test */
13+
#[Test]
1314
public function it_sets_lat_lng_and_srid_in_constructor(): void
1415
{
1516
// 1. Arrange
@@ -26,7 +27,7 @@ public function it_sets_lat_lng_and_srid_in_constructor(): void
2627
$this->assertSame(expected: $srid, actual: $point->getSrid());
2728
}
2829

29-
/** @test */
30+
#[Test]
3031
public function it_returns_default_lat_lng_and_srid_if_they_are_not_given_in_the_constructor(): void
3132
{
3233
// 1. Act
@@ -38,7 +39,7 @@ public function it_returns_default_lat_lng_and_srid_if_they_are_not_given_in_the
3839
$this->assertSame(expected: 4326, actual: $point->getSrid());
3940
}
4041

41-
/** @test */
42+
#[Test]
4243
public function it_returns_default_srid_in_config_if_it_is_not_null(): void
4344
{
4445
// 1. Arrange
@@ -53,7 +54,7 @@ public function it_returns_default_srid_in_config_if_it_is_not_null(): void
5354
$this->assertSame(expected: 4326, actual: $point->getSrid());
5455
}
5556

56-
/** @test */
57+
#[Test]
5758
public function it_returns_point_as_wkt(): void
5859
{
5960
// 1. Arrange
@@ -66,7 +67,7 @@ public function it_returns_point_as_wkt(): void
6667
$this->assertSame("POINT({$point->getLng()} {$point->getLat()})", $wkt);
6768
}
6869

69-
/** @test */
70+
#[Test]
7071
public function it_returns_point_as_pair(): void
7172
{
7273
// 1. Arrange
@@ -79,10 +80,7 @@ public function it_returns_point_as_pair(): void
7980
$this->assertSame("{$point->getLng()} {$point->getLat()}", $pair);
8081
}
8182

82-
/**
83-
* @test
84-
* @see
85-
*/
83+
#[Test]
8684
public function it_returns_points_as_geometry(): void
8785
{
8886
// 1. Arrange
@@ -95,7 +93,7 @@ public function it_returns_points_as_geometry(): void
9593
$this->assertSame("ST_GeomFromText('{$point->toWkt()}', {$point->getSrid()}, 'axis-order=long-lat')", $geometry);
9694
}
9795

98-
/** @test */
96+
#[Test]
9997
public function it_returns_points_as_array(): void
10098
{
10199
// 1. Arrange

0 commit comments

Comments
 (0)