From facbeb94d82812a9b107ba9d2d9e1a0ec2796c4e Mon Sep 17 00:00:00 2001 From: Chris Page Date: Fri, 31 May 2024 17:04:01 +0100 Subject: [PATCH] Changed to Event::dispatch() Added convenience helper to event --- src/EloquentModelSortedEvent.php | 11 +++++++++++ src/SortableTrait.php | 25 ++++++++++++++++--------- tests/SortableTest.php | 2 +- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/EloquentModelSortedEvent.php b/src/EloquentModelSortedEvent.php index b3cbb0b..3c78bec 100644 --- a/src/EloquentModelSortedEvent.php +++ b/src/EloquentModelSortedEvent.php @@ -2,6 +2,8 @@ namespace Spatie\EloquentSortable; +use Illuminate\Database\Eloquent\Model; + class EloquentModelSortedEvent { public string $model; @@ -10,4 +12,13 @@ public function __construct(string $model) { $this->model = $model; } + + public function isFor(Model|string $model): bool + { + if (is_string($model)) { + return $model === $this->model; + } + + return get_class($model) === $this->model; + } } diff --git a/src/SortableTrait.php b/src/SortableTrait.php index ddc780f..40ce734 100644 --- a/src/SortableTrait.php +++ b/src/SortableTrait.php @@ -5,6 +5,7 @@ use ArrayAccess; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\SoftDeletingScope; +use Illuminate\Support\Facades\Event; use InvalidArgumentException; trait SortableTrait @@ -27,12 +28,12 @@ public function setHighestOrderNumber(): void public function getHighestOrderNumber(): int { - return (int) $this->buildSortQuery()->max($this->determineOrderColumnName()); + return (int)$this->buildSortQuery()->max($this->determineOrderColumnName()); } public function getLowestOrderNumber(): int { - return (int) $this->buildSortQuery()->min($this->determineOrderColumnName()); + return (int)$this->buildSortQuery()->min($this->determineOrderColumnName()); } public function scopeOrdered(Builder $query, string $direction = 'asc') @@ -40,9 +41,13 @@ public function scopeOrdered(Builder $query, string $direction = 'asc') return $query->orderBy($this->determineOrderColumnName(), $direction); } - public static function setNewOrder($ids, int $startOrder = 1, string $primaryKeyColumn = null, callable $modifyQuery = null): void - { - if (! is_array($ids) && ! $ids instanceof ArrayAccess) { + public static function setNewOrder( + $ids, + int $startOrder = 1, + string $primaryKeyColumn = null, + callable $modifyQuery = null + ): void { + if (!is_array($ids) && !$ids instanceof ArrayAccess) { throw new InvalidArgumentException('You must pass an array or ArrayAccess object to setNewOrder'); } @@ -67,7 +72,7 @@ public static function setNewOrder($ids, int $startOrder = 1, string $primaryKey ->update([$orderColumnName => $startOrder++]); } - event(new EloquentModelSortedEvent(static::class)); + Event::dispatch(new EloquentModelSortedEvent(static::class)); if (config('eloquent-sortable.ignore_timestamps', false)) { static::$ignoreTimestampsOn = array_values(array_diff(static::$ignoreTimestampsOn, [static::class])); @@ -101,7 +106,7 @@ public function moveOrderDown(): static ->where($orderColumnName, '>', $this->$orderColumnName) ->first(); - if (! $swapWithModel) { + if (!$swapWithModel) { return $this; } @@ -117,7 +122,7 @@ public function moveOrderUp(): static ->where($orderColumnName, '<', $this->$orderColumnName) ->first(); - if (! $swapWithModel) { + if (!$swapWithModel) { return $this; } @@ -159,7 +164,9 @@ public function moveToStart(): static $this->$orderColumnName = $firstModel->$orderColumnName; $this->save(); - $this->buildSortQuery()->where($this->getQualifiedKeyName(), '!=', $this->getKey())->increment($orderColumnName); + $this->buildSortQuery()->where($this->getQualifiedKeyName(), '!=', $this->getKey())->increment( + $orderColumnName + ); return $this; } diff --git a/tests/SortableTest.php b/tests/SortableTest.php index d0a6d6e..11ad2ff 100644 --- a/tests/SortableTest.php +++ b/tests/SortableTest.php @@ -47,7 +47,7 @@ public function it_can_set_a_new_order() } Event::assertDispatched(EloquentModelSortedEvent::class, function (EloquentModelSortedEvent $event) { - return $event->model === Dummy::class; + return $event->isFor(Dummy::class); }); }