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.

36 lines
834B

  1. <?php
  2. namespace Knockout\BbCode\Tag;
  3. use Knockout\BbCode\Tokenizer\Tag as TokenTag;
  4. /**
  5. * @author po_taka <angel.koilov@gmail.com>
  6. */
  7. class Link implements TagInterface
  8. {
  9. const REG_EXP_VALID_URL = '~https?://[a-zA-Z0-9_\-.:/#?]+~';
  10. public function format(TokenTag $tokenTag): string
  11. {
  12. $url = $tokenTag->getArgument();
  13. if (!preg_match(self::REG_EXP_VALID_URL, $url)) {
  14. $simpleTag = new UnknownSimpleType();
  15. return $simpleTag->format($tokenTag);
  16. }
  17. return "<a href=\"{$url}\" target=\"_blank\">{$tokenTag->getText()}</a>";
  18. }
  19. public function getName(): string
  20. {
  21. return 'url';
  22. }
  23. public function getOriginalText(TokenTag $tokenTag) : string
  24. {
  25. return "[url={$tokenTag->getArgument()}]{$tokenTag->getText()}[/url]";
  26. }
  27. }