Skip to content

Commit bfb9a4f

Browse files
SynchroMatanYadaev
andauthored
Add Geometry@toSqlExpression method (#70)
* Add a manual casting function, fixes #69 * Requested changes for #70 * Update .run/Test.run.xml Co-authored-by: Matan Yadaev <[email protected]> * Update API.md Co-authored-by: Matan Yadaev <[email protected]> * Update tests/Objects/GeometryTest.php Co-authored-by: Matan Yadaev <[email protected]> * Update tests/Objects/GeometryTest.php Co-authored-by: Matan Yadaev <[email protected]> * Use expression generator in cast and builder, see #70 Co-authored-by: Matan Yadaev <[email protected]>
1 parent 2d9628e commit bfb9a4f

File tree

5 files changed

+38
-24
lines changed

5 files changed

+38
-24
lines changed

API.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Geometry classes can be also created by these static methods:
2525
* `toWkt()` - Serializes the geometry object into a WKT.
2626
* `toWkb()` - Serializes the geometry object into a WKB.
2727
* `getCoordinates()` - Returns the coordinates of the geometry object.
28-
28+
* `toSqlExpression(ConnectionInterface $connection)` - Serializes the geometry object into an SQL query.
2929
In addition, `GeometryCollection` also has these functions:
3030

3131
* `getGeometries()` - Returns a geometry array. Can be used with `ArrayAccess` as well.

src/GeometryCast.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
88
use Illuminate\Database\Eloquent\Model;
99
use Illuminate\Database\Query\Expression;
10-
use Illuminate\Support\Facades\DB;
1110
use InvalidArgumentException;
1211
use MatanYadaev\EloquentSpatial\Objects\Geometry;
1312

@@ -73,17 +72,7 @@ public function set($model, string $key, $value, array $attributes): Expression|
7372
);
7473
}
7574

76-
$wkt = $value->toWkt();
77-
78-
$connection = $model->getConnection();
79-
80-
if (! (new AxisOrder)->supported($connection)) {
81-
// @codeCoverageIgnoreStart
82-
return DB::raw("ST_GeomFromText('{$wkt}', {$value->srid})");
83-
// @codeCoverageIgnoreEnd
84-
}
85-
86-
return DB::raw("ST_GeomFromText('{$wkt}', {$value->srid}, 'axis-order=long-lat')");
75+
return $value->toSqlExpression($model->getConnection());
8776
}
8877

8978
private function extractWktFromExpression(Expression $expression): string

src/Objects/Geometry.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@
99
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
1010
use Illuminate\Contracts\Support\Arrayable;
1111
use Illuminate\Contracts\Support\Jsonable;
12+
use Illuminate\Database\ConnectionInterface;
13+
use Illuminate\Database\Query\Expression;
14+
use Illuminate\Support\Facades\DB;
1215
use Illuminate\Support\Traits\Macroable;
1316
use InvalidArgumentException;
1417
use JsonException;
1518
use JsonSerializable;
19+
use MatanYadaev\EloquentSpatial\AxisOrder;
1620
use MatanYadaev\EloquentSpatial\Factory;
1721
use MatanYadaev\EloquentSpatial\GeometryCast;
1822
use Stringable;
@@ -203,4 +207,21 @@ public static function castUsing(array $arguments): CastsAttributes
203207
{
204208
return new GeometryCast(static::class);
205209
}
210+
211+
/**
212+
* @param ConnectionInterface $connection
213+
* @return Expression
214+
*/
215+
public function toSqlExpression(ConnectionInterface $connection): Expression
216+
{
217+
$wkt = $this->toWkt();
218+
219+
if (! (new AxisOrder)->supported($connection)) {
220+
// @codeCoverageIgnoreStart
221+
return DB::raw("ST_GeomFromText('{$wkt}', {$this->srid})");
222+
// @codeCoverageIgnoreEnd
223+
}
224+
225+
return DB::raw("ST_GeomFromText('{$wkt}', {$this->srid}, 'axis-order=long-lat')");
226+
}
206227
}

src/SpatialBuilder.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -289,17 +289,7 @@ public function whereSrid(
289289
protected function toExpression(Geometry|string $geometryOrColumn): Expression
290290
{
291291
if ($geometryOrColumn instanceof Geometry) {
292-
$wkt = $geometryOrColumn->toWkt();
293-
294-
$connection = $this->getConnection();
295-
296-
if (! (new AxisOrder)->supported($connection)) {
297-
// @codeCoverageIgnoreStart
298-
return DB::raw("ST_GeomFromText('{$wkt}', {$geometryOrColumn->srid})");
299-
// @codeCoverageIgnoreEnd
300-
}
301-
302-
return DB::raw("ST_GeomFromText('{$wkt}', {$geometryOrColumn->srid}, 'axis-order=long-lat')");
292+
return $geometryOrColumn->toSqlExpression($this->getConnection());
303293
}
304294

305295
return DB::raw($this->getQuery()->getGrammar()->wrap($geometryOrColumn));

tests/Objects/GeometryTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,20 @@
8080
})->toThrow(InvalidArgumentException::class);
8181
});
8282

83+
it('creates an SQL expression from a geometry', function (): void {
84+
$point = new Point(0, 180, 4326);
85+
86+
expect($point->toSqlExpression(DB::connection()))
87+
->toEqual("ST_GeomFromText('POINT(180 0)', 4326, 'axis-order=long-lat')");
88+
})->skip(fn () => ! (new AxisOrder)->supported(DB::connection()));
89+
90+
it('creates an SQL expression from a geometry - without axis-order', function (): void {
91+
$point = new Point(0, 180, 4326);
92+
93+
expect($point->toSqlExpression(DB::connection()))
94+
->toEqual("ST_GeomFromText('POINT(180 0)', 4326)");
95+
})->skip(fn () => (new AxisOrder)->supported(DB::connection()));
96+
8397
it('creates a geometry object from a geo json array', function (): void {
8498
$point = new Point(0, 180);
8599
$pointGeoJsonArray = $point->toArray();

0 commit comments

Comments
 (0)