Skip to content

Commit

Permalink
v1 pint format and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
asanikovich committed Jun 1, 2023
1 parent dea6aa4 commit 1cb21d3
Show file tree
Hide file tree
Showing 36 changed files with 118 additions and 90 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/pest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@ on: [ push, pull_request ]

jobs:
test:
name: Pest - PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }} - DB ${{ matrix.db }} ${{ matrix.dependency-version }}
name: Pest PHP${{ matrix.php }} Laravel ${{ matrix.laravel }} ${{ matrix.db }} ${{ matrix.dependency-version }}

runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
php: [ 8.2, 8.1 ]
laravel: [ 9.* ]
laravel: [ 9.*, 8.* ]
db: [ 'mysql:8.0', 'mysql:5.7', 'mariadb:10.9' ]
dependency-version: [ prefer-lowest, prefer-stable ]
include:
- laravel: 9.*
testbench: ^7.0
- laravel: 8.*
testbench: ^6.23

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 "orchestra/testbench:${{ matrix.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, 8.0 ]
php: [ 8.2, 8.1 ]

steps:
- name: Checkout code
Expand Down
60 changes: 45 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

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

The latest version, v3, supports Laravel 10 and PHP 8.1+. For Laravel 8 or 9, and PHP 8.0, use v2.
v1 supports Laravel 8,9 and PHP 8.1+.

This package supports MySQL v8, MySQL v5.7, and MariaDB v10.
This package supports MySQL v8 or v5.7, and MariaDB v10.

## Getting Started

Expand All @@ -21,12 +21,32 @@ You can install the package via composer:
composer require asanikovich/laravel-spatial
```

### Configuration

Default Configuration file includes geometry types mapping:
```php
<?php

use ASanikovich\LaravelSpatial\Enums\GeometryType;
use ASanikovich\LaravelSpatial\Geometry;

return [
GeometryType::POINT->value => Geometry\Point::class,
GeometryType::POLYGON->value => Geometry\Polygon::class,
/// ...
];
```

You can publish the config file with:

```bash
php artisan vendor:publish --tag="laravel-spatial-config"
```

If you want you can override custom geometry types mapping:
* globally by config file
* by custom `$casts` in your model (top priority)

### Setting Up Your First Model

1. First, generate a new model along with a migration file by running:
Expand Down Expand Up @@ -67,7 +87,7 @@ php artisan vendor:publish --tag="laravel-spatial-config"
php artisan migrate
```

4. In your new model, fill the `$fillable` and `$casts` arrays and use the `HasSpatial` trait:
4. In your new model, fill `$casts` arrays and use the `HasSpatial` trait (fill the `$fillable` - optional):

```php
namespace App\Models;
Expand All @@ -81,7 +101,6 @@ php artisan vendor:publish --tag="laravel-spatial-config"
/**
* @property Point $location
* @property Polygon $area
* @method static SpatialBuilder<Place> query()
*/
class Place extends Model
{
Expand Down Expand Up @@ -155,10 +174,6 @@ echo $vacationCity->area->toJson(); // {"type":"Polygon","coordinates":[[[41.907

For more comprehensive documentation on the API, please refer to the [API](API.md) page.

## Tips for Improving IDE Support

In order to get better IDE support, you can add a `query` method phpDoc annotation to your model:

```php
/**
* @method static SpatialBuilder query()
Expand All @@ -169,11 +184,10 @@ class Place extends Model
}
```

Create queries only with the `query()` static method:
Create queries only with scopes methods:

```php
Place::query()->whereDistance(...); // This is IDE-friendly
Place::whereDistance(...); // This is not
Place::whereDistance(...); // This is IDE-friendly
```

## Extension
Expand Down Expand Up @@ -207,10 +221,22 @@ echo $londonEyePoint->getName(); // Point

Here are some useful commands for development:

* Run tests: `composer pest`
* Run tests with coverage: `composer pest-coverage`
* Perform type checking: `composer phpstan`
* Format your code: `composer format`
Run tests:
```bash
composer run test
```
Run tests with coverage:
```bash
composer run test-coverage
```
Perform type checking:
```bash
composer run phpstan
```
Format your code:
```bash
composer run format
```

## Updates and Changes

Expand All @@ -219,3 +245,7 @@ For details on updates and changes, please refer to our [CHANGELOG](CHANGELOG.md
## License

Laravel Eloquent Spatial is released under The MIT License (MIT). For more information, please see our [License File](LICENSE.md).

## Credits

Originally inspired from [MatanYadaev's laravel-eloquent-spatial package](https://github.com/MatanYadaev/laravel-eloquent-spatial).
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
}
],
"require": {
"php": "^8.0",
"php": "^8.1",
"ext-json": "*",
"ext-pdo": "*",
"laravel/framework": "^9.0",
"laravel/framework": "^9.0|^8.0",
"phayes/geophp": "^1.2"
},
"require-dev": {
"doctrine/dbal": "^2.0|^3.0",
"laravel/pint": "^1.10",
"doctrine/dbal": "^3.0",
"laravel/pint": "^1.5",
"nunomaduro/larastan": "^1.0|^2.4",
"orchestra/testbench": "^7.0",
"pestphp/pest": "^1.0",
Expand Down
2 changes: 1 addition & 1 deletion src/Doctrine/GeometryCollectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace ASanikovich\LaravelSpatial\Doctrine;

use ASanikovich\LaravelSpatial\Enums\GeometryType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use ASanikovich\LaravelSpatial\Enums\GeometryType;

final class GeometryCollectionType extends Type
{
Expand Down
2 changes: 1 addition & 1 deletion src/Doctrine/LineStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace ASanikovich\LaravelSpatial\Doctrine;

use ASanikovich\LaravelSpatial\Enums\GeometryType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use ASanikovich\LaravelSpatial\Enums\GeometryType;

final class LineStringType extends Type
{
Expand Down
2 changes: 1 addition & 1 deletion src/Doctrine/MultiLineStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace ASanikovich\LaravelSpatial\Doctrine;

use ASanikovich\LaravelSpatial\Enums\GeometryType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use ASanikovich\LaravelSpatial\Enums\GeometryType;

final class MultiLineStringType extends Type
{
Expand Down
2 changes: 1 addition & 1 deletion src/Doctrine/MultiPointType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace ASanikovich\LaravelSpatial\Doctrine;

use ASanikovich\LaravelSpatial\Enums\GeometryType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use ASanikovich\LaravelSpatial\Enums\GeometryType;

final class MultiPointType extends Type
{
Expand Down
2 changes: 1 addition & 1 deletion src/Doctrine/MultiPolygonType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace ASanikovich\LaravelSpatial\Doctrine;

use ASanikovich\LaravelSpatial\Enums\GeometryType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use ASanikovich\LaravelSpatial\Enums\GeometryType;

final class MultiPolygonType extends Type
{
Expand Down
2 changes: 1 addition & 1 deletion src/Doctrine/PointType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace ASanikovich\LaravelSpatial\Doctrine;

use ASanikovich\LaravelSpatial\Enums\GeometryType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use ASanikovich\LaravelSpatial\Enums\GeometryType;

final class PointType extends Type
{
Expand Down
2 changes: 1 addition & 1 deletion src/Doctrine/PolygonType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace ASanikovich\LaravelSpatial\Doctrine;

use ASanikovich\LaravelSpatial\Enums\GeometryType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use ASanikovich\LaravelSpatial\Enums\GeometryType;

final class PolygonType extends Type
{
Expand Down
3 changes: 1 addition & 2 deletions src/Eloquent/GeometryCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

namespace ASanikovich\LaravelSpatial\Eloquent;

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Connection;
use ASanikovich\LaravelSpatial\Exceptions\LaravelSpatialException;
use ASanikovich\LaravelSpatial\Geometry\Geometry;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Query\Expression;

class GeometryCast implements CastsAttributes
Expand Down
6 changes: 3 additions & 3 deletions src/Eloquent/HasSpatial.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function scopeWithDistance(
Expression|Geometry|string $geometryOrColumn,
string $alias = 'distance'
): Builder {
if (is_null($query->getQuery()->columns)) {
if (empty($query->getQuery()->columns)) {
$query->select();
}

Expand All @@ -51,7 +51,7 @@ public function scopeWithDistanceSphere(
Expression|Geometry|string $geometryOrColumn,
string $alias = 'distance'
): Builder {
if (is_null($query->getQuery()->columns)) {
if (empty($query->getQuery()->columns)) {
$query->select();
}

Expand Down 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;
}
}
2 changes: 1 addition & 1 deletion src/Enums/GeometryType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace ASanikovich\LaravelSpatial\Enums;

use Doctrine\DBAL\Types\Type;
use ASanikovich\LaravelSpatial\Doctrine;
use ASanikovich\LaravelSpatial\Geometry;
use Doctrine\DBAL\Types\Type;

enum GeometryType: string
{
Expand Down
11 changes: 5 additions & 6 deletions src/Geometry/Geometry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace ASanikovich\LaravelSpatial\Geometry;

use ASanikovich\LaravelSpatial\Database\Connection;
use ASanikovich\LaravelSpatial\Eloquent\GeometryCast;
use ASanikovich\LaravelSpatial\Exceptions\LaravelSpatialException;
use ASanikovich\LaravelSpatial\Exceptions\LaravelSpatialJsonException;
use geoPHP;
use Illuminate\Contracts\Database\Eloquent\Castable;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
Expand All @@ -15,10 +19,6 @@
use Illuminate\Support\Traits\Macroable;
use JsonException;
use JsonSerializable;
use ASanikovich\LaravelSpatial\Database\Connection;
use ASanikovich\LaravelSpatial\Eloquent\GeometryCast;
use ASanikovich\LaravelSpatial\Exceptions\LaravelSpatialException;
use ASanikovich\LaravelSpatial\Exceptions\LaravelSpatialJsonException;
use Stringable;
use Throwable;
use WKB as geoPHPWkb;
Expand All @@ -45,8 +45,7 @@ public function toJson($options = 0): string
{
try {
return json_encode($this, $options | JSON_THROW_ON_ERROR);
}
catch (JsonException $e) { // @codeCoverageIgnore
} catch (JsonException $e) { // @codeCoverageIgnore
throw new LaravelSpatialJsonException($e->getMessage(), previous: $e); // @codeCoverageIgnore
} // @codeCoverageIgnore
}
Expand Down
2 changes: 1 addition & 1 deletion src/Geometry/GeometryCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace ASanikovich\LaravelSpatial\Geometry;

use ArrayAccess;
use ASanikovich\LaravelSpatial\Exceptions\LaravelSpatialException;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use ASanikovich\LaravelSpatial\Exceptions\LaravelSpatialException;

class GeometryCollection extends Geometry implements ArrayAccess
{
Expand Down
4 changes: 2 additions & 2 deletions src/Geometry/GeometryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace ASanikovich\LaravelSpatial\Geometry;

use ASanikovich\LaravelSpatial\Enums\GeometryType;
use ASanikovich\LaravelSpatial\Exceptions\LaravelSpatialException;
use Geometry as geoPHPGeometry;
use GeometryCollection as geoPHPGeometryCollection;
use geoPHP;
use LineString as geoPHPLineString;
use ASanikovich\LaravelSpatial\Enums\GeometryType;
use ASanikovich\LaravelSpatial\Exceptions\LaravelSpatialException;
use MultiLineString as geoPHPMultiLineString;
use MultiPoint as geoPHPMultiPoint;
use MultiPolygon as geoPHPMultiPolygon;
Expand Down
2 changes: 1 addition & 1 deletion src/Geometry/MultiLineString.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace ASanikovich\LaravelSpatial\Geometry;

use Illuminate\Support\Collection;
use ASanikovich\LaravelSpatial\Exceptions\LaravelSpatialException;
use Illuminate\Support\Collection;

/**
* @property Collection<int, LineString> $geometries
Expand Down
2 changes: 1 addition & 1 deletion src/Geometry/MultiPolygon.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace ASanikovich\LaravelSpatial\Geometry;

use Illuminate\Support\Collection;
use ASanikovich\LaravelSpatial\Exceptions\LaravelSpatialException;
use Illuminate\Support\Collection;

/**
* @property Collection<int, Polygon> $geometries
Expand Down
2 changes: 1 addition & 1 deletion src/Geometry/PointCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace ASanikovich\LaravelSpatial\Geometry;

use Illuminate\Support\Collection;
use ASanikovich\LaravelSpatial\Exceptions\LaravelSpatialException;
use Illuminate\Support\Collection;

/**
* @property Collection<int, Point> $geometries
Expand Down

0 comments on commit 1cb21d3

Please sign in to comment.