|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of phpDocumentor. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + * |
| 11 | + * @link https://phpdoc.org |
| 12 | + */ |
| 13 | + |
| 14 | +namespace phpDocumentor\Guides\RestructuredText\Directives; |
| 15 | + |
| 16 | +use phpDocumentor\Guides\Nodes\EmbeddedFrame; |
| 17 | +use phpDocumentor\Guides\RestructuredText\Parser\BlockContext; |
| 18 | +use phpDocumentor\Guides\RestructuredText\Parser\Directive; |
| 19 | + |
| 20 | +use function array_filter; |
| 21 | + |
| 22 | +/** |
| 23 | + * This directive is used to embed a youtube video in the document. |
| 24 | + * |
| 25 | + * Basic usage |
| 26 | + * |
| 27 | + * ```rst |
| 28 | + * .. youtube:: dQw4w9WgXcQ |
| 29 | + * ``` |
| 30 | + * |
| 31 | + * Options: |
| 32 | + * |
| 33 | + * - string title The title of the video |
| 34 | + * - int width The width of the video, default is 560 |
| 35 | + * - int height The height of the video, default is 315 |
| 36 | + * - string allow The allow attribute of the iframe, default is 'encrypted-media; picture-in-picture; web-share' |
| 37 | + * - bool allowfullscreen Whether the video should be allowed to go fullscreen, default is true |
| 38 | + */ |
| 39 | +final class YoutubeDirective extends BaseDirective |
| 40 | +{ |
| 41 | + public function getName(): string |
| 42 | + { |
| 43 | + return 'youtube'; |
| 44 | + } |
| 45 | + |
| 46 | + public function process( |
| 47 | + BlockContext $blockContext, |
| 48 | + Directive $directive, |
| 49 | + ): EmbeddedFrame { |
| 50 | + $node = new EmbeddedFrame( |
| 51 | + 'https://www.youtube-nocookie.com/embed/' . $directive->getData(), |
| 52 | + ); |
| 53 | + |
| 54 | + return $node->withOptions( |
| 55 | + array_filter( |
| 56 | + [ |
| 57 | + 'width' => $directive->getOption('width')->getValue() ?? 560, |
| 58 | + 'title' => $directive->getOption('title')->getValue(), |
| 59 | + 'height' => $directive->getOption('height')->getValue() ?? 315, |
| 60 | + 'allow' => $directive->getOption('allow')->getValue() ?? 'encrypted-media; picture-in-picture; web-share', |
| 61 | + 'allowfullscreen' => (bool) ($directive->getOption('allowfullscreen')->getValue() ?? true), |
| 62 | + ], |
| 63 | + ), |
| 64 | + ); |
| 65 | + } |
| 66 | +} |
0 commit comments