|
2 | 2 |
|
3 | 3 | namespace Zoon\CommonMark\Ext\YouTubeIframe; |
4 | 4 |
|
5 | | -use League\CommonMark\ElementRendererInterface; |
6 | | -use League\CommonMark\HtmlElement; |
7 | | -use League\CommonMark\Inline\Element\AbstractInline; |
8 | | -use League\CommonMark\Inline\Renderer\InlineRendererInterface; |
9 | | - |
10 | | -final class YouTubeIframeRenderer implements InlineRendererInterface { |
11 | | - |
12 | | - private $width; |
13 | | - private $height; |
14 | | - private $allowFullScreen; |
15 | | - |
16 | | - /** |
17 | | - * YouTubeIframeRenderer constructor. |
18 | | - * @param string $width |
19 | | - * @param string $height |
20 | | - * @param bool $allowFullScreen |
21 | | - */ |
22 | | - public function __construct(string $width, string $height, bool $allowFullScreen) { |
23 | | - $this->width = $width; |
24 | | - $this->height = $height; |
25 | | - $this->allowFullScreen = $allowFullScreen; |
26 | | - } |
27 | | - |
28 | | - /** |
29 | | - * @inheritDoc |
30 | | - */ |
31 | | - public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer) { |
32 | | - if (!($inline instanceof YouTubeIframe)) { |
33 | | - throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline)); |
34 | | - } |
35 | | - $src = "https://www.youtube.com/embed/{$inline->getUrl()->getVideoId()}"; |
36 | | - $startTimestamp = $inline->getUrl()->getStartTimestamp(); |
37 | | - if ($startTimestamp !== null) { |
38 | | - $src .= "?start={$startTimestamp}"; |
39 | | - } |
40 | | - return new HtmlElement('iframe', [ |
41 | | - 'width' => $this->width, |
42 | | - 'height' => $this->height, |
43 | | - 'src' => $src, |
44 | | - 'frameborder' => 0, |
45 | | - 'allowfullscreen' => $this->allowFullScreen, |
46 | | - ]); |
47 | | - } |
| 5 | +use InvalidArgumentException; |
| 6 | +use League\CommonMark\Node\Node; |
| 7 | +use League\CommonMark\Renderer\ChildNodeRendererInterface; |
| 8 | +use League\CommonMark\Renderer\NodeRendererInterface; |
| 9 | +use League\CommonMark\Util\HtmlElement; |
| 10 | + |
| 11 | +final class YouTubeIframeRenderer implements NodeRendererInterface |
| 12 | +{ |
| 13 | + private string $width; |
| 14 | + private string $height; |
| 15 | + private ?string $wrapperClass; |
| 16 | + private bool $allowFullScreen; |
| 17 | + |
| 18 | + /** |
| 19 | + * YouTubeIframeRenderer constructor. |
| 20 | + * @param array $props |
| 21 | + */ |
| 22 | + public function __construct(array $props = []) |
| 23 | + { |
| 24 | + $this->width = $props['width'] ?? ''; |
| 25 | + $this->height = $props['height'] ?? ''; |
| 26 | + $this->wrapperClass = $props['wrapper_class'] ?? null; |
| 27 | + $this->allowFullScreen = $props['allow_full_screen'] ?? true; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @inheritDoc |
| 32 | + */ |
| 33 | + public function render(Node $node, ChildNodeRendererInterface $childRenderer): HtmlElement |
| 34 | + { |
| 35 | + if (!($node instanceof YouTubeIframe)) { |
| 36 | + throw new InvalidArgumentException('Incompatible inline type: ' . get_class($node)); |
| 37 | + } |
| 38 | + |
| 39 | + $src = "https://www.youtube.com/embed/{$node->getUrl()->getVideoId()}"; |
| 40 | + $startTimestamp = $node->getUrl()->getStartTimestamp(); |
| 41 | + |
| 42 | + if ($startTimestamp !== null) { |
| 43 | + $src .= "?start=$startTimestamp"; |
| 44 | + } |
| 45 | + |
| 46 | + $iframeElement = new HtmlElement('iframe', array_merge([ |
| 47 | + 'width' => $this->width, |
| 48 | + 'height' => $this->height, |
| 49 | + 'src' => $src, |
| 50 | + 'frameborder' => "0", |
| 51 | + ], $this->allowFullScreen ? [ |
| 52 | + 'allowfullscreen' => "1", |
| 53 | + ] : [])); |
| 54 | + |
| 55 | + return is_null($this->wrapperClass) ? $iframeElement : new HtmlElement('div', [ |
| 56 | + 'class' => $this->wrapperClass, |
| 57 | + ], $iframeElement); |
| 58 | + } |
48 | 59 | } |
0 commit comments