Skip to content

Commit

Permalink
Fix SRID and options not being optional for expression in GeometryCast
Browse files Browse the repository at this point in the history
  • Loading branch information
nickknissen committed Jul 3, 2024
1 parent a6bae05 commit b76c6c2
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions src/GeometryCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ public function get($model, string $key, $value, array $attributes): ?Geometry
}

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

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

private function extractWktFromExpression(ExpressionContract $expression, Connection $connection): string
{
$grammar = $connection->getQueryGrammar();
$expressionValue = $expression->getValue($grammar);

preg_match('/ST_GeomFromText\(\'(.+)\', .+(, .+)?\)/', (string) $expressionValue, $match);

return $match[1];
}

private function extractSridFromExpression(ExpressionContract $expression, Connection $connection): int
/**
* @return array{string, int, string}
*/
private function extractFromExpression(ExpressionContract $expression, Connection $connection): array
{
$grammar = $connection->getQueryGrammar();
$expressionValue = $expression->getValue($grammar);

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

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

0 comments on commit b76c6c2

Please sign in to comment.