Skip to content

Commit da936d6

Browse files
committed
formatting
1 parent d11470b commit da936d6

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

src/GeometryCast.php

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

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

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

4444
return $this->className::fromWkb($value);
@@ -89,13 +89,16 @@ private function isCorrectGeometryType(mixed $value): bool
8989
/**
9090
* @return array{wkt: string, srid: int}
9191
*/
92-
private function extractFromExpression(ExpressionContract $expression, Connection $connection): array
92+
private function extractValuesFromExpression(ExpressionContract $expression, Connection $connection): array
9393
{
9494
$grammar = $connection->getQueryGrammar();
9595
$expressionValue = $expression->getValue($grammar);
9696

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

99-
return ['wkt' => $matches[1], 'srid' => (int) ($matches[2] ?? 0)];
99+
return [
100+
'wkt' => $matches[1],
101+
'srid' => (int) ($matches[2] ?? 0),
102+
];
100103
}
101104
}

tests/GeometryCastTest.php

+17-17
Original file line numberDiff line numberDiff line change
@@ -191,29 +191,29 @@
191191
expect($testPlace->isDirty())->toBeFalse();
192192
});
193193

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

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

209-
it('handles ST_GeomFromText option for mysql on a raw expression', function (): void {
209+
it('handles casting geometry columns with raw expressions with axis order', function (): void {
210210
// Arrange
211-
$testPlace = TestPlace::factory()->create(['point' => DB::raw("ST_GeomFromText('POINT(12.38057 55.73406)', 4326, 'axis-order=long-lat')")]);
212-
213-
// Act
214-
$testPlace->point = null;
211+
/** @var TestPlace $testPlace */
212+
$testPlace = TestPlace::factory()->create(['point' => DB::raw("ST_GeomFromText('POINT(0 0)', 4326, 'axis-order=long-lat')")]);
215213

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

0 commit comments

Comments
 (0)