Skip to content

Commit

Permalink
v2: laravel 10 init
Browse files Browse the repository at this point in the history
  • Loading branch information
asanikovich committed Jun 2, 2023
1 parent 0034ff3 commit 6d16c51
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 66 deletions.
8 changes: 3 additions & 5 deletions .github/workflows/pest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ jobs:
fail-fast: false
matrix:
php: [ 8.2, 8.1 ]
laravel: [ 9.*, 8.* ]
laravel: [ 10.* ]
db: [ 'mysql:8.0', 'mysql:5.7', 'mariadb:10.9' ]
dependency-version: [ prefer-stable ]
include:
- laravel: 9.*
testbench: ^7.0
- laravel: 8.*
testbench: ^6.23
- laravel: 10.*
testbench: ^8.0

services:
db:
Expand Down
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `laravel-spatial` will be documented in this file.

## v2.0.0 - 2023-06-02

Initial release for laravel 10

## v1.0.0 - 2023-06-02

Initial release
Initial release for laravel 8,9
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

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

v1 supports Laravel 8,9 and PHP 8.1+.
* v2 supports Laravel 10+ 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
8 changes: 4 additions & 4 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": "^8.0",
"laravel/framework": "^10.0",
"phayes/geophp": "^1.2"
},
"require-dev": {
"doctrine/dbal": "^3.0",
"laravel/pint": "^1.5",
"nunomaduro/larastan": "^1.0|^2.4",
"orchestra/testbench": "^6.23|^7.0",
"pestphp/pest": "^1.0",
"pestphp/pest-plugin-laravel": "^1.4.0"
"orchestra/testbench": "^8.0",
"pestphp/pest": "^1.0|^2.6",
"pestphp/pest-plugin-laravel": "^1.0|^2.0"
},
"autoload": {
"psr-4": {
Expand Down
7 changes: 0 additions & 7 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,3 @@ parameters:
level: max
checkMissingIterableValueType: true
checkGenericClassInNonGenericObjectType: false
ignoreErrors:
-
message: '#^Call to an undefined method Pest\\Expectation\|Pest\\Support\\Extendable\:\:.+\(\)\.$#'
path: tests/*.php
-
message: '#^Access to an undefined property Pest\\Expectation\|Pest\\Support\\Extendable\:\:\$.+\.$#'
path: tests/*.php
50 changes: 26 additions & 24 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,31 @@
colors="true"
processIsolation="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.0/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd"
>
<testsuites>
<testsuite name="Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
<report>
<html outputDirectory="build/coverage"/>
<text outputFile="build/coverage.txt"/>
<clover outputFile="build/logs/clover.xml"/>
</report>
</coverage>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
<php>
<env name="DB_DATABASE" value="laravel"/>
<env name="DB_HOST" value="127.0.0.1"/>
<env name="DB_USERNAME" value="root"/>
</php>
<testsuites>
<testsuite name="Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage>
<report>
<html outputDirectory="build/coverage"/>
<text outputFile="build/coverage.txt"/>
<clover outputFile="build/logs/clover.xml"/>
</report>
</coverage>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
<php>
<env name="DB_DATABASE" value="laravel"/>
<env name="DB_HOST" value="127.0.0.1"/>
<env name="DB_USERNAME" value="root"/>
</php>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
29 changes: 17 additions & 12 deletions src/Eloquent/GeometryCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use ASanikovich\LaravelSpatial\Exceptions\LaravelSpatialException;
use ASanikovich\LaravelSpatial\Geometry\Geometry;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Query\Expression;
use Illuminate\Contracts\Database\Query\Expression;
use Illuminate\Database\Connection;
use Illuminate\Database\Eloquent\Model;

class GeometryCast implements CastsAttributes
{
Expand All @@ -26,15 +28,15 @@ public function __construct(string $className)
* @param string|Expression|null $value
* @param array<string, mixed> $attributes
*/
public function get($model, string $key, mixed $value, array $attributes): ?Geometry
public function get(Model $model, string $key, mixed $value, array $attributes): ?Geometry
{
if (! $value) {
return null;
}

if ($value instanceof Expression) {
$wkt = $this->extractWktFromExpression($value);
$srid = $this->extractSridFromExpression($value);
$wkt = $this->extractWktFromExpression($value, $model->getConnection());
$srid = $this->extractSridFromExpression($value, $model->getConnection());

return $this->className::fromWkt($wkt, $srid);
}
Expand All @@ -48,7 +50,7 @@ public function get($model, string $key, mixed $value, array $attributes): ?Geom
*
* @throws LaravelSpatialException
*/
public function set($model, string $key, mixed $value, array $attributes): Expression|null
public function set(Model $model, string $key, mixed $value, array $attributes): Expression|null
{
if (! $value) {
return null;
Expand All @@ -64,24 +66,27 @@ public function set($model, string $key, mixed $value, array $attributes): Expre

if (! ($value instanceof $this->className)) {
$geometryType = is_object($value) ? $value::class : gettype($value);
throw new LaravelSpatialException(
sprintf('Expected %s, %s given.', static::class, $geometryType) // todo
);

throw new LaravelSpatialException(sprintf('Expected %s, %s given.', static::class, $geometryType));
}

return $value->toSqlExpression($model->getConnection());
}

private function extractWktFromExpression(Expression $expression): string
private function extractWktFromExpression(Expression $expression, Connection $connection): string
{
preg_match('/ST_GeomFromText\(\'(.+)\', .+(, .+)?\)/', (string) $expression, $match);
$expressionValue = $expression->getValue($connection->getQueryGrammar());

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

return $match[1];
}

private function extractSridFromExpression(Expression $expression): int
private function extractSridFromExpression(Expression $expression, Connection $connection): int
{
preg_match('/ST_GeomFromText\(\'.+\', (.+)(, .+)?\)/', (string) $expression, $match);
$expressionValue = $expression->getValue($connection->getQueryGrammar());

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

return (int) $match[1];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Eloquent/HasSpatial.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,6 @@ protected function toSpatialExpressionString(Builder $query, Expression|Geometry
$expression = $query->raw($grammar->wrap($value));
}

return (string) $expression;
return (string) $expression->getValue($grammar);
}
}
2 changes: 1 addition & 1 deletion src/Geometry/Geometry.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
use geoPHP;
use Illuminate\Contracts\Database\Eloquent\Castable;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Contracts\Database\Query\Expression;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\Query\Expression;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Traits\Macroable;
use JsonException;
Expand Down
5 changes: 3 additions & 2 deletions tests/Eloquent/HasSpatialTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
TestPlace::factory()->create(['point' => new Point(0, 0, Srid::WGS84->value)]);

/** @var TestPlace $testPlaceWithDistance */
$testPlaceWithDistance = TestPlace::query()->select(['id'])->selectRaw(DB::raw('id as id_new'))
$testPlaceWithDistance = TestPlace::query()->select(['id'])->selectRaw('id as id_new')
->withDistance('point', new Point(1, 1, Srid::WGS84->value))
->firstOrFail();

Expand Down Expand Up @@ -397,8 +397,9 @@

$result = $method->invoke($model, $model->newQuery(), $polygon);

$grammar = $model->newQuery()->getGrammar();
$connection = $model->newQuery()->getConnection();
$sqlSerializedPolygon = $polygon->toSqlExpression($connection)->getValue();
$sqlSerializedPolygon = $polygon->toSqlExpression($connection)->getValue($grammar);
expect($result)->toBe($sqlSerializedPolygon);
});

Expand Down
6 changes: 4 additions & 2 deletions tests/Geometry/GeometryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@

$expression = $point->toSqlExpression(DB::connection());

$expressionValue = $expression->getValue();
$grammar = DB::getQueryGrammar();
$expressionValue = $expression->getValue($grammar);
expect($expressionValue)->toEqual("ST_GeomFromText('POINT(180 0)', 4326, 'axis-order=long-lat')");
})->skip(fn () => ! isSupportAxisOrder());

Expand All @@ -91,7 +92,8 @@

$expression = $point->toSqlExpression(DB::connection());

$expressionValue = $expression->getValue();
$grammar = DB::getQueryGrammar();
$expressionValue = $expression->getValue($grammar);
expect($expressionValue)->toEqual("ST_GeomFromText('POINT(180 0)', 4326)");
})->skip(fn () => isSupportAxisOrder());

Expand Down
10 changes: 4 additions & 6 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use ASanikovich\LaravelSpatial\Database\Connection;
use ASanikovich\LaravelSpatial\Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTruncation;
use Illuminate\Support\Facades\DB;

Expand All @@ -13,11 +12,10 @@ function isSupportAxisOrder(): bool
return (new Connection())->isSupportAxisOrder(DB::connection());
}

/**
* @return class-string
*/
function getDatabaseTruncationClass(): string
{
if (class_exists(DatabaseTruncation::class)) {
return DatabaseTruncation::class;
}

return DatabaseMigrations::class;
return DatabaseTruncation::class;
}

0 comments on commit 6d16c51

Please sign in to comment.