diff --git a/amigor/app/Models/User.php b/amigor/app/Models/User.php index 37328b5..d822101 100644 --- a/amigor/app/Models/User.php +++ b/amigor/app/Models/User.php @@ -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 { @@ -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'); + } } diff --git a/amigor/composer.json b/amigor/composer.json index 65d4147..98cdd1e 100644 --- a/amigor/composer.json +++ b/amigor/composer.json @@ -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" diff --git a/amigor/composer.lock b/amigor/composer.lock index b4ffc1e..bf16c09 100644 --- a/amigor/composer.lock +++ b/amigor/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "1b46dd8ffd906200ec34a036c691631a", + "content-hash": "34a9b595e36d533d9d546bcb20f31248", "packages": [ { "name": "brick/math", @@ -137,11 +137,11 @@ }, { "name": "cybex/laravel-transmorpher-client", - "version": "dev-feature/media-urls", + "version": "dev-fix/media-methods", "dist": { "type": "path", "url": "vendor/cybex/laravel-transmorpher-client", - "reference": "1c3fe06194bbc5d97e2aa09dcca8704c5a6a627a" + "reference": "e321d6247d190e8ed02b89cb648e629ec64d6214" }, "require": { "enyo/dropzone": "^5.9", @@ -7918,7 +7918,7 @@ "prefer-stable": true, "prefer-lowest": false, "platform": { - "php": "^8.3" + "php": "8.3.*" }, "platform-dev": [], "plugin-api-version": "2.6.0" diff --git a/src/HasTransmorpherMedia.php b/src/HasTransmorpherMedia.php index 4f20f1a..0da0a31 100644 --- a/src/HasTransmorpherMedia.php +++ b/src/HasTransmorpherMedia.php @@ -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); @@ -107,7 +108,7 @@ 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'); } /** @@ -115,20 +116,23 @@ public function getImageMediaNames(): 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); @@ -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 */ @@ -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(); + } } diff --git a/src/Media.php b/src/Media.php index d16d46e..c57ee05 100644 --- a/src/Media.php +++ b/src/Media.php @@ -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