Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MatanYadaev committed Jul 24, 2024
1 parent f62b5dd commit f16e304
Show file tree
Hide file tree
Showing 23 changed files with 262 additions and 101 deletions.
4 changes: 3 additions & 1 deletion .run/Test - MySQL 8.0.run.xml → .run/Test - MySQL.run.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Test - MySQL 8.0" type="PestRunConfigurationType">
<configuration default="false" name="Test - MySQL" type="PestRunConfigurationType">
<CommandLine>
<envs>
<env name="DB_COLLATION" value="utf8mb4_unicode_ci"/>
<env name="DB_CONNECTION" value="mysql"/>
<env name="DB_PORT" value="3307"/>
</envs>
</CommandLine>
Expand Down
12 changes: 0 additions & 12 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,6 @@

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

## v4.2.1 - 2024-04-02

### What's Changed

* `Geometry::fromArray` added optional`$srid` paramter by @ju-gow in https://github.com/MatanYadaev/laravel-eloquent-spatial/pull/118

### New Contributors

* @ju-gow made their first contribution in https://github.com/MatanYadaev/laravel-eloquent-spatial/pull/118

**Full Changelog**: https://github.com/MatanYadaev/laravel-eloquent-spatial/compare/4.2.0...4.2.1

## v4.2.0 - 2024-03-13

### What's Changed
Expand Down
96 changes: 57 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,90 +152,108 @@ For more comprehensive documentation on the API, please refer to the [API](API.m
## Extension
### Extend Geometry class with macros
You can add new methods to the `Geometry` class through macros.
Here's an example of how to register a macro in your service provider's `boot` method:
```php
class AppServiceProvider extends ServiceProvider
{
public function boot(): void
{
Geometry::macro('getName', function (): string {
/** @var Geometry $this */
return class_basename($this);
});
}
}
```
Use the method in your code:
```php
$londonEyePoint = new Point(51.5032973, -0.1217424);
echo $londonEyePoint->getName(); // Point
```
### Extend with custom geometry classes
You can extend the geometry classes by creating custom geometry classes and add or overwrite certain functionality.
You can extend the geometry classes by creating custom geometry classes and add functionality. You can also override existing methods, although it is not recommended, as it may lead to unexpected behavior.
Here's an example how to overwrite the `toArray()` method by adding array keys to the `coordinates` array and removing the `type` key.
1. Create a custom geometry class that extends the base geometry class.
```php
use MatanYadaev\EloquentSpatial\Objects\Point as EloquentSpatialPoint;
use MatanYadaev\EloquentSpatial\Objects\Point;
class Point extends EloquentSpatialPoint
class ExtendedPoint extends Point
{
public function toArray(): array
public function toCustomArray(): array
{
return [
'coordinates' => [
'longitude' => $this->longitude,
'latitude' => $this->latitude,
],
];
return 'coordinates' => [
'latitude' => $this->latitude,
'longitude' => $this->longitude
]
}
}
```
All we have to do is tell the package that we want to use are our custom Point class, we can do this in the AppServiceProvider under the `boot()` method.
2. Update the geometry class mapping in a service provider file.
```php
use App\ValueObjects\Point;
use App\ValueObjects\ExtendedPoint;
use Illuminate\Support\ServiceProvider;
use MatanYadaev\EloquentSpatial\EloquentSpatial;
class AppServiceProvider extends ServiceProvider
{
public function boot(): void
{
EloquentSpatial::usePoint(Point::class);
EloquentSpatial::usePoint(ExtendedPoint::class);
}
}
```
Update your model to use the custom geometry class in the `casts()` method
3. Update your model to use the custom geometry class in the `$casts` property or `casts()` method.
```php
use App\ValueObjects\Point;
use App\ValueObjects\ExtendedPoint;
use Illuminate\Database\Eloquent\Model;
use MatanYadaev\EloquentSpatial\Traits\HasSpatial;
class Location extends Model
class Place extends Model
{
use HasSpatial;
protected $casts = [
'coordinates' => ExtendedPoint::class,
];
// Or:
protected function casts(): array
{
return [
'coordinates' => Point::class,
'coordinates' => ExtendedPoint::class,
];
}
}
```
### Extend Geometry class with macros
You can add new methods to the `Geometry` class through macros.
Here's an example of how to register a macro in your service provider's `boot` method:
4. Use the custom geometry class in your code.
```php
class AppServiceProvider extends ServiceProvider
{
public function boot(): void
{
Geometry::macro('getName', function (): string {
/** @var Geometry $this */
return class_basename($this);
});
}
}
```
use App\Models\Location;
use App\ValueObjects\ExtendedPoint;
Use the method in your code:
```php
$londonEyePoint = new Point(51.5032973, -0.1217424);
$place = Place::create([
'name' => 'London Eye',
'coordinates' => new ExtendedPoint(51.5032973, -0.1217424),
]);
echo $londonEyePoint->getName(); // Point
echo $place->coordinates->toCustomArray(); // ['longitude' => -0.1217424, 'latitude' => 51.5032973]
```
## Development
Expand Down
28 changes: 14 additions & 14 deletions src/EloquentSpatial.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,29 @@

class EloquentSpatial
{
/** @var class-string<\MatanYadaev\EloquentSpatial\Objects\GeometryCollection> */
/** @var class-string<GeometryCollection> */
public static string $geometryCollection = GeometryCollection::class;

/** @var class-string<\MatanYadaev\EloquentSpatial\Objects\LineString> */
/** @var class-string<LineString> */
public static string $lineString = LineString::class;

/** @var class-string<\MatanYadaev\EloquentSpatial\Objects\MultiLineString> */
/** @var class-string<MultiLineString> */
public static string $multiLineString = MultiLineString::class;

/** @var class-string<\MatanYadaev\EloquentSpatial\Objects\MultiPoint> */
/** @var class-string<MultiPoint> */
public static string $multiPoint = MultiPoint::class;

/** @var class-string<\MatanYadaev\EloquentSpatial\Objects\MultiPolygon> */
/** @var class-string<MultiPolygon> */
public static string $multiPolygon = MultiPolygon::class;

/** @var class-string<\MatanYadaev\EloquentSpatial\Objects\Point> */
/** @var class-string<Point> */
public static string $point = Point::class;

/** @var class-string<\MatanYadaev\EloquentSpatial\Objects\Polygon> */
/** @var class-string<Polygon> */
public static string $polygon = Polygon::class;

/**
* @param class-string<\MatanYadaev\EloquentSpatial\Objects\GeometryCollection> $class
* @param class-string<GeometryCollection> $class
*/
public static function useGeometryCollection(string $class): string
{
Expand All @@ -46,7 +46,7 @@ public static function useGeometryCollection(string $class): string
}

/**
* @param class-string<\MatanYadaev\EloquentSpatial\Objects\LineString> $class
* @param class-string<LineString> $class
*/
public static function useLineString(string $class): string
{
Expand All @@ -56,7 +56,7 @@ public static function useLineString(string $class): string
}

/**
* @param class-string<\MatanYadaev\EloquentSpatial\Objects\MultiLineString> $class
* @param class-string<MultiLineString> $class
*/
public static function useMultiLineString(string $class): string
{
Expand All @@ -66,7 +66,7 @@ public static function useMultiLineString(string $class): string
}

/**
* @param class-string<\MatanYadaev\EloquentSpatial\Objects\MultiPoint> $class
* @param class-string<MultiPoint> $class
*/
public static function useMultiPoint(string $class): string
{
Expand All @@ -76,7 +76,7 @@ public static function useMultiPoint(string $class): string
}

/**
* @param class-string<\MatanYadaev\EloquentSpatial\Objects\MultiPolygon> $class
* @param class-string<MultiPolygon> $class
*/
public static function useMultiPolygon(string $class): string
{
Expand All @@ -86,7 +86,7 @@ public static function useMultiPolygon(string $class): string
}

/**
* @param class-string<\MatanYadaev\EloquentSpatial\Objects\Point> $class
* @param class-string<Point> $class
*/
public static function usePoint(string $class): string
{
Expand All @@ -96,7 +96,7 @@ public static function usePoint(string $class): string
}

/**
* @param class-string<\MatanYadaev\EloquentSpatial\Objects\Polygon> $class
* @param class-string<Polygon> $class
*/
public static function usePolygon(string $class): string
{
Expand Down
2 changes: 1 addition & 1 deletion src/GeometryCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function set($model, string $key, $value, array $attributes): ?Expression
return $value;
}

if (! ($value instanceof $this->className)) {
if (! ($value instanceof $this->className) || get_class($value) !== $this->className) {
$geometryType = is_object($value) ? $value::class : gettype($value);
throw new InvalidArgumentException(
sprintf('Expected %s, %s given.', $this->className, $geometryType)
Expand Down
4 changes: 3 additions & 1 deletion src/GeometryExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
/** @codeCoverageIgnore */
class GeometryExpression
{
public function __construct(readonly private string $expression) {}
public function __construct(readonly private string $expression)
{
}

public function normalize(ConnectionInterface $connection): string
{
Expand Down
6 changes: 3 additions & 3 deletions tests/HasSpatialTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@
});

it('toExpressionString can handle a Expression input', function (): void {
$spatialBuilder = new TestPlace;
$spatialBuilder = new TestPlace();
$toExpressionStringMethod = (new ReflectionClass($spatialBuilder))->getMethod('toExpressionString');

$result = $toExpressionStringMethod->invoke($spatialBuilder, DB::raw('POINT(longitude, latitude)'));
Expand All @@ -456,7 +456,7 @@
});

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

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

it('toExpressionString can handle a string input', function (): void {
$spatialBuilder = new TestPlace;
$spatialBuilder = new TestPlace();
$toExpressionStringMethod = (new ReflectionClass($spatialBuilder))->getMethod('toExpressionString');

$result = $toExpressionStringMethod->invoke($spatialBuilder, 'test_places.point');
Expand Down
36 changes: 30 additions & 6 deletions tests/Objects/GeometryCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@
it('adds a macro toGeometryCollection', function (): void {
Geometry::macro('getName', function (): string {
/** @var Geometry $this */
// @phpstan-ignore-next-line
return class_basename($this);

Check failure on line 490 in tests/Objects/GeometryCollectionTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3)

No error to ignore is reported on line 490.

Check failure on line 490 in tests/Objects/GeometryCollectionTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2)

No error to ignore is reported on line 490.
});

Expand All @@ -507,8 +508,8 @@
});

it('uses an extended GeometryCollection class', function (): void {
// Arrange
EloquentSpatial::useGeometryCollection(ExtendedGeometryCollection::class);

$geometryCollection = new ExtendedGeometryCollection([
new Polygon([
new LineString([
Expand All @@ -522,18 +523,18 @@
new Point(0, 180),
], 4326);

// Act
/** @var TestExtendedPlace $testPlace */
$testPlace = TestExtendedPlace::factory()->create(['geometry_collection' => $geometryCollection])->fresh();

expect($testPlace->geometry_collection)
->toBeInstanceOf(ExtendedGeometryCollection::class)
->and($testPlace->geometry_collection)
->toEqual($geometryCollection);
// Assert
expect($testPlace->geometry_collection)->toBeInstanceOf(ExtendedGeometryCollection::class);
expect($testPlace->geometry_collection)->toEqual($geometryCollection);
});

it('throws exception when storing a record with regular GeometryCollection instead of the extended one', function (): void {
// Arrange
EloquentSpatial::useGeometryCollection(ExtendedGeometryCollection::class);

$geometryCollection = new GeometryCollection([
new Polygon([
new LineString([
Expand All @@ -547,7 +548,30 @@
new Point(0, 180),
], 4326);

// Act & Assert
expect(function () use ($geometryCollection): void {
TestExtendedPlace::factory()->create(['geometry_collection' => $geometryCollection]);
})->toThrow(InvalidArgumentException::class);
});

it('throws exception when storing a record with extended GeometryCollection instead of the regular one', function (): void {
// Arrange
EloquentSpatial::useGeometryCollection(ExtendedGeometryCollection::class);
$geometryCollection = new ExtendedGeometryCollection([
new Polygon([
new LineString([
new Point(0, 180),
new Point(1, 179),
new Point(2, 178),
new Point(3, 177),
new Point(0, 180),
]),
]),
new Point(0, 180),
], 4326);

// Act & Assert
expect(function () use ($geometryCollection): void {
TestPlace::factory()->create(['geometry_collection' => $geometryCollection]);
})->toThrow(InvalidArgumentException::class);
});
Loading

0 comments on commit f16e304

Please sign in to comment.