Skip to content

Commit

Permalink
fix an issue where media methods would use the function name instead …
Browse files Browse the repository at this point in the history
…of the media name
  • Loading branch information
mszulik committed Oct 14, 2024
1 parent 1d40e3d commit 7771cb3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
12 changes: 11 additions & 1 deletion amigor/app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Notifications\Notifiable;
use Transmorpher\HasTransmorpherMedia;
use Transmorpher\HasTransmorpherMediaInterface;
use Transmorpher\Image;

class User extends Authenticatable implements HasTransmorpherMediaInterface
{
Expand Down Expand Up @@ -49,11 +50,20 @@ 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, 'passed');
}
}
8 changes: 6 additions & 2 deletions src/HasTransmorpherMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ trait HasTransmorpherMedia
/**
* @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 @@ -119,6 +119,8 @@ public function getVideoMediaNames(): Collection
}

/**
* Get the media names extracted from the media arrays and media methods based on the media class.
*
* @param string $mediaClass
* @param array $mediaArray
* @return Collection
Expand All @@ -142,6 +144,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 @@ -154,7 +158,7 @@ protected function getMediaMethods(string $mediaClass): Collection
$reflectionMethodName = $reflectionMethod->getName();

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

Expand Down
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

0 comments on commit 7771cb3

Please sign in to comment.