diff --git a/src/Model/Serializer/ValueObject/MediaObject/VideoNormalizer.php b/src/Model/Serializer/ValueObject/MediaObject/VideoNormalizer.php index faf1c60cd7..6668ae16aa 100644 --- a/src/Model/Serializer/ValueObject/MediaObject/VideoNormalizer.php +++ b/src/Model/Serializer/ValueObject/MediaObject/VideoNormalizer.php @@ -8,6 +8,7 @@ use CultuurNet\UDB3\Model\ValueObject\MediaObject\Video; use CultuurNet\UDB3\Model\ValueObject\Translation\Language; use CultuurNet\UDB3\Model\ValueObject\Web\Url; +use InvalidArgumentException; use RuntimeException; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; @@ -15,6 +16,8 @@ final class VideoNormalizer implements NormalizerInterface { private const YOUTUBE_EMBED = 'https://www.youtube.com/embed/'; + private const YOUTUBE_SHORT = 'https://www.youtube.com/shorts/'; + private const YOUTUBE_NAME = 'YouTube'; private const VIMEO_EMBED = 'https://player.vimeo.com/video/'; @@ -22,10 +25,11 @@ final class VideoNormalizer implements NormalizerInterface private const VIMEO_NAME = 'Vimeo'; private array $videoPlatforms = [ + // This index is the group number from the matching regexp from Video::REGEX 5 => [ 'embed' => self::YOUTUBE_EMBED, 'name' => self::YOUTUBE_NAME, - ], + ], 7 => [ 'embed' => self::VIMEO_EMBED, 'name' => self::VIMEO_NAME, @@ -38,6 +42,10 @@ final class VideoNormalizer implements NormalizerInterface 'embed' => self::YOUTUBE_EMBED, 'name' => self::YOUTUBE_NAME, ], + 13 => [ + 'embed' => self::YOUTUBE_SHORT, + 'name' => self::YOUTUBE_NAME, + ], ]; private array $defaultCopyrightHolders; @@ -55,6 +63,10 @@ public function __construct(array $defaultCopyrightHolders) */ public function normalize($video, $format = null, array $context = []): array { + if (!$video instanceof Video) { + throw new InvalidArgumentException('Expected video object, got ' . get_class($video)); + } + $platformData = $this->getPlatformData($video->getUrl()); $videoArray = [ 'id' => $video->getId(), @@ -90,7 +102,7 @@ private function getPlatformData(Url $url): array ); foreach ($this->videoPlatforms as $videoPlatformIndex => $videoPlatformData) { - if (isset($matches[$videoPlatformIndex]) && !empty($matches[$videoPlatformIndex])) { + if (!empty($matches[$videoPlatformIndex])) { return [ 'embed' => $videoPlatformData['embed'], 'name' => $videoPlatformData['name'], diff --git a/src/Model/ValueObject/MediaObject/Video.php b/src/Model/ValueObject/MediaObject/Video.php index 9502e8fd89..de44c11a4e 100644 --- a/src/Model/ValueObject/MediaObject/Video.php +++ b/src/Model/ValueObject/MediaObject/Video.php @@ -9,7 +9,7 @@ final class Video { - public const REGEX = '/^http(s?):\/\/(www\.)?((youtube\.com\/watch\?v=([^\/#&?]*))|(vimeo\.com\/([^\/#&?]*))|(youtu\.be\/([^\/#&?]*))|(youtube.com\/embed\/([^\/#&?]*)))/'; + public const REGEX = '/^http(s?):\/\/(www\.)?((youtube\.com\/watch\?v=([^\/#&?]*))|(vimeo\.com\/([^\/#&?]*))|(youtu\.be\/([^\/#&?]*))|(youtube.com\/embed\/([^\/#&?]*))|(youtube.com\/shorts\/([^\/#&?]*)))/'; private string $id; diff --git a/tests/Model/Serializer/ValueObject/MediaObject/VideoNormalizerTest.php b/tests/Model/Serializer/ValueObject/MediaObject/VideoNormalizerTest.php index 316f64c54d..500dd9e5a3 100644 --- a/tests/Model/Serializer/ValueObject/MediaObject/VideoNormalizerTest.php +++ b/tests/Model/Serializer/ValueObject/MediaObject/VideoNormalizerTest.php @@ -79,6 +79,20 @@ public function videoAddedProvider(): array 'copyrightHolder' => 'Droits d\'auteur gérés par YouTube', ], ], + 'video_from_youtube_short' => [ + new Video( + '91c75325-3830-4000-b580-5778b2de4548', + new Url('https://www.youtube.com/shorts/ViOS7SeT0HE'), + new Language('fr') + ), + [ + 'id' => '91c75325-3830-4000-b580-5778b2de4548', + 'url' => 'https://www.youtube.com/shorts/ViOS7SeT0HE', + 'embedUrl' => 'https://www.youtube.com/shorts/ViOS7SeT0HE', + 'language' => 'fr', + 'copyrightHolder' => 'Droits d\'auteur gérés par YouTube', + ], + ], 'video_from_youtube_url_shortener' => [ new Video( '91c75325-3830-4000-b580-5778b2de4548',