vendor/symfony/web-link/Link.php line 16

Open in your IDE?
  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 array<string, string|bool|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. * @param string|\Stringable|int|float|bool|string[] $value
  114. *
  115. * @return static
  116. */
  117. public function withAttribute($attribute, $value)
  118. {
  119. $that = clone $this;
  120. $that->attributes[$attribute] = $value;
  121. return $that;
  122. }
  123. /**
  124. * {@inheritdoc}
  125. *
  126. * @return static
  127. */
  128. public function withoutAttribute($attribute)
  129. {
  130. $that = clone $this;
  131. unset($that->attributes[$attribute]);
  132. return $that;
  133. }
  134. private function hrefIsTemplated(string $href): bool
  135. {
  136. return str_contains($href, '{') || str_contains($href, '}');
  137. }
  138. }