Skip to content

Commit facbeb9

Browse files
committed
Changed to Event::dispatch()
Added convenience helper to event
1 parent 6c49c89 commit facbeb9

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

src/EloquentModelSortedEvent.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Spatie\EloquentSortable;
44

5+
use Illuminate\Database\Eloquent\Model;
6+
57
class EloquentModelSortedEvent
68
{
79
public string $model;
@@ -10,4 +12,13 @@ public function __construct(string $model)
1012
{
1113
$this->model = $model;
1214
}
15+
16+
public function isFor(Model|string $model): bool
17+
{
18+
if (is_string($model)) {
19+
return $model === $this->model;
20+
}
21+
22+
return get_class($model) === $this->model;
23+
}
1324
}

src/SortableTrait.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use ArrayAccess;
66
use Illuminate\Database\Eloquent\Builder;
77
use Illuminate\Database\Eloquent\SoftDeletingScope;
8+
use Illuminate\Support\Facades\Event;
89
use InvalidArgumentException;
910

1011
trait SortableTrait
@@ -27,22 +28,26 @@ public function setHighestOrderNumber(): void
2728

2829
public function getHighestOrderNumber(): int
2930
{
30-
return (int) $this->buildSortQuery()->max($this->determineOrderColumnName());
31+
return (int)$this->buildSortQuery()->max($this->determineOrderColumnName());
3132
}
3233

3334
public function getLowestOrderNumber(): int
3435
{
35-
return (int) $this->buildSortQuery()->min($this->determineOrderColumnName());
36+
return (int)$this->buildSortQuery()->min($this->determineOrderColumnName());
3637
}
3738

3839
public function scopeOrdered(Builder $query, string $direction = 'asc')
3940
{
4041
return $query->orderBy($this->determineOrderColumnName(), $direction);
4142
}
4243

43-
public static function setNewOrder($ids, int $startOrder = 1, string $primaryKeyColumn = null, callable $modifyQuery = null): void
44-
{
45-
if (! is_array($ids) && ! $ids instanceof ArrayAccess) {
44+
public static function setNewOrder(
45+
$ids,
46+
int $startOrder = 1,
47+
string $primaryKeyColumn = null,
48+
callable $modifyQuery = null
49+
): void {
50+
if (!is_array($ids) && !$ids instanceof ArrayAccess) {
4651
throw new InvalidArgumentException('You must pass an array or ArrayAccess object to setNewOrder');
4752
}
4853

@@ -67,7 +72,7 @@ public static function setNewOrder($ids, int $startOrder = 1, string $primaryKey
6772
->update([$orderColumnName => $startOrder++]);
6873
}
6974

70-
event(new EloquentModelSortedEvent(static::class));
75+
Event::dispatch(new EloquentModelSortedEvent(static::class));
7176

7277
if (config('eloquent-sortable.ignore_timestamps', false)) {
7378
static::$ignoreTimestampsOn = array_values(array_diff(static::$ignoreTimestampsOn, [static::class]));
@@ -101,7 +106,7 @@ public function moveOrderDown(): static
101106
->where($orderColumnName, '>', $this->$orderColumnName)
102107
->first();
103108

104-
if (! $swapWithModel) {
109+
if (!$swapWithModel) {
105110
return $this;
106111
}
107112

@@ -117,7 +122,7 @@ public function moveOrderUp(): static
117122
->where($orderColumnName, '<', $this->$orderColumnName)
118123
->first();
119124

120-
if (! $swapWithModel) {
125+
if (!$swapWithModel) {
121126
return $this;
122127
}
123128

@@ -159,7 +164,9 @@ public function moveToStart(): static
159164
$this->$orderColumnName = $firstModel->$orderColumnName;
160165
$this->save();
161166

162-
$this->buildSortQuery()->where($this->getQualifiedKeyName(), '!=', $this->getKey())->increment($orderColumnName);
167+
$this->buildSortQuery()->where($this->getQualifiedKeyName(), '!=', $this->getKey())->increment(
168+
$orderColumnName
169+
);
163170

164171
return $this;
165172
}

tests/SortableTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function it_can_set_a_new_order()
4747
}
4848

4949
Event::assertDispatched(EloquentModelSortedEvent::class, function (EloquentModelSortedEvent $event) {
50-
return $event->model === Dummy::class;
50+
return $event->isFor(Dummy::class);
5151
});
5252
}
5353

0 commit comments

Comments
 (0)