diff --git a/composer.json b/composer.json index 4451cb50..ba92744e 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "grimzy/laravel-mysql-spatial", + "name": "moova/laravel-mysql-spatial", "description": "MySQL spatial data types extension for Laravel.", "scripts": { "test": "phpunit -c phpunit.xml.dist", @@ -15,18 +15,18 @@ } ], "require": { - "php": ">=7.3", + "php": ">=8.2", "ext-pdo": "*", "ext-json": "*", - "illuminate/database": "^8.0", + "illuminate/database": "^10.0|^11.0", "geo-io/wkb-parser": "^1.0", - "jmikola/geojson": "^1.0" + "jmikola/geojson": "^1.1" }, "require-dev": { - "phpunit/phpunit": "~6.5", - "laravel/laravel": "^8.0", - "doctrine/dbal": "^2.5", - "laravel/browser-kit-testing": "^2.0", + "phpunit/phpunit": "^10.0", + "laravel/laravel": "^10.0", + "doctrine/dbal": "^4.0", + "laravel/browser-kit-testing": "^7.2", "mockery/mockery": "^1.3" }, "autoload": { diff --git a/src/Eloquent/SpatialExpression.php b/src/Eloquent/SpatialExpression.php index 9224af0f..249a5ab7 100644 --- a/src/Eloquent/SpatialExpression.php +++ b/src/Eloquent/SpatialExpression.php @@ -3,12 +3,16 @@ namespace Grimzy\LaravelMysqlSpatial\Eloquent; use Illuminate\Database\Query\Expression; +use Illuminate\Database\Grammar; class SpatialExpression extends Expression { - public function getValue() + public function getValue(Grammar $grammar) { - return "ST_GeomFromText(?, ?, 'axis-order=long-lat')"; + if (config('moova_spatial.mysql_version_8')) { + return "ST_GeomFromText(?, ?, 'axis-order=long-lat')"; + } + return "ST_GeomFromText(?, ?)"; } public function getSpatialValue() diff --git a/src/Eloquent/SpatialTrait.php b/src/Eloquent/SpatialTrait.php index 5cc3f4b1..fb38e59c 100755 --- a/src/Eloquent/SpatialTrait.php +++ b/src/Eloquent/SpatialTrait.php @@ -134,7 +134,8 @@ public function scopeDistance($query, $geometryColumn, $geometry, $distance) { $this->isColumnAllowed($geometryColumn); - $query->whereRaw("st_distance(`$geometryColumn`, ST_GeomFromText(?, ?, 'axis-order=long-lat')) <= ?", [ + $geometryObjectText = $this->getGeometryObjectText(); + $query->whereRaw("st_distance(`$geometryColumn`, $geometryObjectText) <= ?", [ $geometry->toWkt(), $geometry->getSrid(), $distance, @@ -148,8 +149,8 @@ public function scopeDistanceExcludingSelf($query, $geometryColumn, $geometry, $ $this->isColumnAllowed($geometryColumn); $query = $this->scopeDistance($query, $geometryColumn, $geometry, $distance); - - $query->whereRaw("st_distance(`$geometryColumn`, ST_GeomFromText(?, ?, 'axis-order=long-lat')) != 0", [ + $geometryObjectText = $this->getGeometryObjectText(); + $query->whereRaw("st_distance(`$geometryColumn`, $geometryObjectText) != 0", [ $geometry->toWkt(), $geometry->getSrid(), ]); @@ -166,8 +167,8 @@ public function scopeDistanceValue($query, $geometryColumn, $geometry) if (!$columns) { $query->select('*'); } - - $query->selectRaw("st_distance(`$geometryColumn`, ST_GeomFromText(?, ?, 'axis-order=long-lat')) as distance", [ + $geometryObjectText = $this->getGeometryObjectText(); + $query->selectRaw("st_distance(`$geometryColumn`, $geometryObjectText) as distance", [ $geometry->toWkt(), $geometry->getSrid(), ]); @@ -176,8 +177,8 @@ public function scopeDistanceValue($query, $geometryColumn, $geometry) public function scopeDistanceSphere($query, $geometryColumn, $geometry, $distance) { $this->isColumnAllowed($geometryColumn); - - $query->whereRaw("st_distance_sphere(`$geometryColumn`, ST_GeomFromText(?, ?, 'axis-order=long-lat')) <= ?", [ + $geometryObjectText = $this->getGeometryObjectText(); + $query->whereRaw("st_distance_sphere(`$geometryColumn`, $geometryObjectText) <= ?", [ $geometry->toWkt(), $geometry->getSrid(), $distance, @@ -191,8 +192,8 @@ public function scopeDistanceSphereExcludingSelf($query, $geometryColumn, $geome $this->isColumnAllowed($geometryColumn); $query = $this->scopeDistanceSphere($query, $geometryColumn, $geometry, $distance); - - $query->whereRaw("st_distance_sphere($geometryColumn, ST_GeomFromText(?, ?, 'axis-order=long-lat')) != 0", [ + $geometryObjectText = $this->getGeometryObjectText(); + $query->whereRaw("st_distance_sphere($geometryColumn, $geometryObjectText) != 0", [ $geometry->toWkt(), $geometry->getSrid(), ]); @@ -209,7 +210,8 @@ public function scopeDistanceSphereValue($query, $geometryColumn, $geometry) if (!$columns) { $query->select('*'); } - $query->selectRaw("st_distance_sphere(`$geometryColumn`, ST_GeomFromText(?, ?, 'axis-order=long-lat')) as distance", [ + $geometryObjectText = $this->getGeometryObjectText(); + $query->selectRaw("st_distance_sphere(`$geometryColumn`, $geometryObjectText) as distance", [ $geometry->toWkt(), $geometry->getSrid(), ]); @@ -222,8 +224,8 @@ public function scopeComparison($query, $geometryColumn, $geometry, $relationshi if (!in_array($relationship, $this->stRelations)) { throw new UnknownSpatialRelationFunction($relationship); } - - $query->whereRaw("st_{$relationship}(`$geometryColumn`, ST_GeomFromText(?, ?, 'axis-order=long-lat'))", [ + $geometryObjectText = $this->getGeometryObjectText(); + $query->whereRaw("st_{$relationship}(`$geometryColumn`, $geometryObjectText)", [ $geometry->toWkt(), $geometry->getSrid(), ]); @@ -271,15 +273,21 @@ public function scopeDoesTouch($query, $geometryColumn, $geometry) return $this->scopeComparison($query, $geometryColumn, $geometry, 'touches'); } - public function scopeOrderBySpatial($query, $geometryColumn, $geometry, $orderFunction, $direction = 'asc') - { + public function scopeOrderBySpatial( + $query, + $geometryColumn, + $geometry, + $orderFunction, + $direction = 'asc', + $geometryOptions = null + ) { $this->isColumnAllowed($geometryColumn); if (!in_array($orderFunction, $this->stOrderFunctions)) { throw new UnknownSpatialFunctionException($orderFunction); } - - $query->orderByRaw("st_{$orderFunction}(`$geometryColumn`, ST_GeomFromText(?, ?, 'axis-order=long-lat')) {$direction}", [ + $geometryObjectText = $this->getGeometryObjectText(); + $query->orderByRaw("st_{$orderFunction}(`$geometryColumn`, $geometryObjectText) {$direction}", [ $geometry->toWkt(), $geometry->getSrid(), ]); @@ -296,4 +304,12 @@ public function scopeOrderByDistanceSphere($query, $geometryColumn, $geometry, $ { return $this->scopeOrderBySpatial($query, $geometryColumn, $geometry, 'distance_sphere', $direction); } + + private function getGeometryObjectText() + { + if (config('moova_spatial.mysql_version_8')) { + return "ST_GeomFromText(?, ?, 'axis-order=long-lat')"; + } + return "ST_GeomFromText(?, ?)"; + } } diff --git a/src/MysqlConnection.php b/src/MysqlConnection.php index 38a2b1a4..921c7060 100644 --- a/src/MysqlConnection.php +++ b/src/MysqlConnection.php @@ -2,37 +2,12 @@ namespace Grimzy\LaravelMysqlSpatial; -use Doctrine\DBAL\Types\Type as DoctrineType; use Grimzy\LaravelMysqlSpatial\Schema\Builder; use Grimzy\LaravelMysqlSpatial\Schema\Grammars\MySqlGrammar; use Illuminate\Database\MySqlConnection as IlluminateMySqlConnection; class MysqlConnection extends IlluminateMySqlConnection { - public function __construct($pdo, $database = '', $tablePrefix = '', array $config = []) - { - parent::__construct($pdo, $database, $tablePrefix, $config); - - if (class_exists(DoctrineType::class)) { - // Prevent geometry type fields from throwing a 'type not found' error when changing them - $geometries = [ - 'geometry', - 'point', - 'linestring', - 'polygon', - 'multipoint', - 'multilinestring', - 'multipolygon', - 'geometrycollection', - 'geomcollection', - ]; - $dbPlatform = $this->getDoctrineSchemaManager()->getDatabasePlatform(); - foreach ($geometries as $type) { - $dbPlatform->registerDoctrineTypeMapping($type, 'string'); - } - } - } - /** * Get the default schema grammar instance. * @@ -56,4 +31,4 @@ public function getSchemaBuilder() return new Builder($this); } -} +} \ No newline at end of file diff --git a/src/Schema/Blueprint.php b/src/Schema/Blueprint.php index 0a333f06..bc11aed7 100644 --- a/src/Schema/Blueprint.php +++ b/src/Schema/Blueprint.php @@ -14,9 +14,9 @@ class Blueprint extends IlluminateBlueprint * * @return \Illuminate\Support\Fluent */ - public function geometry($column, $srid = null) + public function geometry($column, $subtype = null, $srid = 0) { - return $this->addColumn('geometry', $column, compact('srid')); + return $this->addColumn('geometry', $column, compact('subtype', 'srid')); } /** diff --git a/src/SpatialServiceProvider.php b/src/SpatialServiceProvider.php index 3b859f8e..9cf14db5 100644 --- a/src/SpatialServiceProvider.php +++ b/src/SpatialServiceProvider.php @@ -2,16 +2,7 @@ namespace Grimzy\LaravelMysqlSpatial; -use Doctrine\DBAL\Types\Type as DoctrineType; use Grimzy\LaravelMysqlSpatial\Connectors\ConnectionFactory; -use Grimzy\LaravelMysqlSpatial\Doctrine\Geometry; -use Grimzy\LaravelMysqlSpatial\Doctrine\GeometryCollection; -use Grimzy\LaravelMysqlSpatial\Doctrine\LineString; -use Grimzy\LaravelMysqlSpatial\Doctrine\MultiLineString; -use Grimzy\LaravelMysqlSpatial\Doctrine\MultiPoint; -use Grimzy\LaravelMysqlSpatial\Doctrine\MultiPolygon; -use Grimzy\LaravelMysqlSpatial\Doctrine\Point; -use Grimzy\LaravelMysqlSpatial\Doctrine\Polygon; use Illuminate\Database\DatabaseManager; use Illuminate\Database\DatabaseServiceProvider; @@ -40,25 +31,5 @@ public function register() $this->app->singleton('db', function ($app) { return new DatabaseManager($app, $app['db.factory']); }); - - if (class_exists(DoctrineType::class)) { - // Prevent geometry type fields from throwing a 'type not found' error when changing them - $geometries = [ - 'geometry' => Geometry::class, - 'point' => Point::class, - 'linestring' => LineString::class, - 'polygon' => Polygon::class, - 'multipoint' => MultiPoint::class, - 'multilinestring' => MultiLineString::class, - 'multipolygon' => MultiPolygon::class, - 'geometrycollection' => GeometryCollection::class, - ]; - $typeNames = array_keys(DoctrineType::getTypesMap()); - foreach ($geometries as $type => $class) { - if (!in_array($type, $typeNames)) { - DoctrineType::addType($type, $class); - } - } - } } -} +} \ No newline at end of file