Skip to content

Commit ed03da2

Browse files
authored
Merge pull request #42 from tarfin-labs/handling-expression-in-location-cast-v2
Handling expression in location cast v2
2 parents a48cc6c + 115849d commit ed03da2

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

CHANGELOG.md

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

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

5+
## 2.2.0 - 2025-05-15
6+
- Expression support added to `get()` method in `LocationCast`.
7+
58
## 2.1.0 - 2025-02-20
69
- Removed support for Laravel 11 due to incompatibility with the current implementation.
710

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/LocationCastTest.php

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

33
namespace TarfinLabs\LaravelSpatial\Tests;
44

5+
use Illuminate\Database\Query\Expression;
56
use InvalidArgumentException;
67
use Illuminate\Support\Facades\DB;
78
use TarfinLabs\LaravelSpatial\Casts\LocationCast;
@@ -73,6 +74,24 @@ public function it_can_get_a_casted_attribute(): void
7374
$this->assertEquals($point->getSrid(), $address->location->getSrid());
7475
}
7576

77+
/** @test */
78+
public function it_can_get_a_casted_attribute_using_expression(): void
79+
{
80+
// 1. Arrange
81+
$address = new Address();
82+
$point = new Point(27.1234, 39.1234);
83+
84+
// 2. Act
85+
$cast = new LocationCast();
86+
$result = $cast->get($address, 'location', new Expression($point->toGeomFromText()), $address->getAttributes());
87+
88+
// 3. Assert
89+
$this->assertInstanceOf(Point::class, $result);
90+
$this->assertEquals($point->getLat(), $result->getLat());
91+
$this->assertEquals($point->getLng(), $result->getLng());
92+
$this->assertEquals($point->getSrid(), $result->getSrid());
93+
}
94+
7695
/** @test */
7796
public function it_returns_null_if_the_value_of_the_casted_column_is_null(): void
7897
{

0 commit comments

Comments
 (0)