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.     // @see https://www.iana.org/assignments/link-relations/link-relations.xhtml
  15.     public const REL_ABOUT = 'about';
  16.     public const REL_ACL = 'acl';
  17.     public const REL_ALTERNATE = 'alternate';
  18.     public const REL_AMPHTML = 'amphtml';
  19.     public const REL_APPENDIX = 'appendix';
  20.     public const REL_APPLE_TOUCH_ICON = 'apple-touch-icon';
  21.     public const REL_APPLE_TOUCH_STARTUP_IMAGE = 'apple-touch-startup-image';
  22.     public const REL_ARCHIVES = 'archives';
  23.     public const REL_AUTHOR = 'author';
  24.     public const REL_BLOCKED_BY = 'blocked-by';
  25.     public const REL_BOOKMARK = 'bookmark';
  26.     public const REL_CANONICAL = 'canonical';
  27.     public const REL_CHAPTER = 'chapter';
  28.     public const REL_CITE_AS = 'cite-as';
  29.     public const REL_COLLECTION = 'collection';
  30.     public const REL_CONTENTS = 'contents';
  31.     public const REL_CONVERTEDFROM = 'convertedfrom';
  32.     public const REL_COPYRIGHT = 'copyright';
  33.     public const REL_CREATE_FORM = 'create-form';
  34.     public const REL_CURRENT = 'current';
  35.     public const REL_DESCRIBEDBY = 'describedby';
  36.     public const REL_DESCRIBES = 'describes';
  37.     public const REL_DISCLOSURE = 'disclosure';
  38.     public const REL_DNS_PREFETCH = 'dns-prefetch';
  39.     public const REL_DUPLICATE = 'duplicate';
  40.     public const REL_EDIT = 'edit';
  41.     public const REL_EDIT_FORM = 'edit-form';
  42.     public const REL_EDIT_MEDIA = 'edit-media';
  43.     public const REL_ENCLOSURE = 'enclosure';
  44.     public const REL_EXTERNAL = 'external';
  45.     public const REL_FIRST = 'first';
  46.     public const REL_GLOSSARY = 'glossary';
  47.     public const REL_HELP = 'help';
  48.     public const REL_HOSTS = 'hosts';
  49.     public const REL_HUB = 'hub';
  50.     public const REL_ICON = 'icon';
  51.     public const REL_INDEX = 'index';
  52.     public const REL_INTERVALAFTER = 'intervalafter';
  53.     public const REL_INTERVALBEFORE = 'intervalbefore';
  54.     public const REL_INTERVALCONTAINS = 'intervalcontains';
  55.     public const REL_INTERVALDISJOINT = 'intervaldisjoint';
  56.     public const REL_INTERVALDURING = 'intervalduring';
  57.     public const REL_INTERVALEQUALS = 'intervalequals';
  58.     public const REL_INTERVALFINISHEDBY = 'intervalfinishedby';
  59.     public const REL_INTERVALFINISHES = 'intervalfinishes';
  60.     public const REL_INTERVALIN = 'intervalin';
  61.     public const REL_INTERVALMEETS = 'intervalmeets';
  62.     public const REL_INTERVALMETBY = 'intervalmetby';
  63.     public const REL_INTERVALOVERLAPPEDBY = 'intervaloverlappedby';
  64.     public const REL_INTERVALOVERLAPS = 'intervaloverlaps';
  65.     public const REL_INTERVALSTARTEDBY = 'intervalstartedby';
  66.     public const REL_INTERVALSTARTS = 'intervalstarts';
  67.     public const REL_ITEM = 'item';
  68.     public const REL_LAST = 'last';
  69.     public const REL_LATEST_VERSION = 'latest-version';
  70.     public const REL_LICENSE = 'license';
  71.     public const REL_LINKSET = 'linkset';
  72.     public const REL_LRDD = 'lrdd';
  73.     public const REL_MANIFEST = 'manifest';
  74.     public const REL_MASK_ICON = 'mask-icon';
  75.     public const REL_MEDIA_FEED = 'media-feed';
  76.     public const REL_MEMENTO = 'memento';
  77.     public const REL_MICROPUB = 'micropub';
  78.     public const REL_MODULEPRELOAD = 'modulepreload';
  79.     public const REL_MONITOR = 'monitor';
  80.     public const REL_MONITOR_GROUP = 'monitor-group';
  81.     public const REL_NEXT = 'next';
  82.     public const REL_NEXT_ARCHIVE = 'next-archive';
  83.     public const REL_NOFOLLOW = 'nofollow';
  84.     public const REL_NOOPENER = 'noopener';
  85.     public const REL_NOREFERRER = 'noreferrer';
  86.     public const REL_OPENER = 'opener';
  87.     public const REL_OPENID_2_LOCAL_ID = 'openid2.local_id';
  88.     public const REL_OPENID_2_PROVIDER = 'openid2.provider';
  89.     public const REL_ORIGINAL = 'original';
  90.     public const REL_P_3_PV_1 = 'p3pv1';
  91.     public const REL_PAYMENT = 'payment';
  92.     public const REL_PINGBACK = 'pingback';
  93.     public const REL_PRECONNECT = 'preconnect';
  94.     public const REL_PREDECESSOR_VERSION = 'predecessor-version';
  95.     public const REL_PREFETCH = 'prefetch';
  96.     public const REL_PRELOAD = 'preload';
  97.     /**
  98.      * This feature is deprecated and superseded by the Speculation Rules API.
  99.      *
  100.      * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/rel/prerender
  101.      */
  102.     public const REL_PRERENDER = 'prerender';
  103.     public const REL_PREV = 'prev';
  104.     public const REL_PREVIEW = 'preview';
  105.     public const REL_PREVIOUS = 'previous';
  106.     public const REL_PREV_ARCHIVE = 'prev-archive';
  107.     public const REL_PRIVACY_POLICY = 'privacy-policy';
  108.     public const REL_PROFILE = 'profile';
  109.     public const REL_PUBLICATION = 'publication';
  110.     public const REL_RELATED = 'related';
  111.     public const REL_RESTCONF = 'restconf';
  112.     public const REL_REPLIES = 'replies';
  113.     public const REL_RULEINPUT = 'ruleinput';
  114.     public const REL_SEARCH = 'search';
  115.     public const REL_SECTION = 'section';
  116.     public const REL_SELF = 'self';
  117.     public const REL_SERVICE = 'service';
  118.     public const REL_SERVICE_DESC = 'service-desc';
  119.     public const REL_SERVICE_DOC = 'service-doc';
  120.     public const REL_SERVICE_META = 'service-meta';
  121.     public const REL_SIPTRUNKINGCAPABILITY = 'siptrunkingcapability';
  122.     public const REL_SPONSORED = 'sponsored';
  123.     public const REL_START = 'start';
  124.     public const REL_STATUS = 'status';
  125.     public const REL_STYLESHEET = 'stylesheet';
  126.     public const REL_SUBSECTION = 'subsection';
  127.     public const REL_SUCCESSOR_VERSION = 'successor-version';
  128.     public const REL_SUNSET = 'sunset';
  129.     public const REL_TAG = 'tag';
  130.     public const REL_TERMS_OF_SERVICE = 'terms-of-service';
  131.     public const REL_TIMEGATE = 'timegate';
  132.     public const REL_TIMEMAP = 'timemap';
  133.     public const REL_TYPE = 'type';
  134.     public const REL_UGC = 'ugc';
  135.     public const REL_UP = 'up';
  136.     public const REL_VERSION_HISTORY = 'version-history';
  137.     public const REL_VIA = 'via';
  138.     public const REL_WEBMENTION = 'webmention';
  139.     public const REL_WORKING_COPY = 'working-copy';
  140.     public const REL_WORKING_COPY_OF = 'working-copy-of';
  141.     // Extra relations
  142.     public const REL_MERCURE = 'mercure';
  143.     private string $href = '';
  144.     /**
  145.      * @var string[]
  146.      */
  147.     private array $rel = [];
  148.     /**
  149.      * @var array<string, string|bool|string[]>
  150.      */
  151.     private array $attributes = [];
  152.     public function __construct(?string $rel = null, string $href = '')
  153.     {
  154.         if (null !== $rel) {
  155.             $this->rel[$rel] = $rel;
  156.         }
  157.         $this->href = $href;
  158.     }
  159.     public function getHref(): string
  160.     {
  161.         return $this->href;
  162.     }
  163.     public function isTemplated(): bool
  164.     {
  165.         return $this->hrefIsTemplated($this->href);
  166.     }
  167.     public function getRels(): array
  168.     {
  169.         return array_values($this->rel);
  170.     }
  171.     public function getAttributes(): array
  172.     {
  173.         return $this->attributes;
  174.     }
  175.     public function withHref(string|\Stringable $href): static
  176.     {
  177.         $that = clone $this;
  178.         $that->href = $href;
  179.         return $that;
  180.     }
  181.     public function withRel(string $rel): static
  182.     {
  183.         $that = clone $this;
  184.         $that->rel[$rel] = $rel;
  185.         return $that;
  186.     }
  187.     public function withoutRel(string $rel): static
  188.     {
  189.         $that = clone $this;
  190.         unset($that->rel[$rel]);
  191.         return $that;
  192.     }
  193.     public function withAttribute(string $attribute, string|\Stringable|int|float|bool|array $value): static
  194.     {
  195.         $that = clone $this;
  196.         $that->attributes[$attribute] = $value;
  197.         return $that;
  198.     }
  199.     public function withoutAttribute(string $attribute): static
  200.     {
  201.         $that = clone $this;
  202.         unset($that->attributes[$attribute]);
  203.         return $that;
  204.     }
  205.     private function hrefIsTemplated(string $href): bool
  206.     {
  207.         return str_contains($href, '{') || str_contains($href, '}');
  208.     }
  209. }