From 46a827c54fd706c7eb0652fe9616886fc87fb8cb Mon Sep 17 00:00:00 2001 From: Dominique Vienne Date: Tue, 7 Mar 2023 12:11:29 +0100 Subject: [PATCH 1/4] Composer updates for Laravel 10 migration --- composer.json | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index 4451cb50..218a078d 100644 --- a/composer.json +++ b/composer.json @@ -15,19 +15,19 @@ } ], "require": { - "php": ">=7.3", + "php": "^8.1", "ext-pdo": "*", "ext-json": "*", - "illuminate/database": "^8.0", + "illuminate/database": "^10.2", "geo-io/wkb-parser": "^1.0", "jmikola/geojson": "^1.0" }, "require-dev": { - "phpunit/phpunit": "~6.5", - "laravel/laravel": "^8.0", - "doctrine/dbal": "^2.5", - "laravel/browser-kit-testing": "^2.0", - "mockery/mockery": "^1.3" + "laravel/laravel": "^10.0", + "doctrine/dbal": "^3.4", + "phpunit/phpunit": "^10.0", + "laravel/browser-kit-testing": "^7.0", + "mockery/mockery": "^1.5" }, "autoload": { "psr-4": { @@ -42,7 +42,7 @@ }, "extra": { "branch-alias": { - "dev-master": "4.0.x-dev" + "dev-master": "6.0.x-dev" }, "laravel": { "providers": [ From 71edb273ae83c283ebf44bc94e24d5b36c3d48d1 Mon Sep 17 00:00:00 2001 From: Dominique Vienne Date: Tue, 7 Mar 2023 12:12:48 +0100 Subject: [PATCH 2/4] PHPDoc and enforcing data types --- src/Connectors/ConnectionFactory.php | 10 +++--- src/Doctrine/Geometry.php | 4 +-- src/Doctrine/GeometryCollection.php | 4 +-- src/Doctrine/LineString.php | 4 +-- src/Doctrine/MultiLineString.php | 4 +-- src/Doctrine/MultiPoint.php | 4 +-- src/Doctrine/MultiPolygon.php | 4 +-- src/Doctrine/Point.php | 4 +-- src/Doctrine/Polygon.php | 4 +-- src/Eloquent/Builder.php | 2 +- src/Eloquent/SpatialExpression.php | 2 +- src/Eloquent/SpatialTrait.php | 19 ++++++----- src/MysqlConnection.php | 25 +++++++++++--- src/Schema/Blueprint.php | 41 +++++++++++----------- src/Schema/Builder.php | 2 +- src/Schema/Grammars/MySqlGrammar.php | 22 ++++++------ src/SpatialServiceProvider.php | 4 ++- src/Types/Factory.php | 16 ++++----- src/Types/Geometry.php | 42 ++++++++++------------- src/Types/GeometryCollection.php | 51 ++++++++++++++++------------ src/Types/LineString.php | 12 +++---- src/Types/MultiLineString.php | 14 ++++---- src/Types/MultiPoint.php | 12 +++---- src/Types/MultiPolygon.php | 28 +++++++++------ src/Types/Point.php | 21 ++++++------ src/Types/PointCollection.php | 6 ++-- src/Types/Polygon.php | 6 ++-- 27 files changed, 197 insertions(+), 170 deletions(-) diff --git a/src/Connectors/ConnectionFactory.php b/src/Connectors/ConnectionFactory.php index 223cea13..9bff2b81 100644 --- a/src/Connectors/ConnectionFactory.php +++ b/src/Connectors/ConnectionFactory.php @@ -3,19 +3,21 @@ namespace Grimzy\LaravelMysqlSpatial\Connectors; use Grimzy\LaravelMysqlSpatial\MysqlConnection; +use Illuminate\Contracts\Container\BindingResolutionException; use Illuminate\Database\Connectors\ConnectionFactory as IlluminateConnectionFactory; use PDO; class ConnectionFactory extends IlluminateConnectionFactory { /** - * @param string $driver + * @param string $driver * @param \Closure|PDO $connection - * @param string $database - * @param string $prefix - * @param array $config + * @param string $database + * @param string $prefix + * @param array $config * * @return \Illuminate\Database\ConnectionInterface + * @throws BindingResolutionException */ protected function createConnection($driver, $connection, $database, $prefix = '', array $config = []) { diff --git a/src/Doctrine/Geometry.php b/src/Doctrine/Geometry.php index e7e138f9..0211b3f2 100644 --- a/src/Doctrine/Geometry.php +++ b/src/Doctrine/Geometry.php @@ -9,12 +9,12 @@ class Geometry extends Type { const GEOMETRY = 'geometry'; - public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) + public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string { return 'geometry'; } - public function getName() + public function getName(): string { return self::GEOMETRY; } diff --git a/src/Doctrine/GeometryCollection.php b/src/Doctrine/GeometryCollection.php index 16d106fb..d62d5ef7 100644 --- a/src/Doctrine/GeometryCollection.php +++ b/src/Doctrine/GeometryCollection.php @@ -9,12 +9,12 @@ class GeometryCollection extends Type { const GEOMETRYCOLLECTION = 'geometrycollection'; - public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) + public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string { return 'geometrycollection'; } - public function getName() + public function getName(): string { return self::GEOMETRYCOLLECTION; } diff --git a/src/Doctrine/LineString.php b/src/Doctrine/LineString.php index 9ba31476..e0cf79c8 100644 --- a/src/Doctrine/LineString.php +++ b/src/Doctrine/LineString.php @@ -9,12 +9,12 @@ class LineString extends Type { const LINESTRING = 'linestring'; - public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) + public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string { return 'linestring'; } - public function getName() + public function getName(): string { return self::LINESTRING; } diff --git a/src/Doctrine/MultiLineString.php b/src/Doctrine/MultiLineString.php index ffabd599..2da4b7e8 100644 --- a/src/Doctrine/MultiLineString.php +++ b/src/Doctrine/MultiLineString.php @@ -9,12 +9,12 @@ class MultiLineString extends Type { const MULTILINESTRING = 'multilinestring'; - public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) + public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string { return 'multilinestring'; } - public function getName() + public function getName(): string { return self::MULTILINESTRING; } diff --git a/src/Doctrine/MultiPoint.php b/src/Doctrine/MultiPoint.php index 894ce70c..5778d8b4 100644 --- a/src/Doctrine/MultiPoint.php +++ b/src/Doctrine/MultiPoint.php @@ -9,12 +9,12 @@ class MultiPoint extends Type { const MULTIPOINT = 'multipoint'; - public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) + public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string { return 'multipoint'; } - public function getName() + public function getName(): string { return self::MULTIPOINT; } diff --git a/src/Doctrine/MultiPolygon.php b/src/Doctrine/MultiPolygon.php index 0d2e6185..fc9e897a 100644 --- a/src/Doctrine/MultiPolygon.php +++ b/src/Doctrine/MultiPolygon.php @@ -9,12 +9,12 @@ class MultiPolygon extends Type { const MULTIPOLYGON = 'multipolygon'; - public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) + public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string { return 'multipolygon'; } - public function getName() + public function getName(): string { return self::MULTIPOLYGON; } diff --git a/src/Doctrine/Point.php b/src/Doctrine/Point.php index 3e8365e9..3fbf3d1d 100644 --- a/src/Doctrine/Point.php +++ b/src/Doctrine/Point.php @@ -9,12 +9,12 @@ class Point extends Type { const POINT = 'point'; - public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) + public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string { return 'point'; } - public function getName() + public function getName(): string { return self::POINT; } diff --git a/src/Doctrine/Polygon.php b/src/Doctrine/Polygon.php index e8c81c09..0743ad74 100644 --- a/src/Doctrine/Polygon.php +++ b/src/Doctrine/Polygon.php @@ -9,12 +9,12 @@ class Polygon extends Type { const POLYGON = 'polygon'; - public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) + public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string { return 'polygon'; } - public function getName() + public function getName(): string { return self::POLYGON; } diff --git a/src/Eloquent/Builder.php b/src/Eloquent/Builder.php index 6230ed17..e31d2064 100755 --- a/src/Eloquent/Builder.php +++ b/src/Eloquent/Builder.php @@ -18,7 +18,7 @@ public function update(array $values) return parent::update($values); } - protected function asWKT(GeometryInterface $geometry) + protected function asWKT(GeometryInterface $geometry): SpatialExpression { return new SpatialExpression($geometry); } diff --git a/src/Eloquent/SpatialExpression.php b/src/Eloquent/SpatialExpression.php index 9224af0f..3b09d446 100644 --- a/src/Eloquent/SpatialExpression.php +++ b/src/Eloquent/SpatialExpression.php @@ -6,7 +6,7 @@ class SpatialExpression extends Expression { - public function getValue() + public function getValue($expression) { return "ST_GeomFromText(?, ?, 'axis-order=long-lat')"; } diff --git a/src/Eloquent/SpatialTrait.php b/src/Eloquent/SpatialTrait.php index 5cc3f4b1..5382bb25 100755 --- a/src/Eloquent/SpatialTrait.php +++ b/src/Eloquent/SpatialTrait.php @@ -8,6 +8,7 @@ use Grimzy\LaravelMysqlSpatial\Types\Geometry; use Grimzy\LaravelMysqlSpatial\Types\GeometryInterface; use Illuminate\Database\Eloquent\Builder as EloquentBuilder; +use JetBrains\PhpStorm\Pure; /** * Trait SpatialTrait. @@ -40,9 +41,9 @@ trait SpatialTrait * protected $spatialFields = []; */ - public $geometries = []; + public array $geometries = []; - protected $stRelations = [ + protected array $stRelations = [ 'within', 'crosses', 'contains', @@ -53,7 +54,7 @@ trait SpatialTrait 'touches', ]; - protected $stOrderFunctions = [ + protected array $stOrderFunctions = [ 'distance', 'distance_sphere', ]; @@ -65,12 +66,12 @@ trait SpatialTrait * * @return \Grimzy\LaravelMysqlSpatial\Eloquent\Builder */ - public function newEloquentBuilder($query) + #[Pure] public function newEloquentBuilder($query): Builder { return new Builder($query); } - protected function newBaseQueryBuilder() + protected function newBaseQueryBuilder(): BaseBuilder { $connection = $this->getConnection(); @@ -81,7 +82,7 @@ protected function newBaseQueryBuilder() ); } - protected function performInsert(EloquentBuilder $query, array $options = []) + protected function performInsert(EloquentBuilder $query, array $options = []): bool { foreach ($this->attributes as $key => $value) { if ($value instanceof GeometryInterface) { @@ -121,7 +122,7 @@ public function getSpatialFields() } } - public function isColumnAllowed($geometryColumn) + public function isColumnAllowed($geometryColumn): bool { if (!in_array($geometryColumn, $this->getSpatialFields())) { throw new SpatialFieldsNotDefinedException(); @@ -157,7 +158,7 @@ public function scopeDistanceExcludingSelf($query, $geometryColumn, $geometry, $ return $query; } - public function scopeDistanceValue($query, $geometryColumn, $geometry) + public function scopeDistanceValue($query, $geometryColumn, $geometry): void { $this->isColumnAllowed($geometryColumn); @@ -200,7 +201,7 @@ public function scopeDistanceSphereExcludingSelf($query, $geometryColumn, $geome return $query; } - public function scopeDistanceSphereValue($query, $geometryColumn, $geometry) + public function scopeDistanceSphereValue($query, $geometryColumn, $geometry): void { $this->isColumnAllowed($geometryColumn); diff --git a/src/MysqlConnection.php b/src/MysqlConnection.php index 38a2b1a4..1864088a 100644 --- a/src/MysqlConnection.php +++ b/src/MysqlConnection.php @@ -2,14 +2,29 @@ namespace Grimzy\LaravelMysqlSpatial; +use Doctrine\DBAL\Exception; use Doctrine\DBAL\Types\Type as DoctrineType; use Grimzy\LaravelMysqlSpatial\Schema\Builder; use Grimzy\LaravelMysqlSpatial\Schema\Grammars\MySqlGrammar; +use Illuminate\Database\Grammar; use Illuminate\Database\MySqlConnection as IlluminateMySqlConnection; +use Illuminate\Database\Schema\MySqlBuilder; class MysqlConnection extends IlluminateMySqlConnection { - public function __construct($pdo, $database = '', $tablePrefix = '', array $config = []) + /** + * @param $pdo + * @param string $database + * @param string $tablePrefix + * @param array $config + * @throws Exception + */ + public function __construct( + $pdo, + string $database = '', + string $tablePrefix = '', + array $config = [] + ) { parent::__construct($pdo, $database, $tablePrefix, $config); @@ -36,9 +51,9 @@ public function __construct($pdo, $database = '', $tablePrefix = '', array $conf /** * Get the default schema grammar instance. * - * @return \Illuminate\Database\Grammar + * @return Grammar */ - protected function getDefaultSchemaGrammar() + protected function getDefaultSchemaGrammar(): Grammar { return $this->withTablePrefix(new MySqlGrammar()); } @@ -46,9 +61,9 @@ protected function getDefaultSchemaGrammar() /** * Get a schema builder instance for the connection. * - * @return \Illuminate\Database\Schema\MySqlBuilder + * @return MySqlBuilder */ - public function getSchemaBuilder() + public function getSchemaBuilder(): MySqlBuilder|Builder { if (is_null($this->schemaGrammar)) { $this->useDefaultSchemaGrammar(); diff --git a/src/Schema/Blueprint.php b/src/Schema/Blueprint.php index 0a333f06..7d998714 100644 --- a/src/Schema/Blueprint.php +++ b/src/Schema/Blueprint.php @@ -3,6 +3,7 @@ namespace Grimzy\LaravelMysqlSpatial\Schema; use Illuminate\Database\Schema\Blueprint as IlluminateBlueprint; +use Illuminate\Support\Fluent; class Blueprint extends IlluminateBlueprint { @@ -12,9 +13,9 @@ class Blueprint extends IlluminateBlueprint * @param string $column * @param null|int $srid * - * @return \Illuminate\Support\Fluent + * @return Fluent */ - public function geometry($column, $srid = null) + public function geometry($column, $srid = null): Fluent { return $this->addColumn('geometry', $column, compact('srid')); } @@ -25,9 +26,9 @@ public function geometry($column, $srid = null) * @param string $column * @param null|int $srid * - * @return \Illuminate\Support\Fluent + * @return Fluent */ - public function point($column, $srid = null) + public function point($column, $srid = null): Fluent { return $this->addColumn('point', $column, compact('srid')); } @@ -38,9 +39,9 @@ public function point($column, $srid = null) * @param string $column * @param null|int $srid * - * @return \Illuminate\Support\Fluent + * @return Fluent */ - public function lineString($column, $srid = null) + public function lineString($column, $srid = null): Fluent { return $this->addColumn('linestring', $column, compact('srid')); } @@ -51,9 +52,9 @@ public function lineString($column, $srid = null) * @param string $column * @param null|int $srid * - * @return \Illuminate\Support\Fluent + * @return Fluent */ - public function polygon($column, $srid = null) + public function polygon($column, $srid = null): Fluent { return $this->addColumn('polygon', $column, compact('srid')); } @@ -64,9 +65,9 @@ public function polygon($column, $srid = null) * @param string $column * @param null|int $srid * - * @return \Illuminate\Support\Fluent + * @return Fluent */ - public function multiPoint($column, $srid = null) + public function multiPoint($column, $srid = null): Fluent { return $this->addColumn('multipoint', $column, compact('srid')); } @@ -77,9 +78,9 @@ public function multiPoint($column, $srid = null) * @param string $column * @param null|int $srid * - * @return \Illuminate\Support\Fluent + * @return Fluent */ - public function multiLineString($column, $srid = null) + public function multiLineString($column, $srid = null): Fluent { return $this->addColumn('multilinestring', $column, compact('srid')); } @@ -90,9 +91,9 @@ public function multiLineString($column, $srid = null) * @param string $column * @param null|int $srid * - * @return \Illuminate\Support\Fluent + * @return Fluent */ - public function multiPolygon($column, $srid = null) + public function multiPolygon($column, $srid = null): Fluent { return $this->addColumn('multipolygon', $column, compact('srid')); } @@ -103,9 +104,9 @@ public function multiPolygon($column, $srid = null) * @param string $column * @param null|int $srid * - * @return \Illuminate\Support\Fluent + * @return Fluent */ - public function geometryCollection($column, $srid = null) + public function geometryCollection($column, $srid = null): Fluent { return $this->addColumn('geometrycollection', $column, compact('srid')); } @@ -116,9 +117,9 @@ public function geometryCollection($column, $srid = null) * @param string|array $columns * @param string $name * - * @return \Illuminate\Support\Fluent + * @return Fluent */ - public function spatialIndex($columns, $name = null) + public function spatialIndex($columns, $name = null): Fluent { return $this->indexCommand('spatial', $columns, $name); } @@ -128,9 +129,9 @@ public function spatialIndex($columns, $name = null) * * @param string|array $index * - * @return \Illuminate\Support\Fluent + * @return Fluent */ - public function dropSpatialIndex($index) + public function dropSpatialIndex($index): Fluent { return $this->dropIndexCommand('dropIndex', 'spatial', $index); } diff --git a/src/Schema/Builder.php b/src/Schema/Builder.php index baf8dc58..14bc8b58 100644 --- a/src/Schema/Builder.php +++ b/src/Schema/Builder.php @@ -15,7 +15,7 @@ class Builder extends MySqlBuilder * * @return Blueprint */ - protected function createBlueprint($table, Closure $callback = null) + protected function createBlueprint($table, Closure $callback = null): Blueprint { return new Blueprint($table, $callback); } diff --git a/src/Schema/Grammars/MySqlGrammar.php b/src/Schema/Grammars/MySqlGrammar.php index 9afe4513..2508dfbf 100644 --- a/src/Schema/Grammars/MySqlGrammar.php +++ b/src/Schema/Grammars/MySqlGrammar.php @@ -25,7 +25,7 @@ public function __construct() * * @return string */ - public function typeGeometry(Fluent $column) + public function typeGeometry(Fluent $column): string { return 'GEOMETRY'; } @@ -37,7 +37,7 @@ public function typeGeometry(Fluent $column) * * @return string */ - public function typePoint(Fluent $column) + public function typePoint(Fluent $column): string { return 'POINT'; } @@ -49,7 +49,7 @@ public function typePoint(Fluent $column) * * @return string */ - public function typeLinestring(Fluent $column) + public function typeLinestring(Fluent $column): string { return 'LINESTRING'; } @@ -61,7 +61,7 @@ public function typeLinestring(Fluent $column) * * @return string */ - public function typePolygon(Fluent $column) + public function typePolygon(Fluent $column): string { return 'POLYGON'; } @@ -73,7 +73,7 @@ public function typePolygon(Fluent $column) * * @return string */ - public function typeMultipoint(Fluent $column) + public function typeMultipoint(Fluent $column): string { return 'MULTIPOINT'; } @@ -85,7 +85,7 @@ public function typeMultipoint(Fluent $column) * * @return string */ - public function typeMultilinestring(Fluent $column) + public function typeMultilinestring(Fluent $column): string { return 'MULTILINESTRING'; } @@ -97,7 +97,7 @@ public function typeMultilinestring(Fluent $column) * * @return string */ - public function typeMultipolygon(Fluent $column) + public function typeMultipolygon(Fluent $column): string { return 'MULTIPOLYGON'; } @@ -109,7 +109,7 @@ public function typeMultipolygon(Fluent $column) * * @return string */ - public function typeGeometrycollection(Fluent $column) + public function typeGeometrycollection(Fluent $column): string { return 'GEOMETRYCOLLECTION'; } @@ -122,7 +122,7 @@ public function typeGeometrycollection(Fluent $column) * * @return string */ - public function compileSpatial(Blueprint $blueprint, Fluent $command) + public function compileSpatial(Blueprint $blueprint, Fluent $command): string { return $this->compileKey($blueprint, $command, 'spatial'); } @@ -135,9 +135,9 @@ public function compileSpatial(Blueprint $blueprint, Fluent $command) * * @return string|null */ - protected function modifySrid(\Illuminate\Database\Schema\Blueprint $blueprint, Fluent $column) + protected function modifySrid(\Illuminate\Database\Schema\Blueprint $blueprint, Fluent $column): ?string { - if (!is_null($column->srid) && is_int($column->srid) && $column->srid > 0) { + if (!empty($column->srid) && is_int($column->srid) && $column->srid > 0) { return ' srid '.$column->srid; } } diff --git a/src/SpatialServiceProvider.php b/src/SpatialServiceProvider.php index 3b859f8e..7dc04032 100644 --- a/src/SpatialServiceProvider.php +++ b/src/SpatialServiceProvider.php @@ -2,6 +2,7 @@ namespace Grimzy\LaravelMysqlSpatial; +use Doctrine\DBAL\Exception; use Doctrine\DBAL\Types\Type as DoctrineType; use Grimzy\LaravelMysqlSpatial\Connectors\ConnectionFactory; use Grimzy\LaravelMysqlSpatial\Doctrine\Geometry; @@ -24,8 +25,9 @@ class SpatialServiceProvider extends DatabaseServiceProvider * Register the service provider. * * @return void + * @throws Exception */ - public function register() + public function register(): void { // The connection factory is used to create the actual connection instances on // the database. We will inject the factory into the manager so that it may diff --git a/src/Types/Factory.php b/src/Types/Factory.php index ed04ac2d..57baa5d1 100755 --- a/src/Types/Factory.php +++ b/src/Types/Factory.php @@ -4,42 +4,42 @@ class Factory implements \GeoIO\Factory { - public function createPoint($dimension, array $coordinates, $srid = null) + public function createPoint($dimension, array $coordinates, $srid = null): Point { return new Point($coordinates['y'], $coordinates['x'], $srid); } - public function createLineString($dimension, array $points, $srid = null) + public function createLineString($dimension, array $points, $srid = null): LineString { return new LineString($points, $srid); } - public function createLinearRing($dimension, array $points, $srid = null) + public function createLinearRing($dimension, array $points, $srid = null): LineString { return new LineString($points, $srid); } - public function createPolygon($dimension, array $lineStrings, $srid = null) + public function createPolygon($dimension, array $lineStrings, $srid = null): Polygon { return new Polygon($lineStrings, $srid); } - public function createMultiPoint($dimension, array $points, $srid = null) + public function createMultiPoint($dimension, array $points, $srid = null): MultiPoint { return new MultiPoint($points, $srid); } - public function createMultiLineString($dimension, array $lineStrings, $srid = null) + public function createMultiLineString($dimension, array $lineStrings, $srid = null): MultiLineString { return new MultiLineString($lineStrings, $srid); } - public function createMultiPolygon($dimension, array $polygons, $srid = null) + public function createMultiPolygon($dimension, array $polygons, $srid = null): MultiPolygon { return new MultiPolygon($polygons, $srid); } - public function createGeometryCollection($dimension, array $geometries, $srid = null) + public function createGeometryCollection($dimension, array $geometries, $srid = null): GeometryCollection { return new GeometryCollection($geometries, $srid); } diff --git a/src/Types/Geometry.php b/src/Types/Geometry.php index f840874c..894f358a 100644 --- a/src/Types/Geometry.php +++ b/src/Types/Geometry.php @@ -9,7 +9,7 @@ abstract class Geometry implements GeometryInterface, Jsonable, \JsonSerializable { - protected static $wkb_types = [ + protected static array $wkb_types = [ 1 => Point::class, 2 => LineString::class, 3 => Polygon::class, @@ -19,14 +19,14 @@ abstract class Geometry implements GeometryInterface, Jsonable, \JsonSerializabl 7 => GeometryCollection::class, ]; - protected $srid; + protected int $srid; public function __construct($srid = 0) { $this->srid = (int) $srid; } - public function getSrid() + public function getSrid(): int { return $this->srid; } @@ -36,7 +36,7 @@ public function setSrid($srid) $this->srid = (int) $srid; } - public static function getWKTArgument($value) + public static function getWKTArgument($value): string { $left = strpos($value, '('); $right = strrpos($value, ')'); @@ -44,32 +44,24 @@ public static function getWKTArgument($value) return substr($value, $left + 1, $right - $left - 1); } - public static function getWKTClass($value) + public static function getWKTClass($value): string { $left = strpos($value, '('); $type = trim(substr($value, 0, $left)); - switch (strtoupper($type)) { - case 'POINT': - return Point::class; - case 'LINESTRING': - return LineString::class; - case 'POLYGON': - return Polygon::class; - case 'MULTIPOINT': - return MultiPoint::class; - case 'MULTILINESTRING': - return MultiLineString::class; - case 'MULTIPOLYGON': - return MultiPolygon::class; - case 'GEOMETRYCOLLECTION': - return GeometryCollection::class; - default: - throw new UnknownWKTTypeException('Type was '.$type); - } + return match (strtoupper($type)) { + 'POINT' => Point::class, + 'LINESTRING' => LineString::class, + 'POLYGON' => Polygon::class, + 'MULTIPOINT' => MultiPoint::class, + 'MULTILINESTRING' => MultiLineString::class, + 'MULTIPOLYGON' => MultiPolygon::class, + 'GEOMETRYCOLLECTION' => GeometryCollection::class, + default => throw new UnknownWKTTypeException('Type was ' . $type), + }; } - public static function fromWKB($wkb) + public static function fromWKB($wkb): Geometry { $srid = substr($wkb, 0, 4); $srid = unpack('L', $srid)[1]; @@ -113,7 +105,7 @@ public static function fromJson($geoJson) return $type::fromJson($geoJson); } - public function toJson($options = 0) + public function toJson($options = 0): bool|string { return json_encode($this, $options); } diff --git a/src/Types/GeometryCollection.php b/src/Types/GeometryCollection.php index 35f093f7..12f4639d 100755 --- a/src/Types/GeometryCollection.php +++ b/src/Types/GeometryCollection.php @@ -11,6 +11,7 @@ use Illuminate\Contracts\Support\Arrayable; use InvalidArgumentException; use IteratorAggregate; +use ReturnTypeWillChange; class GeometryCollection extends Geometry implements IteratorAggregate, ArrayAccess, Arrayable, Countable { @@ -19,29 +20,29 @@ class GeometryCollection extends Geometry implements IteratorAggregate, ArrayAcc * * @var int */ - protected $minimumCollectionItems = 0; + protected int $minimumCollectionItems = 0; /** * The class of the items in the collection. * * @var string */ - protected $collectionItemType = GeometryInterface::class; + protected string $collectionItemType = GeometryInterface::class; /** * The items contained in the spatial collection. * * @var GeometryInterface[] */ - protected $items = []; + protected array $items = []; /** * @param GeometryInterface[] $geometries - * @param int $srid + * @param int $srid * * @throws InvalidArgumentException */ - public function __construct(array $geometries, $srid = 0) + public function __construct(array $geometries, int $srid = 0) { parent::__construct($srid); @@ -50,12 +51,12 @@ public function __construct(array $geometries, $srid = 0) $this->items = $geometries; } - public function getGeometries() + public function getGeometries(): array { return $this->items; } - public function toWKT() + public function toWKT(): string { return sprintf('GEOMETRYCOLLECTION(%s)', (string) $this); } @@ -67,42 +68,48 @@ public function __toString() }, $this->items)); } - public static function fromString($wktArgument, $srid = 0) + public static function fromString($wktArgument, $srid = 0): static { if (empty($wktArgument)) { return new static([]); } - $geometry_strings = preg_split('/,\s*(?=[A-Za-z])/', $wktArgument); + $geometryStrings = preg_split('/,\s*(?=[A-Za-z])/', $wktArgument); - return new static(array_map(function ($geometry_string) { - $klass = Geometry::getWKTClass($geometry_string); + return new static( + array_map( + function ($geometryString) { + $klass = Geometry::getWKTClass($geometryString); - return call_user_func($klass.'::fromWKT', $geometry_string); - }, $geometry_strings), $srid); + return call_user_func($klass.'::fromWKT', $geometryString); + }, + $geometryStrings + ), + $srid + ); } - public function toArray() + public function toArray(): array { return $this->items; } - public function getIterator() + #[ReturnTypeWillChange] public function getIterator() { return new ArrayIterator($this->items); } - public function offsetExists($offset) + #[ReturnTypeWillChange] public function offsetExists($offset) { return isset($this->items[$offset]); } - public function offsetGet($offset) + #[ReturnTypeWillChange] public function offsetGet($offset) { return $this->offsetExists($offset) ? $this->items[$offset] : null; } - public function offsetSet($offset, $value) + #[ReturnTypeWillChange] public function offsetSet($offset, $value) { $this->validateItemType($value); @@ -113,17 +120,17 @@ public function offsetSet($offset, $value) } } - public function offsetUnset($offset) + #[ReturnTypeWillChange] public function offsetUnset($offset) { unset($this->items[$offset]); } - public function count() + #[ReturnTypeWillChange] public function count() { return count($this->items); } - public static function fromJson($geoJson) + public static function fromJson($geoJson): GeometryCollection { if (is_string($geoJson)) { $geoJson = GeoJson::jsonUnserialize(json_decode($geoJson)); @@ -146,7 +153,7 @@ public static function fromJson($geoJson) * * @return \GeoJson\Geometry\GeometryCollection */ - public function jsonSerialize() + #[ReturnTypeWillChange] public function jsonSerialize() { $geometries = []; foreach ($this->items as $geometry) { diff --git a/src/Types/LineString.php b/src/Types/LineString.php index 1cc4a410..6a6f000a 100644 --- a/src/Types/LineString.php +++ b/src/Types/LineString.php @@ -13,21 +13,21 @@ class LineString extends PointCollection * * @var int */ - protected $minimumCollectionItems = 2; + protected int $minimumCollectionItems = 2; - public function toWKT() + public function toWKT(): string { return sprintf('LINESTRING(%s)', $this->toPairList()); } - public static function fromWkt($wkt, $srid = 0) + public static function fromWkt($wkt, $srid = 0): LineString|static { $wktArgument = Geometry::getWKTArgument($wkt); return static::fromString($wktArgument, $srid); } - public static function fromString($wktArgument, $srid = 0) + public static function fromString($wktArgument, $srid = 0): static { $pairs = explode(',', trim($wktArgument)); $points = array_map(function ($pair) { @@ -42,7 +42,7 @@ public function __toString() return $this->toPairList(); } - public static function fromJson($geoJson) + public static function fromJson($geoJson): GeometryCollection { if (is_string($geoJson)) { $geoJson = GeoJson::jsonUnserialize(json_decode($geoJson)); @@ -65,7 +65,7 @@ public static function fromJson($geoJson) * * @return \GeoJson\Geometry\LineString */ - public function jsonSerialize() + public function jsonSerialize(): GeoJsonLineString { $points = []; foreach ($this->items as $point) { diff --git a/src/Types/MultiLineString.php b/src/Types/MultiLineString.php index 62c4d576..4078a1f9 100644 --- a/src/Types/MultiLineString.php +++ b/src/Types/MultiLineString.php @@ -13,26 +13,26 @@ class MultiLineString extends GeometryCollection * * @var int */ - protected $minimumCollectionItems = 1; + protected int $minimumCollectionItems = 1; /** * The class of the items in the collection. * * @var string */ - protected $collectionItemType = LineString::class; + protected string $collectionItemType = LineString::class; - public function getLineStrings() + public function getLineStrings(): array { return $this->items; } - public function toWKT() + public function toWKT(): string { return sprintf('MULTILINESTRING(%s)', (string) $this); } - public static function fromString($wktArgument, $srid = 0) + public static function fromString($wktArgument, $srid = 0): static { $str = preg_split('/\)\s*,\s*\(/', substr(trim($wktArgument), 1, -1)); $lineStrings = array_map(function ($data) { @@ -56,7 +56,7 @@ public function offsetSet($offset, $value) parent::offsetSet($offset, $value); } - public static function fromJson($geoJson) + public static function fromJson($geoJson): GeometryCollection { if (is_string($geoJson)) { $geoJson = GeoJson::jsonUnserialize(json_decode($geoJson)); @@ -83,7 +83,7 @@ public static function fromJson($geoJson) * * @return \GeoJson\Geometry\MultiLineString */ - public function jsonSerialize() + public function jsonSerialize(): GeoJsonMultiLineString { $lineStrings = []; diff --git a/src/Types/MultiPoint.php b/src/Types/MultiPoint.php index 752967eb..e499482e 100644 --- a/src/Types/MultiPoint.php +++ b/src/Types/MultiPoint.php @@ -13,21 +13,21 @@ class MultiPoint extends PointCollection * * @var int */ - protected $minimumCollectionItems = 1; + protected int $minimumCollectionItems = 1; - public function toWKT() + public function toWKT(): string { return sprintf('MULTIPOINT(%s)', (string) $this); } - public static function fromWkt($wkt, $srid = 0) + public static function fromWkt($wkt, $srid = 0): MultiPoint|static { $wktArgument = Geometry::getWKTArgument($wkt); return static::fromString($wktArgument, $srid); } - public static function fromString($wktArgument, $srid = 0) + public static function fromString($wktArgument, $srid = 0): static { $matches = []; preg_match_all('/\(\s*(\d+\s+\d+)\s*\)/', trim($wktArgument), $matches); @@ -46,7 +46,7 @@ public function __toString() }, $this->items)); } - public static function fromJson($geoJson) + public static function fromJson($geoJson): GeometryCollection { if (is_string($geoJson)) { $geoJson = GeoJson::jsonUnserialize(json_decode($geoJson)); @@ -69,7 +69,7 @@ public static function fromJson($geoJson) * * @return \GeoJson\Geometry\MultiPoint */ - public function jsonSerialize() + public function jsonSerialize(): GeoJsonMultiPoint { $points = []; foreach ($this->items as $point) { diff --git a/src/Types/MultiPolygon.php b/src/Types/MultiPolygon.php index cdea3a9c..cf7420d0 100644 --- a/src/Types/MultiPolygon.php +++ b/src/Types/MultiPolygon.php @@ -13,16 +13,16 @@ class MultiPolygon extends GeometryCollection * * @var int */ - protected $minimumCollectionItems = 1; + protected int $minimumCollectionItems = 1; /** * The class of the items in the collection. * * @var string */ - protected $collectionItemType = Polygon::class; + protected string $collectionItemType = Polygon::class; - public function toWKT() + public function toWKT(): string { return sprintf('MULTIPOLYGON(%s)', (string) $this); } @@ -34,14 +34,20 @@ public function __toString() }, $this->items)); } - public static function fromString($wktArgument, $srid = 0) + public static function fromString($wktArgument, $srid = 0): static { $parts = preg_split('/(\)\s*\)\s*,\s*\(\s*\()/', $wktArgument, -1, PREG_SPLIT_DELIM_CAPTURE); $polygons = static::assembleParts($parts); - return new static(array_map(function ($polygonString) { - return Polygon::fromString($polygonString); - }, $polygons), $srid); + return new static( + array_map( + function ($polygonString) { + return Polygon::fromString($polygonString); + }, + $polygons + ), + $srid + ); } /** @@ -49,7 +55,7 @@ public static function fromString($wktArgument, $srid = 0) * * @return array|Polygon[] */ - public function getPolygons() + public function getPolygons(): array { return $this->items; } @@ -71,7 +77,7 @@ public function getPolygons() * * @return array */ - protected static function assembleParts(array $parts) + protected static function assembleParts(array $parts): array { $polygons = []; $count = count($parts); @@ -96,7 +102,7 @@ public function offsetSet($offset, $value) parent::offsetSet($offset, $value); } - public static function fromJson($geoJson) + public static function fromJson($geoJson): GeometryCollection { if (is_string($geoJson)) { $geoJson = GeoJson::jsonUnserialize(json_decode($geoJson)); @@ -127,7 +133,7 @@ public static function fromJson($geoJson) * * @return \GeoJson\Geometry\MultiPolygon */ - public function jsonSerialize() + public function jsonSerialize(): GeoJsonMultiPolygon { $polygons = []; foreach ($this->items as $polygon) { diff --git a/src/Types/Point.php b/src/Types/Point.php index d424ec5e..4aae1a9d 100644 --- a/src/Types/Point.php +++ b/src/Types/Point.php @@ -5,12 +5,13 @@ use GeoJson\GeoJson; use GeoJson\Geometry\Point as GeoJsonPoint; use Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException; +use ReturnTypeWillChange; class Point extends Geometry { - protected $lat; + protected float $lat; - protected $lng; + protected float $lng; public function __construct($lat, $lng, $srid = 0) { @@ -20,7 +21,7 @@ public function __construct($lat, $lng, $srid = 0) $this->lng = (float) $lng; } - public function getLat() + public function getLat(): float { return $this->lat; } @@ -30,7 +31,7 @@ public function setLat($lat) $this->lat = (float) $lat; } - public function getLng() + public function getLng(): float { return $this->lng; } @@ -40,24 +41,24 @@ public function setLng($lng) $this->lng = (float) $lng; } - public function toPair() + public function toPair(): string { return $this->getLng().' '.$this->getLat(); } - public static function fromPair($pair, $srid = 0) + public static function fromPair($pair, $srid = 0): static { list($lng, $lat) = explode(' ', trim($pair, "\t\n\r \x0B()")); return new static((float) $lat, (float) $lng, (int) $srid); } - public function toWKT() + public function toWKT(): string { return sprintf('POINT(%s)', (string) $this); } - public static function fromString($wktArgument, $srid = 0) + public static function fromString($wktArgument, $srid = 0): static { return static::fromPair($wktArgument, $srid); } @@ -72,7 +73,7 @@ public function __toString() * * @return \Grimzy\LaravelMysqlSpatial\Types\Point */ - public static function fromJson($geoJson) + public static function fromJson($geoJson): Point { if (is_string($geoJson)) { $geoJson = GeoJson::jsonUnserialize(json_decode($geoJson)); @@ -92,7 +93,7 @@ public static function fromJson($geoJson) * * @return \GeoJson\Geometry\Point */ - public function jsonSerialize() + #[ReturnTypeWillChange] public function jsonSerialize() { return new GeoJsonPoint([$this->getLng(), $this->getLat()]); } diff --git a/src/Types/PointCollection.php b/src/Types/PointCollection.php index 30d1b8de..63e588dc 100755 --- a/src/Types/PointCollection.php +++ b/src/Types/PointCollection.php @@ -12,9 +12,9 @@ abstract class PointCollection extends GeometryCollection * * @var string */ - protected $collectionItemType = Point::class; + protected string $collectionItemType = Point::class; - public function toPairList() + public function toPairList(): string { return implode(',', array_map(function (Point $point) { return $point->toPair(); @@ -31,7 +31,7 @@ public function offsetSet($offset, $value) /** * @return array|\Grimzy\LaravelMysqlSpatial\Types\Point[] */ - public function getPoints() + public function getPoints(): array { return $this->items; } diff --git a/src/Types/Polygon.php b/src/Types/Polygon.php index 9c10cecc..ecafa3ae 100644 --- a/src/Types/Polygon.php +++ b/src/Types/Polygon.php @@ -8,12 +8,12 @@ class Polygon extends MultiLineString { - public function toWKT() + public function toWKT(): string { return sprintf('POLYGON(%s)', (string) $this); } - public static function fromJson($geoJson) + public static function fromJson($geoJson): static { if (is_string($geoJson)) { $geoJson = GeoJson::jsonUnserialize(json_decode($geoJson)); @@ -40,7 +40,7 @@ public static function fromJson($geoJson) * * @return \GeoJson\Geometry\Polygon */ - public function jsonSerialize() + public function jsonSerialize(): static { $linearRings = []; foreach ($this->items as $lineString) { From 92be53c25c70f39f7e7bf633841066f5a487b3ac Mon Sep 17 00:00:00 2001 From: Dominique Vienne Date: Tue, 7 Mar 2023 16:53:26 +0100 Subject: [PATCH 3/4] Documentation update --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 39d5303c..d9037f9f 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,9 @@ Please check the documentation for your MySQL version. MySQL's Extension for Spa - `1.x.x`: MySQL 5.6 (also supports MySQL 5.5 but not all spatial analysis functions) - `2.x.x`: MySQL 5.7 and 8.0 (Laravel version < 8.0) - `3.x.x`: MySQL 8.0 with SRID support (Laravel version < 8.0) -- **`4.x.x`: MySQL 8.0 with SRID support (Laravel 8+) [Current branch]** +- **`4.x.x`: MySQL 8.0 with SRID support (Laravel 8.x, 9.x) [Current branch]** - `5.x.x`: MySQL 5.7 and 8.0 (Laravel 8+) +- `6.x.x`: MySQL 8.0 with SRID support (Laravel 10+) This package also works with MariaDB. Please refer to the [MySQL/MariaDB Spatial Support Matrix](https://mariadb.com/kb/en/library/mysqlmariadb-spatial-support-matrix/) for compatibility. @@ -27,6 +28,9 @@ Add the package using composer: ```sh $ composer require grimzy/laravel-mysql-spatial:^4.0 +# for Laravel version 10.x +$ composer require grimzy/laravel-mysql-spatial:^6.0 + # or for Laravel version < 8.0 $ composer require grimzy/laravel-mysql-spatial:^3.0 ``` From 49e529692bee8720a2747f981e2da58f16667b50 Mon Sep 17 00:00:00 2001 From: Dominique Vienne Date: Wed, 8 Mar 2023 08:30:33 +0100 Subject: [PATCH 4/4] Bugfix: no return value when srid id not provided --- src/Schema/Grammars/MySqlGrammar.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Schema/Grammars/MySqlGrammar.php b/src/Schema/Grammars/MySqlGrammar.php index 2508dfbf..dd808fa9 100644 --- a/src/Schema/Grammars/MySqlGrammar.php +++ b/src/Schema/Grammars/MySqlGrammar.php @@ -140,5 +140,7 @@ protected function modifySrid(\Illuminate\Database\Schema\Blueprint $blueprint, if (!empty($column->srid) && is_int($column->srid) && $column->srid > 0) { return ' srid '.$column->srid; } + + return null; } }