Skip to content

Commit

Permalink
support reordering media
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Jan 10, 2024
1 parent 18f6a2c commit f8e671f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Finller\Media\Models;

use Closure;
use Finller\Media\Casts\GeneratedConversion;
use Finller\Media\Casts\GeneratedConversions;
use Finller\Media\Enums\MediaType;
Expand All @@ -12,6 +13,7 @@
use Finller\Media\Traits\InteractsWithMediaFiles;
use Illuminate\Database\Eloquent\Casts\ArrayObject;
use Illuminate\Database\Eloquent\Casts\AsArrayObject;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Http\File as HttpFile;
Expand All @@ -35,7 +37,7 @@
* @property ?int $height
* @property ?float $aspect_ratio
* @property ?string $average_color
* @property ?int $order_column
* @property ?string $order
* @property ?Collection<string, GeneratedConversion> $generated_conversions
* @property ?ArrayObject $metadata
* @property ?Model $model
Expand Down Expand Up @@ -404,4 +406,31 @@ public function getSrcset(): Collection
return $this->getResponsiveImages()
->map(fn (GeneratedConversion $generatedConversion) => $generatedConversion->getUrl().' '.$generatedConversion->width.'w');
}

/**
* @param null|(Closure(string $previous): string) $sequence
*/
public static function reorder(array $ids, ?Closure $sequence = null): void
{
/** @var EloquentCollection<int, Media> */
$models = static::query()
->whereIn('id', $ids)
->get();

$models = $models->sortBy(function (Media $model) use ($ids) {
return array_search($model->getKey(), $ids);
});

$previous = $sequence ? null : -1;

foreach ($models as $model) {

$model->order = $sequence ? value($sequence, $previous) : ($previous+1);

Check failure on line 428 in src/Models/Media.php

View workflow job for this annotation

GitHub Actions / phpstan

Property Finller\Media\Models\Media::$order (string|null) does not accept (Closure)|float|int.

$previous = $model->order;

$model->save();
}

}
}
18 changes: 18 additions & 0 deletions tests/Feature/MediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,21 @@
Storage::disk('media')->assertMissing($generatedConversion->path);
Storage::disk('media')->assertMissing($nestedGeneratedConversion->path);
});

it('reorder models', function () {

$first_media = MediaFactory::new()->create(['order' => '0']);
$second_media = MediaFactory::new()->create(['order' => '1']);
$third_media = MediaFactory::new()->create(['order' => '2']);

Media::reorder([
$third_media->getKey(),
$first_media->getKey(),
$second_media->getKey(),
]);

expect($third_media->refresh()->order)->toBe('0');
expect($first_media->refresh()->order)->toBe('1');
expect($second_media->refresh()->order)->toBe('2');

});

0 comments on commit f8e671f

Please sign in to comment.