medialibrary-helper/tests/Models/Post.php

62 lines
2.1 KiB
PHP
Raw Normal View History

2023-03-07 15:31:24 +01:00
<?php
namespace Zoomyboy\MedialibraryHelper\Tests\Models;
use Illuminate\Database\Eloquent\Model;
2023-03-07 21:53:36 +01:00
use Illuminate\Support\Facades\Event;
2023-03-07 23:11:35 +01:00
use Illuminate\Support\Str;
2023-03-07 15:31:24 +01:00
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
2023-03-07 21:53:36 +01:00
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaStored;
2023-03-07 15:31:24 +01:00
class Post extends Model implements HasMedia
{
use InteractsWithMedia;
public $guarded = [];
public function registerMediaCollections(): void
{
$this->addMediaCollection('defaultSingleFile')->singleFile();
2023-03-08 00:58:15 +01:00
$this->addMediaCollection('conversionsWithDefault')
->singleFile()
->withFallback(fn ($parent) => ['default.jpg', 'public'])
->registerMediaConversions(function () {
$this->addMediaConversion('tiny')->width(200)->height(200);
});
2023-03-07 15:31:24 +01:00
$this->addMediaCollection('images');
2023-03-08 01:38:57 +01:00
$this->addMediaCollection('singleForced')->singleFile()->forceFileName(function ($model, $name) {
2023-03-07 15:31:24 +01:00
return $name.' '.now()->format('Y-m-d');
});
2023-03-08 01:38:57 +01:00
$this->addMediaCollection('multipleForced')->forceFileName(function ($model, $name) {
2023-03-07 15:31:24 +01:00
return $name.' '.now()->format('Y-m-d');
});
2023-03-07 23:11:35 +01:00
$this->addMediaCollection('singleStoringHook')->singleFile()->storing(function ($adder, $fileName) {
2023-03-07 15:31:24 +01:00
return $adder->withCustomProperties([
'use' => 'AAA',
'ttt' => pathinfo($fileName, PATHINFO_FILENAME),
]);
});
2023-03-07 21:53:36 +01:00
2023-03-07 23:11:35 +01:00
$this->addMediaCollection('singleWithEvent')->singleFile()->stored(function (Media $media) {
2023-03-07 21:53:36 +01:00
Event::dispatch(new MediaStored($media));
});
2023-03-07 23:11:35 +01:00
$this->addMediaCollection('multipleFilesWithEvent')->stored(function (Media $media) {
2023-03-07 21:53:36 +01:00
Event::dispatch(new MediaStored($media));
});
2023-03-07 15:31:24 +01:00
2023-03-07 23:11:35 +01:00
$this->addMediaCollection('multipleProperties')->singleFile()->withDefaultProperties(fn ($path) => [
'test' => Str::camel($path),
])->withPropertyValidation(fn ($path) => [
'test' => 'string|max:10',
]);
}
2023-03-07 15:31:24 +01:00
}