Skip to content

Commit

Permalink
Add HasSpatial trait (#75)
Browse files Browse the repository at this point in the history
* Reusable trait rather than adding a method

* corrections from PR review
- update readme instruction
- restore phpdoc ide hint
- trailing newline for phpcs

* Add HasSpatial trait #75
- typo & cs-fixer fix

* Update README.md

Co-authored-by: Matan Yadaev <[email protected]>
  • Loading branch information
lukevi and MatanYadaev authored Jan 9, 2023
1 parent 9360923 commit b00b225
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Run the migration:
php artisan migrate
```

Fill the `$fillable` and `$casts` arrays and add custom eloquent builder to your new model:
Fill the `$fillable` and `$casts` arrays and use the `HasSpatial` trait in your new model:

```php
namespace App\Models;
Expand All @@ -65,6 +65,7 @@ use Illuminate\Database\Eloquent\Model;
use MatanYadaev\EloquentSpatial\SpatialBuilder;
use MatanYadaev\EloquentSpatial\Objects\Point;
use MatanYadaev\EloquentSpatial\Objects\Polygon;
use MatanYadaev\EloquentSpatial\Traits\HasSpatial;

/**
* @property Point $location
Expand All @@ -73,6 +74,8 @@ use MatanYadaev\EloquentSpatial\Objects\Polygon;
*/
class Place extends Model
{
use HasSpatial;

protected $fillable = [
'name',
'location',
Expand All @@ -83,11 +86,6 @@ class Place extends Model
'location' => Point::class,
'area' => Polygon::class,
];

public function newEloquentBuilder($query): SpatialBuilder
{
return new SpatialBuilder($query);
}
}
```

Expand Down
13 changes: 13 additions & 0 deletions src/Traits/HasSpatial.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace MatanYadaev\EloquentSpatial\Traits;

use MatanYadaev\EloquentSpatial\SpatialBuilder;

trait HasSpatial
{
public function newEloquentBuilder($query): SpatialBuilder
{
return new SpatialBuilder($query);
}
}
13 changes: 2 additions & 11 deletions tests/TestModels/TestPlace.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use MatanYadaev\EloquentSpatial\Objects\Polygon;
use MatanYadaev\EloquentSpatial\SpatialBuilder;
use MatanYadaev\EloquentSpatial\Tests\TestFactories\TestPlaceFactory;
use MatanYadaev\EloquentSpatial\Traits\HasSpatial;

/**
* @property Point $point
Expand All @@ -30,7 +31,7 @@
*/
class TestPlace extends Model
{
use HasFactory;
use HasFactory, HasSpatial;

protected $fillable = [
'address',
Expand All @@ -55,16 +56,6 @@ class TestPlace extends Model
'point_with_line_string_cast' => LineString::class,
];

/**
* @param $query
* @return SpatialBuilder<TestPlace>
*/
public function newEloquentBuilder($query): SpatialBuilder
{
// @phpstan-ignore-next-line
return new SpatialBuilder($query);
}

protected static function newFactory(): TestPlaceFactory
{
return new TestPlaceFactory;
Expand Down

0 comments on commit b00b225

Please sign in to comment.