|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace RZ\Roadiz\Documents\MediaFinders; |
| 6 | + |
| 7 | +use RZ\Roadiz\Documents\Exceptions\InvalidEmbedId; |
| 8 | + |
| 9 | +abstract class AbstractPodEducEmbedFinder extends AbstractEmbedFinder |
| 10 | +{ |
| 11 | + private const string PODEDUC_BASE_URL = 'https://podeduc.apps.education.fr'; |
| 12 | + /** |
| 13 | + * @internal Use getPlatform() instead |
| 14 | + */ |
| 15 | + protected static string $platform = 'podeduc'; |
| 16 | + protected static string $videoUrlPattern = '~^https://podeduc\.apps\.education\.fr/video/(?<id>[^/?#]+)(?:/)?(?:\?.*)?$~'; |
| 17 | + |
| 18 | + #[\Override] |
| 19 | + public static function supportEmbedUrl(string $embedUrl): bool |
| 20 | + { |
| 21 | + return str_starts_with($embedUrl, self::PODEDUC_BASE_URL.'/video/'); |
| 22 | + } |
| 23 | + |
| 24 | + #[\Override] |
| 25 | + public static function getPlatform(): string |
| 26 | + { |
| 27 | + return static::$platform; |
| 28 | + } |
| 29 | + |
| 30 | + #[\Override] |
| 31 | + protected function validateEmbedId(string $embedId = ''): string |
| 32 | + { |
| 33 | + if (1 === preg_match(static::$videoUrlPattern, $embedId, $matches)) { |
| 34 | + return self::PODEDUC_BASE_URL.'/video/'.$matches['id'].'/'; |
| 35 | + } |
| 36 | + |
| 37 | + if (str_starts_with($embedId, self::PODEDUC_BASE_URL.'/video/oembed/')) { |
| 38 | + $parsedUrl = parse_url($embedId); |
| 39 | + if (is_array($parsedUrl) && isset($parsedUrl['query'])) { |
| 40 | + parse_str($parsedUrl['query'], $query); |
| 41 | + if (isset($query['url']) && is_string($query['url']) && 1 === preg_match(static::$videoUrlPattern, $query['url'], $matches)) { |
| 42 | + return self::PODEDUC_BASE_URL.'/video/'.$matches['id'].'/'; |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + throw new InvalidEmbedId($embedId, static::$platform); |
| 48 | + } |
| 49 | + |
| 50 | + #[\Override] |
| 51 | + public function getMediaFeed(?string $search = null): string |
| 52 | + { |
| 53 | + $endpoint = self::PODEDUC_BASE_URL.'/video/oembed/'; |
| 54 | + $query = [ |
| 55 | + 'url' => $this->embedId, |
| 56 | + 'format' => 'json', |
| 57 | + ]; |
| 58 | + |
| 59 | + return $this->downloadFeedFromAPI($endpoint.'?'.http_build_query($query)); |
| 60 | + } |
| 61 | + |
| 62 | + #[\Override] |
| 63 | + public function getMediaTitle(): string |
| 64 | + { |
| 65 | + return $this->getFeed()['title'] ?? ''; |
| 66 | + } |
| 67 | + |
| 68 | + #[\Override] |
| 69 | + public function getMediaDescription(): string |
| 70 | + { |
| 71 | + return $this->getFeed()['description'] ?? ''; |
| 72 | + } |
| 73 | + |
| 74 | + #[\Override] |
| 75 | + public function getMediaCopyright(): string |
| 76 | + { |
| 77 | + return ($this->getFeed()['author_name'] ?? '').' ('.($this->getFeed()['provider_name'] ?? '').')'; |
| 78 | + } |
| 79 | + |
| 80 | + #[\Override] |
| 81 | + public function getThumbnailURL(): string |
| 82 | + { |
| 83 | + $thumbnailUrl = $this->getFeed()['thumbnail_url'] ?? ''; |
| 84 | + if (!is_string($thumbnailUrl) || '' === $thumbnailUrl) { |
| 85 | + return ''; |
| 86 | + } |
| 87 | + if (str_starts_with($thumbnailUrl, self::PODEDUC_BASE_URL) || str_starts_with($thumbnailUrl, 'https://')) { |
| 88 | + return $thumbnailUrl; |
| 89 | + } |
| 90 | + if (str_starts_with($thumbnailUrl, '/')) { |
| 91 | + return self::PODEDUC_BASE_URL.$thumbnailUrl; |
| 92 | + } |
| 93 | + if (str_starts_with($thumbnailUrl, 'https:/')) { |
| 94 | + $path = substr($thumbnailUrl, strlen('https:')); |
| 95 | + if (str_starts_with($path, '/')) { |
| 96 | + return self::PODEDUC_BASE_URL.$path; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return $thumbnailUrl; |
| 101 | + } |
| 102 | + |
| 103 | + #[\Override] |
| 104 | + public function getMediaWidth(): ?int |
| 105 | + { |
| 106 | + $width = $this->getFeed()['width'] ?? null; |
| 107 | + |
| 108 | + return is_numeric($width) ? (int) $width : null; |
| 109 | + } |
| 110 | + |
| 111 | + #[\Override] |
| 112 | + public function getMediaHeight(): ?int |
| 113 | + { |
| 114 | + $height = $this->getFeed()['height'] ?? null; |
| 115 | + |
| 116 | + return is_numeric($height) ? (int) $height : null; |
| 117 | + } |
| 118 | + |
| 119 | + #[\Override] |
| 120 | + public function getThumbnailName(string $pathinfo): string |
| 121 | + { |
| 122 | + if (1 === preg_match('#\.(?<extension>[jpe?g|png|gif])$#', $pathinfo, $matches)) { |
| 123 | + $pathinfo = '.'.$matches['extension']; |
| 124 | + } else { |
| 125 | + $pathinfo = '.jpg'; |
| 126 | + } |
| 127 | + if (1 === preg_match(static::$videoUrlPattern, $this->embedId, $matches)) { |
| 128 | + return 'podeduc_'.$matches['id'].$pathinfo; |
| 129 | + } |
| 130 | + |
| 131 | + throw new InvalidEmbedId($this->embedId, static::$platform); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Get embed media source URL. |
| 136 | + */ |
| 137 | + #[\Override] |
| 138 | + public function getSource(array &$options = []): string |
| 139 | + { |
| 140 | + parent::getSource($options); |
| 141 | + |
| 142 | + $queryString = [ |
| 143 | + 'is_iframe' => 'true', |
| 144 | + ]; |
| 145 | + |
| 146 | + if ($options['autoplay']) { |
| 147 | + $queryString['autoplay'] = (int) $options['autoplay']; |
| 148 | + } |
| 149 | + if ($options['loop']) { |
| 150 | + $queryString['loop'] = (int) $options['loop']; |
| 151 | + } |
| 152 | + if ($options['start']) { |
| 153 | + $queryString['start'] = (int) $options['start']; |
| 154 | + } |
| 155 | + |
| 156 | + return $this->embedId.'?'.http_build_query($queryString); |
| 157 | + } |
| 158 | +} |
0 commit comments