medialibrary-helper/tests/Models/Post.php

55 lines
1.8 KiB
PHP

<?php
namespace Zoomyboy\MedialibraryHelper\Tests\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Str;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaStored;
class Post extends Model implements HasMedia
{
use InteractsWithMedia;
public $guarded = [];
public function registerMediaCollections(): void
{
$this->addMediaCollection('defaultSingleFile')->singleFile();
$this->addMediaCollection('images');
$this->addMediaCollection('singleForced')->singleFile()->forceFileName(function ($name) {
return $name.' '.now()->format('Y-m-d');
});
$this->addMediaCollection('multipleForced')->forceFileName(function ($name) {
return $name.' '.now()->format('Y-m-d');
});
$this->addMediaCollection('singleStoringHook')->singleFile()->storing(function ($adder, $fileName) {
return $adder->withCustomProperties([
'use' => 'AAA',
'ttt' => pathinfo($fileName, PATHINFO_FILENAME),
]);
});
$this->addMediaCollection('singleWithEvent')->singleFile()->stored(function (Media $media) {
Event::dispatch(new MediaStored($media));
});
$this->addMediaCollection('multipleFilesWithEvent')->stored(function (Media $media) {
Event::dispatch(new MediaStored($media));
});
$this->addMediaCollection('multipleProperties')->singleFile()->withDefaultProperties(fn ($path) => [
'test' => Str::camel($path),
])->withPropertyValidation(fn ($path) => [
'test' => 'string|max:10',
]);
}
}