Skip to content

Commit

Permalink
- Tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
dash8x committed May 29, 2024
1 parent 8a16147 commit 5874bd5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
13 changes: 6 additions & 7 deletions src/GeometryCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

namespace Javaabu\Geospatial;

use Illuminate\Contracts\Database\Query\Expression as ExpressionContract;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Connection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use MatanYadaev\EloquentSpatial\GeometryExpression;
use MatanYadaev\EloquentSpatial\Objects\Geometry;

class GeometryCast extends \MatanYadaev\EloquentSpatial\GeometryCast
Expand All @@ -21,7 +20,7 @@ public function __construct(string $className)

/**
* @param Model $model
* @param string|ExpressionContract|null $value
* @param string|Expression|null $value
* @param array<string, mixed> $attributes
*/
public function get($model, string $key, $value, array $attributes): ?Geometry
Expand All @@ -34,8 +33,8 @@ public function get($model, string $key, $value, array $attributes): ?Geometry
return null;
}

if (! $value instanceof ExpressionContract) {
$value = DB::raw((new GeometryExpression($value))->normalize($model->getConnection()));
if (! $value instanceof Expression) {
$value = DB::raw($value);
}

$wkt = $this->extractWktFromExpression($value, $model->getConnection());
Expand All @@ -44,7 +43,7 @@ public function get($model, string $key, $value, array $attributes): ?Geometry
return $this->className::fromWkt($wkt, $srid);
}

protected function extractWktFromExpression(ExpressionContract $expression, Connection $connection): string
protected function extractWktFromExpression(Expression $expression, Connection $connection): string
{
$grammar = $connection->getQueryGrammar();
$expressionValue = $expression->getValue($grammar);
Expand All @@ -54,7 +53,7 @@ protected function extractWktFromExpression(ExpressionContract $expression, Conn
return $match[1];
}

protected function extractSridFromExpression(ExpressionContract $expression, Connection $connection): int
protected function extractSridFromExpression(Expression $expression, Connection $connection): int
{
$grammar = $connection->getQueryGrammar();
$expressionValue = $expression->getValue($grammar);
Expand Down
7 changes: 3 additions & 4 deletions src/Objects/Point.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,25 @@
namespace Javaabu\Geospatial\Objects;

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Contracts\Database\Query\Expression as ExpressionContract;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Support\Facades\DB;
use InvalidArgumentException;
use Javaabu\Geospatial\Factory;
use Javaabu\Geospatial\GeometryCast;
use Javaabu\Geospatial\Geospatial;
use MatanYadaev\EloquentSpatial\GeometryExpression;

class Point extends \MatanYadaev\EloquentSpatial\Objects\Point
{
public function toSqlExpression(ConnectionInterface $connection): ExpressionContract
public function toSqlExpression(ConnectionInterface $connection): Expression
{
if (Geospatial::supported($connection)) {
return parent::toSqlExpression($connection);
}

$wkt = $this->toWkt();

return DB::raw('"' . (new GeometryExpression("ST_GeomFromText('{$wkt}', {$this->srid})"))->normalize($connection) . '"');
return DB::raw('"' . "ST_GeomFromText('{$wkt}', {$this->srid})" . '"');
}

public static function castUsing(array $arguments): CastsAttributes
Expand Down
7 changes: 3 additions & 4 deletions src/Objects/Polygon.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,26 @@
namespace Javaabu\Geospatial\Objects;

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Contracts\Database\Query\Expression as ExpressionContract;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use InvalidArgumentException;
use Javaabu\Geospatial\Factory;
use Javaabu\Geospatial\GeometryCast;
use Javaabu\Geospatial\Geospatial;
use MatanYadaev\EloquentSpatial\GeometryExpression;

class Polygon extends \MatanYadaev\EloquentSpatial\Objects\Polygon
{
public function toSqlExpression(ConnectionInterface $connection): ExpressionContract
public function toSqlExpression(ConnectionInterface $connection): Expression
{
if (Geospatial::supported($connection)) {
return parent::toSqlExpression($connection);
}

$wkt = $this->toWkt();

return DB::raw('"' . (new GeometryExpression("ST_GeomFromText('{$wkt}', {$this->srid})"))->normalize($connection) . '"');
return DB::raw('"' . "ST_GeomFromText('{$wkt}', {$this->srid})" . '"');
}

public static function castUsing(array $arguments): CastsAttributes
Expand Down

0 comments on commit 5874bd5

Please sign in to comment.