diff --git a/src/GeometryCast.php b/src/GeometryCast.php index 3b580c0..79a11e5 100644 --- a/src/GeometryCast.php +++ b/src/GeometryCast.php @@ -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); @@ -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), + ]; } } diff --git a/tests/GeometryCastTest.php b/tests/GeometryCastTest.php index bcd0569..e62283c 100644 --- a/tests/GeometryCastTest.php +++ b/tests/GeometryCastTest.php @@ -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()));