Skip to content

Handling expression in location cast v2 #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

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

## 2.2.0 - 2025-05-15
- Expression support added to `get()` method in `LocationCast`.

## 2.1.0 - 2025-02-20
- Removed support for Laravel 11 due to incompatibility with the current implementation.

Expand Down
20 changes: 19 additions & 1 deletion src/Casts/LocationCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function get($model, string $key, $value, array $attributes): ?Point
return null;
}

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

if (count($coordinates) > 1) {
$location = explode(',', str_replace(['POINT(', ')', ' '], ['', '', ','], $coordinates[0]));
Expand Down Expand Up @@ -51,4 +51,22 @@ public function serialize($model, string $key, $value, array $attributes): array
{
return $value->toArray();
}

private function getCoordinates($model, $value): array
{
if ($value instanceof Expression) {
preg_match(
pattern: "/ST_GeomFromText\(\s*'([^']+)'\s*(?:,\s*(\d+))?\s*(?:,\s*'([^']+)')?\s*\)/",
subject: (string) $value->getValue($model->getConnection()->getQueryGrammar()),
matches: $matches,
);

return [
$matches[1],
(int) ($matches[2] ?? 0),
];
}

return explode(',', $value);
}
}
19 changes: 19 additions & 0 deletions tests/LocationCastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace TarfinLabs\LaravelSpatial\Tests;

use Illuminate\Database\Query\Expression;
use InvalidArgumentException;
use Illuminate\Support\Facades\DB;
use TarfinLabs\LaravelSpatial\Casts\LocationCast;
Expand Down Expand Up @@ -73,6 +74,24 @@ public function it_can_get_a_casted_attribute(): void
$this->assertEquals($point->getSrid(), $address->location->getSrid());
}

/** @test */
public function it_can_get_a_casted_attribute_using_expression(): void
{
// 1. Arrange
$address = new Address();
$point = new Point(27.1234, 39.1234);

// 2. Act
$cast = new LocationCast();
$result = $cast->get($address, 'location', new Expression($point->toGeomFromText()), $address->getAttributes());

// 3. Assert
$this->assertInstanceOf(Point::class, $result);
$this->assertEquals($point->getLat(), $result->getLat());
$this->assertEquals($point->getLng(), $result->getLng());
$this->assertEquals($point->getSrid(), $result->getSrid());
}

/** @test */
public function it_returns_null_if_the_value_of_the_casted_column_is_null(): void
{
Expand Down