Skip to content

Commit 0b8bdc5

Browse files
committed
Adjust GeometryCast regex to support geometry expression without SRID
1 parent bb836ba commit 0b8bdc5

File tree

1 file changed

+8
-16
lines changed

1 file changed

+8
-16
lines changed

src/GeometryCast.php

+8-16
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@ 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+
$expressionValues = $this->extractFromExpression($value, $model->getConnection());
4140

42-
return $this->className::fromWkt($wkt, $srid);
41+
return $this->className::fromWkt($expressionValues['wkt'], $expressionValues['srid']);
4342
}
4443

4544
return $this->className::fromWkb($value);
@@ -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{wkt: string, srid: int}
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 ['wkt' => $matches[1], 'srid' => (int) ($matches[2] ?? 0)];
9789
}
9890
}

0 commit comments

Comments
 (0)