Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v2.0.2: supports Laravel 11+ and PHP 8.2+ #18

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions .github/workflows/pest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [ 8.2, 8.1 ]
php: [ 8.1, 8.2 ]
laravel: [ 10.* ]
db: [ 'mysql:8.0', 'mysql:5.7', 'mariadb:10.9' ]
dependency-version: [ prefer-stable ]
include:
- laravel: 10.*
testbench: ^8.0
- php: 8.2
laravel: 11.*
- php: 8.3
laravel: 11.*

services:
db:
Expand All @@ -41,7 +43,7 @@ jobs:

- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench" --no-interaction --no-update
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction

- name: Execute tests
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [ 8.2, 8.1 ]
php: [ 8.1, 8.2, 8.3 ]

steps:
- name: Checkout code
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

**This Laravel package allows you to easily work with spatial data types and functions.**

* v2 supports Laravel 10+ and PHP 8.1+
* v2 supports Laravel 10,11 and PHP 8.1+
* v1 supports Laravel 8,9 and PHP 8.1+

This package supports MySQL v8 or v5.7, and MariaDB v10.
Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
"php": "^8.1",
"ext-json": "*",
"ext-pdo": "*",
"laravel/framework": "^10.0",
"laravel/framework": "^10.0|^11.0",
"phayes/geophp": "^1.2"
},
"require-dev": {
"doctrine/dbal": "^3.0",
"doctrine/dbal": "^3.0|^4.1",
"laravel/pint": "^1.5",
"nunomaduro/larastan": "^1.0|^2.4",
"orchestra/testbench": "^8.0",
"pestphp/pest": "^1.0|^2.6",
"pestphp/pest-plugin-laravel": "^1.0|^2.0"
"larastan/larastan": "^1.0|^2.4",
"orchestra/testbench": "^8.0|^9.4",
"pestphp/pest": "^1.0|^2.6|^3.0",
"pestphp/pest-plugin-laravel": "^1.0|^2.0|^3.0"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
includes:
- ./vendor/nunomaduro/larastan/extension.neon
- ./vendor/larastan/larastan/extension.neon
parameters:
paths:
- src
Expand Down
6 changes: 3 additions & 3 deletions src/Eloquent/GeometryCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function get(Model $model, string $key, mixed $value, array $attributes):
*
* @throws LaravelSpatialException
*/
public function set(Model $model, string $key, mixed $value, array $attributes): Expression|null
public function set(Model $model, string $key, mixed $value, array $attributes): ?Expression
{
if (! $value) {
return null;
Expand Down Expand Up @@ -79,7 +79,7 @@ private function extractWktFromExpression(Expression $expression, Connection $co

preg_match('/ST_GeomFromText\(\'(.+)\', .+(, .+)?\)/', (string) $expressionValue, $match);

return $match[1];
return $match[1] ?? '';
}

private function extractSridFromExpression(Expression $expression, Connection $connection): int
Expand All @@ -88,6 +88,6 @@ private function extractSridFromExpression(Expression $expression, Connection $c

preg_match('/ST_GeomFromText\(\'.+\', (.+)(, .+)?\)/', (string) $expressionValue, $match);

return (int) $match[1];
return (int) ($match[1] ?? 0);
}
}
2 changes: 1 addition & 1 deletion src/Eloquent/HasSpatial.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace ASanikovich\LaravelSpatial\Eloquent;

use ASanikovich\LaravelSpatial\Geometry\Geometry;
use Illuminate\Contracts\Database\Query\Expression;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\Expression;

/**
* @method static withDistance(Expression|Geometry|string $column, Expression|Geometry|string $geometryOrColumn, string $alias = 'distance')
Expand Down
4 changes: 1 addition & 3 deletions src/Exceptions/LaravelSpatialException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@

use DomainException;

class LaravelSpatialException extends DomainException
{
}
class LaravelSpatialException extends DomainException {}
4 changes: 1 addition & 3 deletions src/Exceptions/LaravelSpatialJsonException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

namespace ASanikovich\LaravelSpatial\Exceptions;

class LaravelSpatialJsonException extends LaravelSpatialException
{
}
class LaravelSpatialJsonException extends LaravelSpatialException {}
4 changes: 2 additions & 2 deletions src/Geometry/Geometry.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use Throwable;
use WKB as geoPHPWkb;

abstract class Geometry implements Castable, Arrayable, Jsonable, JsonSerializable, Stringable
abstract class Geometry implements Arrayable, Castable, Jsonable, JsonSerializable, Stringable
{
use Macroable;

Expand Down Expand Up @@ -189,7 +189,7 @@ public function toSqlExpression(ConnectionInterface $connection): Expression
{
$wkt = $this->toWkt();

if (! (new Connection())->isSupportAxisOrder($connection)) {
if (! (new Connection)->isSupportAxisOrder($connection)) {
// @codeCoverageIgnoreStart
return DB::raw(sprintf("ST_GeomFromText('%s', %d)", $wkt, $this->srid));
// @codeCoverageIgnoreEnd
Expand Down
38 changes: 22 additions & 16 deletions src/LaravelSpatialServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use ASanikovich\LaravelSpatial\Exceptions\LaravelSpatialException;
use Doctrine\DBAL\Types\Type;
use Illuminate\Database\DatabaseServiceProvider;
use Illuminate\Support\Facades\DB;
use RuntimeException;
use Throwable;

final class LaravelSpatialServiceProvider extends DatabaseServiceProvider
Expand All @@ -28,9 +28,7 @@ public function boot(): void

$this->validateConfig();

if (DB::connection()->isDoctrineAvailable()) {
$this->registerDoctrineTypes();
}
$this->registerDoctrineTypes();
}

/**
Expand All @@ -45,18 +43,6 @@ private function registerDoctrineTypes(): void
$this->registerDoctrineType(GeometryType::GEOMETRY_COLLECTION->getDoctrineClassName(), 'geomcollection');
}

/**
* @param class-string<Type> $class
*
* @throws Throwable
*/
private function registerDoctrineType(string $class, string $type): void
{
DB::registerDoctrineType($class, $type, $type);

DB::connection()->registerDoctrineType($class, $type, $type);
}

/**
* @throws LaravelSpatialException
*/
Expand Down Expand Up @@ -85,4 +71,24 @@ private function validateConfig(): void
}
}
}

private function isDoctrineAvailable(): bool
{
return class_exists('Doctrine\DBAL\Connection');
}

private function registerDoctrineType(Type|string $class, string $name): void
{
if (! $this->isDoctrineAvailable()) {
throw new RuntimeException(
'Registering a custom Doctrine type requires Doctrine DBAL (doctrine/dbal).'
);
}

if (! Type::hasType($name)) {
/** @var Type $type */
$type = is_string($class) ? new $class : $class;
Type::getTypeRegistry()->register($name, $type);
}
}
}
4 changes: 1 addition & 3 deletions tests/Custom/CustomPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@

use ASanikovich\LaravelSpatial\Geometry\Point;

class CustomPoint extends Point
{
}
class CustomPoint extends Point {}
4 changes: 1 addition & 3 deletions tests/Custom/CustomPointConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@

use ASanikovich\LaravelSpatial\Geometry\Point;

class CustomPointConfig extends Point
{
}
class CustomPointConfig extends Point {}
4 changes: 1 addition & 3 deletions tests/Custom/CustomPointInvalid.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

namespace ASanikovich\LaravelSpatial\Tests\Custom;

class CustomPointInvalid
{
}
class CustomPointInvalid {}
6 changes: 3 additions & 3 deletions tests/Eloquent/HasSpatialTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@
});

it('toSpatialExpressionString can handle a Expression input', function (): void {
$model = new TestPlace();
$model = new TestPlace;
$method = (new ReflectionClass(TestPlace::class))->getMethod('toSpatialExpressionString');

$result = $method->invoke($model, $model->newQuery(), DB::raw('POINT(longitude, latitude)'));
Expand All @@ -391,7 +391,7 @@
});

it('toSpatialExpressionString can handle a Geometry input', function (): void {
$model = new TestPlace();
$model = new TestPlace;
$method = (new ReflectionClass(TestPlace::class))->getMethod('toSpatialExpressionString');
$polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}');

Expand All @@ -404,7 +404,7 @@
});

it('toSpatialExpressionString can handle a string input', function (): void {
$model = new TestPlace();
$model = new TestPlace;
$method = (new ReflectionClass(TestPlace::class))->getMethod('toSpatialExpressionString');

$result = $method->invoke($model, $model->newQuery(), 'test_places.point');
Expand Down
1 change: 0 additions & 1 deletion tests/Geometry/GeometryCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,6 @@
it('adds a macro toGeometryCollection', function (): void {
Geometry::macro('getName', function (): string {
/** @var Geometry $this */
// @phpstan-ignore-next-line
return class_basename($this);
});

Expand Down
1 change: 0 additions & 1 deletion tests/Geometry/LineStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@
it('adds a macro toLineString', function (): void {
Geometry::macro('getName', function (): string {
/** @var Geometry $this */
// @phpstan-ignore-next-line
return class_basename($this);
});

Expand Down
1 change: 0 additions & 1 deletion tests/Geometry/MultiLineStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@
it('adds a macro toMultiLineString', function (): void {
Geometry::macro('getName', function (): string {
/** @var Geometry $this */
// @phpstan-ignore-next-line
return class_basename($this);
});

Expand Down
1 change: 0 additions & 1 deletion tests/Geometry/MultiPointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@
it('adds a macro toMultiPoint', function (): void {
Geometry::macro('getName', function (): string {
/** @var Geometry $this */
// @phpstan-ignore-next-line
return class_basename($this);
});

Expand Down
1 change: 0 additions & 1 deletion tests/Geometry/MultiPolygonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,6 @@
it('adds a macro toMultiPolygon', function (): void {
Geometry::macro('getName', function (): string {
/** @var Geometry $this */
// @phpstan-ignore-next-line
return class_basename($this);
});

Expand Down
1 change: 0 additions & 1 deletion tests/Geometry/PointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@
it('adds a macro toPoint', function (): void {
Geometry::macro('getName', function (): string {
/** @var Geometry $this */
// @phpstan-ignore-next-line
return class_basename($this);
});

Expand Down
1 change: 0 additions & 1 deletion tests/Geometry/PolygonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@
it('adds a macro toPolygon', function (): void {
Geometry::macro('getName', function (): string {
/** @var Geometry $this */
// @phpstan-ignore-next-line
return class_basename($this);
});

Expand Down
2 changes: 1 addition & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

function isSupportAxisOrder(): bool
{
return (new Connection())->isSupportAxisOrder(DB::connection());
return (new Connection)->isSupportAxisOrder(DB::connection());
}

/**
Expand Down
Loading