|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace JustBetter\Http3EarlyHints\Data; |
| 4 | + |
| 5 | +use Fig\Link\GenericLinkProvider; |
| 6 | +use Fig\Link\Link; |
| 7 | +use Illuminate\Support\Arr; |
| 8 | +use Psr\Link\EvolvableLinkInterface; |
| 9 | +use Psr\Link\EvolvableLinkProviderInterface; |
| 10 | +use Psr\Link\LinkInterface; |
| 11 | + |
| 12 | +class LinkHeaders |
| 13 | +{ |
| 14 | + private EvolvableLinkProviderInterface $linkProvider; |
| 15 | + |
| 16 | + public function __construct(?EvolvableLinkProviderInterface $linkProvider = null) |
| 17 | + { |
| 18 | + $this->linkProvider = $linkProvider ?? new GenericLinkProvider(); |
| 19 | + } |
| 20 | + |
| 21 | + public function getLinkProvider(): EvolvableLinkProviderInterface |
| 22 | + { |
| 23 | + return $this->linkProvider; |
| 24 | + } |
| 25 | + |
| 26 | + public function setLinkProvider(EvolvableLinkProviderInterface $linkProvider): static |
| 27 | + { |
| 28 | + $this->linkProvider = $linkProvider; |
| 29 | + |
| 30 | + return $this; |
| 31 | + } |
| 32 | + |
| 33 | + public function addLink(EvolvableLinkInterface|string|array $uri, string|array|null $rel = null, null|array $attributes = []): static |
| 34 | + { |
| 35 | + if (is_array($uri)) { |
| 36 | + foreach ($uri as $data) { |
| 37 | + $data = Arr::Wrap($data); |
| 38 | + $this->addLink(...$data); |
| 39 | + } |
| 40 | + return $this; |
| 41 | + } |
| 42 | + |
| 43 | + if ($uri instanceof EvolvableLinkInterface) { |
| 44 | + $this->setLinkProvider($this->getLinkProvider()->withLink($uri)); |
| 45 | + return $this; |
| 46 | + } |
| 47 | + |
| 48 | + if ($rel === null) { |
| 49 | + return $this; |
| 50 | + } |
| 51 | + $link = new Link('', $uri); |
| 52 | + |
| 53 | + if (\is_string($rel)) { |
| 54 | + $rel = [$rel]; |
| 55 | + } |
| 56 | + |
| 57 | + foreach ($rel as $value) { |
| 58 | + $link = $link->withRel($value); |
| 59 | + } |
| 60 | + |
| 61 | + foreach ($attributes as $key => $value) { |
| 62 | + $link = $link->withAttribute($key, $value); |
| 63 | + } |
| 64 | + |
| 65 | + $this->setLinkProvider($this->getLinkProvider()->withLink($link)); |
| 66 | + |
| 67 | + return $this; |
| 68 | + } |
| 69 | + |
| 70 | + public function addFromString(string $link) : static |
| 71 | + { |
| 72 | + $links = explode(',', trim($link)); |
| 73 | + foreach ($links as $link) { |
| 74 | + $parts = explode('; ', trim($link)); |
| 75 | + $uri = trim(array_shift($parts), '<>'); |
| 76 | + $rel = null; |
| 77 | + $attributes = []; |
| 78 | + foreach($parts as $part) { |
| 79 | + preg_match('/(?<key>[^=]+)(?:="?(?<value>.*)"?)?/', trim($part), $matches); |
| 80 | + $key = $matches['key']; |
| 81 | + $value = $matches['value'] ?? null; |
| 82 | + |
| 83 | + if($key === 'rel') { |
| 84 | + $rel = $value; |
| 85 | + continue; |
| 86 | + } |
| 87 | + $attributes[$key] = $value ?? true; |
| 88 | + } |
| 89 | + |
| 90 | + $this->addLink($uri, $rel, $attributes); |
| 91 | + } |
| 92 | + |
| 93 | + return $this; |
| 94 | + } |
| 95 | + |
| 96 | + public function makeUnique() |
| 97 | + { |
| 98 | + $handledHashes = []; |
| 99 | + |
| 100 | + foreach ($this->getLinkProvider()->getLinks() as $link) { |
| 101 | + $hash = md5(serialize($link)); |
| 102 | + if (!in_array($hash, $handledHashes)) { |
| 103 | + $handledHashes[] = $hash; |
| 104 | + continue; |
| 105 | + } |
| 106 | + |
| 107 | + $this->setLinkProvider($this->getLinkProvider()->withoutLink($link)); |
| 108 | + } |
| 109 | + |
| 110 | + return $this; |
| 111 | + } |
| 112 | + |
| 113 | + public function __toString() |
| 114 | + { |
| 115 | + return trim(collect($this->getLinkProvider()->getLinks()) |
| 116 | + ->map([static::class, 'linkToString']) |
| 117 | + ->filter() |
| 118 | + ->implode(',')); |
| 119 | + } |
| 120 | + |
| 121 | + public static function linkToString(LinkInterface $link) |
| 122 | + { |
| 123 | + if ($link->isTemplated()) { |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + $attributes = ['', sprintf('rel="%s"', implode(' ', $link->getRels()))]; |
| 128 | + |
| 129 | + foreach ($link->getAttributes() as $key => $value) { |
| 130 | + if (\is_array($value)) { |
| 131 | + foreach ($value as $v) { |
| 132 | + $attributes[] = sprintf('%s="%s"', $key, $v); |
| 133 | + } |
| 134 | + |
| 135 | + continue; |
| 136 | + } |
| 137 | + |
| 138 | + if (!\is_bool($value)) { |
| 139 | + $attributes[] = sprintf('%s="%s"', $key, $value); |
| 140 | + |
| 141 | + continue; |
| 142 | + } |
| 143 | + |
| 144 | + if ($value === true) { |
| 145 | + $attributes[] = $key; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + return sprintf('<%s>%s', $link->getHref(), implode('; ', $attributes)); |
| 150 | + } |
| 151 | +} |
0 commit comments