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

media methods now use the media name instead of the function name #49

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
19 changes: 17 additions & 2 deletions amigor/app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Illuminate\Notifications\Notifiable;
use Transmorpher\HasTransmorpherMedia;
use Transmorpher\HasTransmorpherMediaInterface;
use Transmorpher\Image;
use Transmorpher\Video;

class User extends Authenticatable implements HasTransmorpherMediaInterface
{
Expand Down Expand Up @@ -49,11 +51,24 @@ protected function casts(): array

protected array $transmorpherImages = [
'front',
'back'
];

protected array $transmorpherVideos = [
'teaser',
'full'
];

/**
* Example of a media method.
*
* @return Image
*/
public function mediaMethod(): Image
{
return Image::for($this, 'back');
}

public function mediaMethodWithUnionType(): Image|Video
{
return Video::for($this, 'full');
}
}
2 changes: 1 addition & 1 deletion amigor/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"keywords": ["laravel", "framework"],
"license": "MIT",
"require": {
"php": "^8.3",
"php": "8.3.*",
"cybex/laravel-transmorpher-client": "@dev",
"laravel/framework": "^11.9",
"laravel/tinker": "^2.9"
Expand Down
8 changes: 4 additions & 4 deletions amigor/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 25 additions & 10 deletions src/HasTransmorpherMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@
namespace Transmorpher;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Support\Collection;
use ReflectionClass;
use ReflectionMethod;
use ReflectionUnionType;
use Transmorpher\Exceptions\DuplicateMediaNameException;
use Transmorpher\Exceptions\MissingMorphAliasException;
use Transmorpher\Models\TransmorpherMedia;
use Illuminate\Database\Eloquent\Relations\MorphMany;

trait HasTransmorpherMedia
{
protected static Collection $cachedImageMediaNames;
protected static Collection $cachedVideoMediaNames;
public static Collection $cachedImageMediaArrayNames;
public static Collection $cachedVideoMediaArrayNames;

/**
* @throws MissingMorphAliasException
*/
public static function bootHasTransmorpherMedia()
public static function bootHasTransmorpherMedia(): void
{
if (static::getModel()->getTransmorpherAlias() === static::class) {
throw new MissingMorphAliasException(static::class);
Expand Down Expand Up @@ -107,28 +108,31 @@ public function getTransmorpherAlias(): string
*/
public function getImageMediaNames(): Collection
{
return static::$cachedImageMediaNames ??= $this->getMediaNames(Image::class, $this->transmorpherImages ?? []);
return $this->getMediaNames(Image::class, $this->transmorpherImages ?? [], 'cachedImageMediaArrayNames');
}

/**
* @return Collection
*/
public function getVideoMediaNames(): Collection
{
return static::$cachedVideoMediaNames ??= $this->getMediaNames(Video::class, $this->transmorpherVideos ?? []);
return $this->getMediaNames(Video::class, $this->transmorpherVideos ?? [], 'cachedVideoMediaArrayNames');
}

/**
* Get the media names extracted from the media arrays and media methods based on the media class.
*
* @param string $mediaClass
* @param array $mediaArray
* @param string $cacheName
* @return Collection
* @throws DuplicateMediaNameException
*/
protected function getMediaNames(string $mediaClass, array $mediaArray): Collection
protected function getMediaNames(string $mediaClass, array $mediaArray, string $cacheName): Collection
{
$mediaMethods = $this->getMediaMethods($mediaClass);
$loweredMediaMethods = $mediaMethods->map('strtolower');
$loweredMediaNames = collect($mediaArray)->map('strtolower');
$loweredMediaNames = static::${$cacheName} ??= collect($mediaArray)->map('strtolower');
$duplicatesInArray = $loweredMediaNames->duplicates();
$conflictsWithMethods = $loweredMediaMethods->intersect($loweredMediaNames);

Expand All @@ -142,6 +146,8 @@ protected function getMediaNames(string $mediaClass, array $mediaArray): Collect
}

/**
* Extract media names from media methods based on the returned media class.
*
* @param string $mediaClass
* @return Collection
*/
Expand All @@ -153,11 +159,20 @@ protected function getMediaMethods(string $mediaClass): Collection
foreach ($reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC) as $reflectionMethod) {
$reflectionMethodName = $reflectionMethod->getName();

if (is_a($reflectionMethod->getReturnType()?->getName(), $mediaClass, true) && strtolower($reflectionMethodName) !== 'image' && strtolower($reflectionMethodName) !== 'video') {
$mediaMethods->push($reflectionMethodName);
if (is_a($this->getReflectionMethodReturnType($reflectionMethod), $mediaClass, true) && strtolower($reflectionMethodName) !== 'image' && strtolower($reflectionMethodName) !== 'video') {
$mediaMethods->push($reflectionMethod->invoke($this)->getMediaName());
}
}

return $mediaMethods;
}

protected function getReflectionMethodReturnType(ReflectionMethod $reflectionMethod): ?string
{
if (is_a($reflectionMethod->getReturnType(), ReflectionUnionType::class)) {
return $reflectionMethod->invoke($this)::class;
}

return $reflectionMethod->getReturnType()?->getName();
}
}
5 changes: 5 additions & 0 deletions src/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,11 @@ public function getTransmorpherMedia(): TransmorpherMedia
return $this->transmorpherMedia;
}

public function getMediaName(): string
{
return $this->mediaName;
}

/**
* @param array $responseFromServer The server response as an array.
* @param int $httpCode
Expand Down