A fork of potaka/bbcode to handle future maintenance
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.0KB

  1. <?php
  2. namespace Potaka\BbCode\Tag;
  3. use Potaka\BbCode\Tokenizer\Tag as TokenTag;
  4. use Tekstove\UrlVideoParser\Youtube\YoutubeParser;
  5. use Tekstove\UrlVideoParser\Youtube\YoutubeException;
  6. /**
  7. * Tag for youtube videos
  8. *
  9. * @author po_taka <angel.koilov@gmail.com>
  10. */
  11. class YoutubeTag implements TagInterface
  12. {
  13. public function format(TokenTag $tokenTag): string
  14. {
  15. $link = $tokenTag->getText();
  16. try {
  17. $parser = new YoutubeParser();
  18. $videoId = $parser->getId($link);
  19. } catch (YoutubeException $e) {
  20. $unknownTag = new UnknownSimpleType();
  21. return $unknownTag->format($tokenTag);
  22. }
  23. return '<iframe src="https://www.youtube.com/embed/' . $videoId . '" frameborder="0" allowfullscreen></iframe>';
  24. }
  25. public function getName(): string
  26. {
  27. return 'youtube';
  28. }
  29. public function getOriginalText(TokenTag $tokenTag): string
  30. {
  31. return "[{$this->getName()}]{$tokenTag->getText()}[/{$this->getName()}";
  32. }
  33. }