Skip to content

Commit

Permalink
simplify types
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed May 18, 2024
1 parent 61326f6 commit 0fa97db
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 41 deletions.
18 changes: 9 additions & 9 deletions src/Contracts/InteractWithMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public function getMedia(?string $collection_name = null, ?string $collection_gr

public function hasMedia(?string $collection_name = null, ?string $collection_group = null): bool;

/**c
* @return TMedia
/**
* @return ?TMedia
*/
public function getFirstMedia(?string $collection_name = null, ?string $collection_group = null): ?Media;
public function getFirstMedia(?string $collection_name = null, ?string $collection_group = null);

public function getFirstMediaUrl(
?string $collection_name = null,
Expand All @@ -42,7 +42,7 @@ public function registerMediaCollections(): Arrayable|iterable|null;
* @param TMedia $media
* @return Arrayable<int,MediaConversion>|iterable<MediaConversion>|null
*/
public function registerMediaConversions(Media $media): Arrayable|iterable|null;
public function registerMediaConversions($media): Arrayable|iterable|null;

/**
* @return Collection<string, MediaCollection>
Expand All @@ -57,14 +57,14 @@ public function getMediaCollection(string $collection_name): ?MediaCollection;
* @param TMedia $media
* @return Collection<string, MediaConversion>
*/
public function getMediaConversions(Media $media): Collection;
public function getMediaConversions($media): Collection;

public function getMediaConversionKey(string $conversion): string;

/**
* @param TMedia $media
*/
public function getMediaConversion(Media $media, string $conversion): ?MediaConversion;
public function getMediaConversion($media, string $conversion): ?MediaConversion;

/**
* @param int[] $except Array of Media Ids
Expand All @@ -88,18 +88,18 @@ public function addMedia(
?string $name = null,
?string $order = null,
?array $metadata = null,
): Media;
);

/**
* @param TMedia $media
*/
public function dispatchConversion(Media $media, string $conversionName): static;
public function dispatchConversion($media, string $conversionName): static;

/**
* @param TMedia $media
*/
public function dispatchConversions(
Media $media,
$media,
?bool $force = false,
?array $only = null,
?array $except = null,
Expand Down
5 changes: 3 additions & 2 deletions src/MediaZipper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

class MediaZipper implements Responsable
{
/**
* @param Collection<int, Media> $media
*/
public function __construct(
public Collection $media = new Collection(),
public string $fileName = 'media.zip',
Expand Down Expand Up @@ -44,7 +47,6 @@ public function getZipStream(array $options = [])
$options
));

/** @var Media $item */
foreach ($this->media as $index => $item) {
$stream = $item->readStream();

Expand Down Expand Up @@ -85,7 +87,6 @@ public function toResponse($request): StreamedResponse
]);

$zip->finish();

}, 200, [
'Content-Disposition' => "attachment; filename=\"{$this->fileName}\"",
'Content-Type' => 'application/octet-stream',
Expand Down
16 changes: 8 additions & 8 deletions src/Traits/HasMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ public function hasMedia(?string $collection_name = null, ?string $collection_gr
return $this->getMedia($collection_name, $collection_group)->isNotEmpty();
}

/**c
/**
* @return TMedia
*/
public function getFirstMedia(?string $collection_name = null, ?string $collection_group = null): ?Media
public function getFirstMedia(?string $collection_name = null, ?string $collection_group = null)
{
return $this->getMedia($collection_name, $collection_group)->first();
}
Expand Down Expand Up @@ -104,7 +104,7 @@ public function registerMediaCollections(): Arrayable|iterable|null
* @param TMedia $media
* @return Arrayable<MediaConversion>|iterable<MediaConversion>|null
*/
public function registerMediaConversions(Media $media): Arrayable|iterable|null
public function registerMediaConversions($media): Arrayable|iterable|null
{
return collect();
}
Expand Down Expand Up @@ -137,7 +137,7 @@ public function getMediaCollection(string $collection_name): ?MediaCollection
* @param TMedia $media
* @return Collection<string, MediaConversion>
*/
public function getMediaConversions(Media $media): Collection
public function getMediaConversions($media): Collection
{
return collect($this->registerMediaConversions($media))->keyBy('name');
}
Expand All @@ -150,7 +150,7 @@ public function getMediaConversionKey(string $conversion): string
/**
* @param TMedia $media
*/
public function getMediaConversion(Media $media, string $conversion): ?MediaConversion
public function getMediaConversion($media, string $conversion): ?MediaConversion
{
return data_get($this->getMediaConversions($media), $this->getMediaConversionKey($conversion));
}
Expand Down Expand Up @@ -185,7 +185,7 @@ public function addMedia(
?string $name = null,
?string $order = null,
?array $metadata = null,
): Media {
) {
$collection_name ??= config('media.default_collection_name');

$collection = $this->getMediaCollection($collection_name);
Expand Down Expand Up @@ -224,7 +224,7 @@ public function addMedia(
/**
* @param TMedia $media
*/
public function dispatchConversion(Media $media, string $conversionName): static
public function dispatchConversion($media, string $conversionName): static
{
$conversion = $this->getMediaConversion($media, $conversionName);

Expand All @@ -247,7 +247,7 @@ public function dispatchConversion(Media $media, string $conversionName): static
* @param TMedia $media
*/
public function dispatchConversions(
Media $media,
$media,
?bool $force = false,
?array $only = null,
?array $except = null,
Expand Down
7 changes: 3 additions & 4 deletions tests/Models/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
use Finller\Media\Jobs\OptimizedImageConversionJob;
use Finller\Media\MediaCollection;
use Finller\Media\MediaConversion;
use Finller\Media\Models\Media;
use Finller\Media\Traits\HasMedia;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Spatie\Image\Enums\Fit;

class Test extends Model implements InteractWithMedia
Expand All @@ -21,7 +20,7 @@ class Test extends Model implements InteractWithMedia

protected $guarded = [];

public function registerMediaCollections(): Collection
public function registerMediaCollections(): Arrayable|iterable|null
{
return collect([
new MediaCollection(
Expand All @@ -43,7 +42,7 @@ public function registerMediaCollections(): Collection
]);
}

public function registerMediaConversions(Media $media): Collection
public function registerMediaConversions($media): Arrayable|iterable|null
{
$conversions = collect();

Expand Down
7 changes: 3 additions & 4 deletions tests/Models/TestWithMultipleConversions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
use Finller\Media\Jobs\OptimizedImageConversionJob;
use Finller\Media\MediaCollection;
use Finller\Media\MediaConversion;
use Finller\Media\Models\Media;
use Finller\Media\Traits\HasMedia;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;

class TestWithMultipleConversions extends Model implements InteractWithMedia
{
Expand All @@ -20,7 +19,7 @@ class TestWithMultipleConversions extends Model implements InteractWithMedia

protected $guarded = [];

public function registerMediaCollections(): Collection
public function registerMediaCollections(): Arrayable|iterable|null
{
return collect([
new MediaCollection(
Expand All @@ -31,7 +30,7 @@ public function registerMediaCollections(): Collection
]);
}

public function registerMediaConversions(Media $media): Collection
public function registerMediaConversions($media): Arrayable|iterable|null
{

if ($media->type === MediaType::Image) {
Expand Down
3 changes: 1 addition & 2 deletions tests/Models/TestWithNestedConversions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Finller\Media\Enums\MediaType;
use Finller\Media\Jobs\OptimizedImageConversionJob;
use Finller\Media\MediaConversion;
use Finller\Media\Models\Media;
use Finller\Media\Traits\HasMedia;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
Expand All @@ -19,7 +18,7 @@ class TestWithNestedConversions extends Model implements InteractWithMedia

protected $guarded = [];

public function registerMediaConversions(Media $media): Collection
public function registerMediaConversions($media): Collection
{

if ($media->type === MediaType::Image) {
Expand Down
7 changes: 3 additions & 4 deletions tests/Models/TestWithResponsiveImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
use Finller\Media\Contracts\InteractWithMedia;
use Finller\Media\Enums\MediaType;
use Finller\Media\MediaCollection;
use Finller\Media\Models\Media;
use Finller\Media\Support\ResponsiveImagesConversionsPreset;
use Finller\Media\Traits\HasMedia;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;

class TestWithResponsiveImages extends Model implements InteractWithMedia
{
Expand All @@ -19,7 +18,7 @@ class TestWithResponsiveImages extends Model implements InteractWithMedia

protected $guarded = [];

public function registerMediaCollections(): Collection
public function registerMediaCollections(): Arrayable|iterable|null
{
return collect([
new MediaCollection(
Expand All @@ -31,7 +30,7 @@ public function registerMediaCollections(): Collection
]);
}

public function registerMediaConversions(Media $media): Collection
public function registerMediaConversions($media): Arrayable|iterable|null
{
if ($media->type === MediaType::Image) {
return ResponsiveImagesConversionsPreset::get($media);
Expand Down
7 changes: 3 additions & 4 deletions tests/Models/TestWithVideoConversions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
use Finller\Media\Jobs\VideoPosterConversionJob;
use Finller\Media\MediaCollection;
use Finller\Media\MediaConversion;
use Finller\Media\Models\Media;
use Finller\Media\Traits\HasMedia;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;

class TestWithVideoConversions extends Model implements InteractWithMedia
{
Expand All @@ -20,7 +19,7 @@ class TestWithVideoConversions extends Model implements InteractWithMedia

protected $guarded = [];

public function registerMediaCollections(): Collection
public function registerMediaCollections(): Arrayable|iterable|null
{
return collect([
new MediaCollection(
Expand All @@ -31,7 +30,7 @@ public function registerMediaCollections(): Collection
]);
}

public function registerMediaConversions(Media $media): Collection
public function registerMediaConversions($media): Arrayable|iterable|null
{
if ($media->type === MediaType::Video) {
return collect()
Expand Down
7 changes: 3 additions & 4 deletions tests/Models/TestWithVideoPosterPreset.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
use Finller\Media\Contracts\InteractWithMedia;
use Finller\Media\Enums\MediaType;
use Finller\Media\MediaCollection;
use Finller\Media\Models\Media;
use Finller\Media\Support\VideoPosterConversionPreset;
use Finller\Media\Traits\HasMedia;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;

class TestWithVideoPosterPreset extends Model implements InteractWithMedia
{
Expand All @@ -19,7 +18,7 @@ class TestWithVideoPosterPreset extends Model implements InteractWithMedia

protected $guarded = [];

public function registerMediaCollections(): Collection
public function registerMediaCollections(): Arrayable|iterable|null
{
return collect([
new MediaCollection(
Expand All @@ -30,7 +29,7 @@ public function registerMediaCollections(): Collection
]);
}

public function registerMediaConversions(Media $media): Collection
public function registerMediaConversions($media): Arrayable|iterable|null
{
if ($media->type === MediaType::Video) {
return VideoPosterConversionPreset::get($media, withResponsiveImages: true);
Expand Down

0 comments on commit 0fa97db

Please sign in to comment.