Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve return types when using a custom Media model #3777

Merged
merged 2 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions src/InteractsWithMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
use Spatie\MediaLibraryPro\PendingMediaLibraryRequestHandler;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
* @template TMedia of \Spatie\MediaLibrary\MediaCollections\Models\Media
*/
trait InteractsWithMedia
{
/** @var Conversion[] */
Expand Down Expand Up @@ -57,7 +60,7 @@ public static function bootInteractsWithMedia(): void
}

/**
* @return MorphMany<Media, $this>
* @return MorphMany<TMedia, $this>
*/
public function media(): MorphMany
{
Expand All @@ -66,19 +69,26 @@ public function media(): MorphMany

/**
* Add a file to the media library.
*
* @return FileAdder<TMedia>
*/
public function addMedia(string|UploadedFile $file): FileAdder
{
return app(FileAdderFactory::class)->create($this, $file);
}

/**
* @return FileAdder<TMedia>
*/
public function addMediaFromRequest(string $key): FileAdder
{
return app(FileAdderFactory::class)->createFromRequest($this, $key);
}

/**
* Add a file from the given disk.
*
* @return FileAdder<TMedia>
*/
public function addMediaFromDisk(string $key, ?string $disk = null): FileAdder
{
Expand Down Expand Up @@ -111,7 +121,7 @@ public function syncFromMediaLibraryRequest(?array $mediaLibraryRequestItems): P
* Add multiple files from a request by keys.
*
* @param string[] $keys
* @return \Spatie\MediaLibrary\MediaCollections\FileAdder[]
* @return Collection<int, FileAdder<TMedia>>
*/
public function addMultipleMediaFromRequest(array $keys): Collection
{
Expand All @@ -121,7 +131,7 @@ public function addMultipleMediaFromRequest(array $keys): Collection
/**
* Add all files from a request.
*
* @return \Spatie\MediaLibrary\MediaCollections\FileAdder[]
* @return Collection<int, FileAdder<TMedia>>
*/
public function addAllMediaFromRequest(): Collection
{
Expand All @@ -131,7 +141,7 @@ public function addAllMediaFromRequest(): Collection
/**
* Add a remote file to the media library.
*
*
* @return FileAdder<TMedia>
*
* @throws \Spatie\MediaLibrary\MediaCollections\Exceptions\FileCannotBeAdded
*/
Expand Down Expand Up @@ -167,6 +177,7 @@ public function addMediaFromUrl(string $url, array|string ...$allowedMimeTypes):
* Add a file to the media library that contains the given string.
*
* @param string string
* @return FileAdder<TMedia>
*/
public function addMediaFromString(string $text): FileAdder
{
Expand All @@ -184,6 +195,8 @@ public function addMediaFromString(string $text): FileAdder
/**
* Add a base64 encoded file to the media library.
*
* @return FileAdder<TMedia>
*
* @throws \Spatie\MediaLibrary\MediaCollections\Exceptions\FileCannotBeAdded
* @throws InvalidBase64Data
*/
Expand Down Expand Up @@ -220,6 +233,8 @@ public function addMediaFromBase64(string $base64data, array|string ...$allowedM

/**
* Add a file to the media library from a stream.
*
* @return FileAdder<TMedia>
*/
public function addMediaFromStream($stream): FileAdder
{
Expand All @@ -236,6 +251,8 @@ public function addMediaFromStream($stream): FileAdder

/**
* Copy a file to the media library.
*
* @return FileAdder<TMedia>
*/
public function copyMedia(string|UploadedFile $file): FileAdder
{
Expand All @@ -252,6 +269,8 @@ public function hasMedia(string $collectionName = 'default', array|callable $fil

/**
* Get media collection by its collectionName.
*
* @return MediaCollections\Models\Collections\MediaCollection<int, TMedia>
*/
public function getMedia(string $collectionName = 'default', array|callable $filters = []): MediaCollections\Models\Collections\MediaCollection
{
Expand All @@ -270,6 +289,9 @@ public function getMediaModel(): string
return config('media-library.media_model');
}

/**
* @return TMedia|null
*/
public function getFirstMedia(string $collectionName = 'default', $filters = []): ?Media
{
$media = $this->getMedia($collectionName, $filters);
Expand Down
15 changes: 15 additions & 0 deletions src/MediaCollections/FileAdder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
use Symfony\Component\HttpFoundation\File\File as SymfonyFile;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
* @template TMedia of \Spatie\MediaLibrary\MediaCollections\Models\Media
*/
class FileAdder
{
use Macroable;
Expand Down Expand Up @@ -229,11 +232,17 @@ public function addCustomHeaders(array $customRemoteHeaders): self
return $this;
}

/**
* @return TMedia
*/
public function toMediaCollectionOnCloudDisk(string $collectionName = 'default'): Media
{
return $this->toMediaCollection($collectionName, config('filesystems.cloud'));
}

/**
* @return TMedia
*/
public function toMediaCollectionFromRemote(string $collectionName = 'default', string $diskName = ''): Media
{
$storage = Storage::disk($this->file->getDisk());
Expand Down Expand Up @@ -287,6 +296,9 @@ public function toMediaCollectionFromRemote(string $collectionName = 'default',
return $media;
}

/**
* @return TMedia
*/
public function toMediaCollection(string $collectionName = 'default', string $diskName = ''): Media
{
$sanitizedFileName = ($this->fileNameSanitizer)($this->fileName);
Expand Down Expand Up @@ -352,6 +364,9 @@ public function toMediaCollection(string $collectionName = 'default', string $di
return $media;
}

/**
* @return TMedia
*/
public function toMediaLibrary(string $collectionName = 'default', string $diskName = ''): Media
{
return $this->toMediaCollection($collectionName, $diskName);
Expand Down
Loading