Skip to content

Commit

Permalink
Changed to Event::dispatch()
Browse files Browse the repository at this point in the history
Added convenience helper to event
  • Loading branch information
chrispage1 committed May 31, 2024
1 parent 6c49c89 commit facbeb9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
11 changes: 11 additions & 0 deletions src/EloquentModelSortedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Spatie\EloquentSortable;

use Illuminate\Database\Eloquent\Model;

class EloquentModelSortedEvent
{
public string $model;
Expand All @@ -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;
}
}
25 changes: 16 additions & 9 deletions src/SortableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,22 +28,26 @@ 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')
{
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');
}

Expand All @@ -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]));
Expand Down Expand Up @@ -101,7 +106,7 @@ public function moveOrderDown(): static
->where($orderColumnName, '>', $this->$orderColumnName)
->first();

if (! $swapWithModel) {
if (!$swapWithModel) {
return $this;
}

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

if (! $swapWithModel) {
if (!$swapWithModel) {
return $this;
}

Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/SortableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

Expand Down

0 comments on commit facbeb9

Please sign in to comment.