Skip to content

Commit

Permalink
Merge pull request #1420 from cultuurnet/III-5624
Browse files Browse the repository at this point in the history
Iii 5624
  • Loading branch information
grubolsch authored Jul 18, 2023
2 parents 5b747a4 + e53d257 commit 4702656
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/Model/Serializer/ValueObject/MediaObject/VideoNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,28 @@
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;

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/';

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,
Expand All @@ -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;
Expand All @@ -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(),
Expand Down Expand Up @@ -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'],
Expand Down
2 changes: 1 addition & 1 deletion src/Model/ValueObject/MediaObject/Video.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 4702656

Please sign in to comment.