Link.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\WebLink;
  11. use Psr\Link\EvolvableLinkInterface;
  12. class Link implements EvolvableLinkInterface
  13. {
  14. // Relations defined in https://www.w3.org/TR/html5/links.html#links and applicable on link elements
  15. public const REL_ALTERNATE = 'alternate';
  16. public const REL_AUTHOR = 'author';
  17. public const REL_HELP = 'help';
  18. public const REL_ICON = 'icon';
  19. public const REL_LICENSE = 'license';
  20. public const REL_SEARCH = 'search';
  21. public const REL_STYLESHEET = 'stylesheet';
  22. public const REL_NEXT = 'next';
  23. public const REL_PREV = 'prev';
  24. // Relation defined in https://www.w3.org/TR/preload/
  25. public const REL_PRELOAD = 'preload';
  26. // Relations defined in https://www.w3.org/TR/resource-hints/
  27. public const REL_DNS_PREFETCH = 'dns-prefetch';
  28. public const REL_PRECONNECT = 'preconnect';
  29. public const REL_PREFETCH = 'prefetch';
  30. public const REL_PRERENDER = 'prerender';
  31. // Extra relations
  32. public const REL_MERCURE = 'mercure';
  33. private $href = '';
  34. /**
  35. * @var string[]
  36. */
  37. private $rel = [];
  38. /**
  39. * @var string[]
  40. */
  41. private $attributes = [];
  42. public function __construct(string $rel = null, string $href = '')
  43. {
  44. if (null !== $rel) {
  45. $this->rel[$rel] = $rel;
  46. }
  47. $this->href = $href;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function getHref(): string
  53. {
  54. return $this->href;
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function isTemplated(): bool
  60. {
  61. return $this->hrefIsTemplated($this->href);
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function getRels(): array
  67. {
  68. return array_values($this->rel);
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function getAttributes(): array
  74. {
  75. return $this->attributes;
  76. }
  77. /**
  78. * {@inheritdoc}
  79. *
  80. * @return static
  81. */
  82. public function withHref($href)
  83. {
  84. $that = clone $this;
  85. $that->href = $href;
  86. return $that;
  87. }
  88. /**
  89. * {@inheritdoc}
  90. *
  91. * @return static
  92. */
  93. public function withRel($rel)
  94. {
  95. $that = clone $this;
  96. $that->rel[$rel] = $rel;
  97. return $that;
  98. }
  99. /**
  100. * {@inheritdoc}
  101. *
  102. * @return static
  103. */
  104. public function withoutRel($rel)
  105. {
  106. $that = clone $this;
  107. unset($that->rel[$rel]);
  108. return $that;
  109. }
  110. /**
  111. * {@inheritdoc}
  112. *
  113. * @return static
  114. */
  115. public function withAttribute($attribute, $value)
  116. {
  117. $that = clone $this;
  118. $that->attributes[$attribute] = $value;
  119. return $that;
  120. }
  121. /**
  122. * {@inheritdoc}
  123. *
  124. * @return static
  125. */
  126. public function withoutAttribute($attribute)
  127. {
  128. $that = clone $this;
  129. unset($that->attributes[$attribute]);
  130. return $that;
  131. }
  132. private function hrefIsTemplated(string $href): bool
  133. {
  134. return false !== strpos($href, '{') || false !== strpos($href, '}');
  135. }
  136. }