Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
MatanYadaev committed Aug 7, 2024
1 parent d11470b commit da936d6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
11 changes: 7 additions & 4 deletions src/GeometryCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public function get($model, string $key, $value, array $attributes): ?Geometry
}

if ($value instanceof ExpressionContract) {
$expressionValues = $this->extractFromExpression($value, $model->getConnection());
['wkt' => $wkt, 'srid' => $srid] = $this->extractValuesFromExpression($value, $model->getConnection());

return $this->className::fromWkt($expressionValues['wkt'], $expressionValues['srid']);
return $this->className::fromWkt($wkt, $srid);
}

return $this->className::fromWkb($value);
Expand Down Expand Up @@ -89,13 +89,16 @@ private function isCorrectGeometryType(mixed $value): bool
/**
* @return array{wkt: string, srid: int}
*/
private function extractFromExpression(ExpressionContract $expression, Connection $connection): array
private function extractValuesFromExpression(ExpressionContract $expression, Connection $connection): array
{
$grammar = $connection->getQueryGrammar();
$expressionValue = $expression->getValue($grammar);

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

return ['wkt' => $matches[1], 'srid' => (int) ($matches[2] ?? 0)];
return [
'wkt' => $matches[1],
'srid' => (int) ($matches[2] ?? 0),
];
}
}
34 changes: 17 additions & 17 deletions tests/GeometryCastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,29 +191,29 @@
expect($testPlace->isDirty())->toBeFalse();
});

it('handles ST_GeomFromText optional values on a raw expression', function (string $expression): void {
it('handles casting geometry columns with raw expressions', function (string $expression): void {
// Arrange
/** @var TestPlace $testPlace */
$testPlace = TestPlace::factory()->create(['point' => DB::raw($expression)]);

// Act
$testPlace->point = null;

// Assert
// Will trigger 'point' attribute to cast raw expression to a Point object
expect($testPlace->isDirty())->toBeTrue();
// Act & Assert
expect(function () use ($testPlace): void {
// Trigger 'point' attribute to cast raw expression to a `Point` object
$testPlace->originalIsEquivalent('point');
})->not->toThrow(Exception::class);
})->with([
'without SRID' => "ST_GeomFromText('POINT(12.38057 55.73406)')",
'with SRID' => "ST_GeomFromText('POINT(12.38057 55.73406)', 4326)",
'without SRID' => "ST_GeomFromText('POINT(0 0)')",
'with SRID' => "ST_GeomFromText('POINT(0 0)', 4326)",
]);

it('handles ST_GeomFromText option for mysql on a raw expression', function (): void {
it('handles casting geometry columns with raw expressions with axis order', function (): void {
// Arrange
$testPlace = TestPlace::factory()->create(['point' => DB::raw("ST_GeomFromText('POINT(12.38057 55.73406)', 4326, 'axis-order=long-lat')")]);

// Act
$testPlace->point = null;
/** @var TestPlace $testPlace */
$testPlace = TestPlace::factory()->create(['point' => DB::raw("ST_GeomFromText('POINT(0 0)', 4326, 'axis-order=long-lat')")]);

// Assert
// Will trigger 'point' attribute to cast raw expression to a Point object
expect($testPlace->isDirty())->toBeTrue();
// Act & Assert
expect(function () use ($testPlace): void {
// Trigger 'point' attribute to cast raw expression to a `Point` object
$testPlace->originalIsEquivalent('point');
})->not->toThrow(Exception::class);
})->skip(fn () => ! AxisOrder::supported(DB::connection()));

0 comments on commit da936d6

Please sign in to comment.