Skip to content

Commit 26825ce

Browse files
committed
Adjust GeometryCast regex to support geometry expression without SRID
1 parent 96b5d0c commit 26825ce

File tree

2 files changed

+9
-18
lines changed

2 files changed

+9
-18
lines changed

Diff for: src/GeometryCast.php

+7-15
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ public function get($model, string $key, $value, array $attributes): ?Geometry
3636
}
3737

3838
if ($value instanceof ExpressionContract) {
39-
$wkt = $this->extractWktFromExpression($value, $model->getConnection());
40-
$srid = $this->extractSridFromExpression($value, $model->getConnection());
39+
[$wkt, $srid, $options] = $this->extractFromExpression($value, $model->getConnection());
4140

4241
return $this->className::fromWkt($wkt, $srid);
4342
}
@@ -76,23 +75,16 @@ public function set($model, string $key, $value, array $attributes): ?Expression
7675
return $value->toSqlExpression($model->getConnection());
7776
}
7877

79-
private function extractWktFromExpression(ExpressionContract $expression, Connection $connection): string
80-
{
81-
$grammar = $connection->getQueryGrammar();
82-
$expressionValue = $expression->getValue($grammar);
83-
84-
preg_match('/ST_GeomFromText\(\'(.+)\', .+(, .+)?\)/', (string) $expressionValue, $match);
85-
86-
return $match[1];
87-
}
88-
89-
private function extractSridFromExpression(ExpressionContract $expression, Connection $connection): int
78+
/**
79+
* @return array{string, int, string}
80+
*/
81+
private function extractFromExpression(ExpressionContract $expression, Connection $connection): array
9082
{
9183
$grammar = $connection->getQueryGrammar();
9284
$expressionValue = $expression->getValue($grammar);
9385

94-
preg_match('/ST_GeomFromText\(\'.+\', (.+)(, .+)?\)/', (string) $expressionValue, $match);
86+
preg_match("/ST_GeomFromText\(\s*'([^']+)'\s*(?:,\s*(\d+))?\s*(?:,\s*'([^']+)')?\s*\)/", (string) $expressionValue, $matches);
9587

96-
return (int) $match[1];
88+
return [$matches[1], (int) ($matches[2] ?? 0), $matches[3] ?? ''];
9789
}
9890
}

Diff for: tests/GeometryCastTest.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22

33
use Illuminate\Support\Facades\DB;
4-
use MatanYadaev\EloquentSpatial\AxisOrder;
54
use MatanYadaev\EloquentSpatial\Enums\Srid;
65
use MatanYadaev\EloquentSpatial\GeometryExpression;
76
use MatanYadaev\EloquentSpatial\Objects\LineString;
@@ -197,6 +196,6 @@
197196
$testPlace->point = null;
198197
expect($testPlace->isDirty())->toBeTrue();
199198
})->with([
200-
"without SRID" => "ST_GeomFromText('POINT(12.38057 55.73406)')",
201-
"with SRID" => "ST_GeomFromText('POINT(12.38057 55.73406)', 4326)",
199+
'without SRID' => "ST_GeomFromText('POINT(12.38057 55.73406)')",
200+
'with SRID' => "ST_GeomFromText('POINT(12.38057 55.73406)', 4326)",
202201
]);

0 commit comments

Comments
 (0)