ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
URLBuilder.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\UI;
22
24
41{
46 public const URL_MAX_LENGTH = 8192;
50 public const SEPARATOR = '_';
54 private URI $uri;
59 private ?string $fragment = null;
66 private array $params = [];
72 private array $tokens = [];
73
74 public function __construct(URI $uri)
75 {
76 $this->uri = $uri;
77 }
78
82 public function withURI(URI $uri): self
83 {
84 $clone = clone $this;
85 $clone->uri = $uri;
86 return $clone;
87 }
88
92 public function withFragment(?string $fragment): self
93 {
94 $clone = clone $this;
95 $clone->fragment = $fragment;
96 return $clone;
97 }
98
119 public function acquireParameter(array $namespace, string $name, ?string $initial_value = null): array
120 {
121 if ($name === '' || empty($namespace)) {
122 throw new \InvalidArgumentException("Parameter name or namespace not set");
123 }
124
125 $parameter = implode(self::SEPARATOR, $namespace) . self::SEPARATOR . $name;
126 if ($this->parameterExists($parameter)) {
127 throw new \ilException("Parameter '" . $parameter . "' already reserved in URL");
128 }
129
130 $token = new URLBuilderToken($namespace, $name);
131 $clone = clone $this;
132 $clone->params[$parameter] = ($initial_value) ?? '';
133 $clone->tokens[$parameter] = $token;
134
135 return [$clone, $token];
136 }
137
138 public function acquireParameters(array $namespace, string ...$names): array
139 {
140 $tokens = [];
141 $builder = $this;
142 foreach ($names as $name) {
143 list($builder, $token) = $builder->acquireParameter($namespace, $name);
144 $tokens[] = $token;
145 }
146 array_unshift($tokens, $builder);
147 return $tokens;
148 }
149
154 {
155 $this->checkToken($token);
156 $clone = clone $this;
157 unset($clone->params[$token->getName()]);
158 unset($clone->tokens[$token->getName()]);
159
160 return $clone;
161 }
162
166 public function withParameter(URLBuilderToken $token, string|array $value): self
167 {
168 $this->checkToken($token);
169 $clone = clone $this;
170 $clone->params[$token->getName()] = $value;
171
172 return $clone;
173 }
174
183 public function renderTokens(array $tokens): string
184 {
185 $token_render = [];
186 foreach ($tokens as $token) {
187 $token_render[] = '["' . $token->getName() . '",' . $token->render() . ']';
188 }
189 $output = 'new Map([' . implode(',', $token_render) . '])';
190 return $output;
191 }
192
203 public function renderObject(array $tokens): string
204 {
205 $output = 'new il.UI.core.URLBuilder(new URL("' . $this->buildURI() . '"), ' . $this->renderTokens($tokens) . ')';
206 return $output;
207 }
208
214 public function buildURI(): URI
215 {
216 $uri = new URI($this->uri->getBaseURI() . $this->buildQuery() . $this->buildFragment());
217 $this->checkLength($uri);
218 return $uri;
219 }
220
227 private function buildQuery(): string
228 {
229 $params = array_merge($this->uri->getParameters(), $this->params);
230 $query = (! empty($params)) ? '?' . http_build_query($params) : '';
231 $query = preg_replace('/%5B[0-9]+%5D/simU', '%5B%5D', $query);
232 return $query;
233 }
234
238 private function buildFragment(): string
239 {
240 if ($this->fragment !== null) {
241 return ($this->fragment !== '') ? '#' . $this->fragment : '';
242 }
243 $fragment = ($this->uri->getFragment()) ? '#' . $this->uri->getFragment() : '';
244 return $fragment;
245 }
246
250 private function parameterExists(string $name): bool
251 {
252 return array_key_exists($name, $this->params);
253 }
254
261 private function checkToken(URLBuilderToken $token): void
262 {
263 if (! in_array($token, $this->tokens)
264 || $this->tokens[$token->getName()]->getToken() !== $token->getToken()) {
265 throw new \DomainException("Token for '" . $token->getName() . "' is not valid");
266 }
267 if (! $this->parameterExists($token->getName())) {
268 throw new \ilException("Parameter '" . $token->getName() . "' does not exist in URL");
269 }
270 }
271
277 private function checkLength(URI $uri): void
278 {
279 if (! (strlen((string) $uri) <= self::URL_MAX_LENGTH)) {
280 throw new \LengthException("The final URL is longer than " . self::URL_MAX_LENGTH . " and will not be valid.");
281 }
282 }
283}
The scope of this class is split ilias-conform URI's into components.
Definition: URI.php:35
getName()
Get the full name of the token including its namespace.
__construct(URI $uri)
Definition: URLBuilder.php:74
buildFragment()
Create the fragment/hash part of the URL.
Definition: URLBuilder.php:238
buildQuery()
Create the query part of the URL from all parameters Claimed parameters overwrite base parameters in ...
Definition: URLBuilder.php:227
renderTokens(array $tokens)
Renders a Javascript Map of all given tokens.
Definition: URLBuilder.php:183
withURI(URI $uri)
Changes the base URI of the Builder.
Definition: URLBuilder.php:82
parameterExists(string $name)
Check if parameter was already acquired.
Definition: URLBuilder.php:250
array $tokens
Stores all generated tokens for acquired parameters.
Definition: URLBuilder.php:72
acquireParameters(array $namespace, string ... $names)
Definition: URLBuilder.php:138
buildURI()
Get a URI representation of the full URL including query string and fragment/hash.
Definition: URLBuilder.php:214
withFragment(?string $fragment)
Change the fragment/hash part of the URL.
Definition: URLBuilder.php:92
array $params
Stores all acquired parameters These always take precedence over existing parameters in the base URI.
Definition: URLBuilder.php:66
checkToken(URLBuilderToken $token)
Check if a token is valid.
Definition: URLBuilder.php:261
deleteParameter(URLBuilderToken $token)
Delete an acquired parameter if the supplied token is valid.
Definition: URLBuilder.php:153
withParameter(URLBuilderToken $token, string|array $value)
Change an acquired parameter's value if the supplied token is valid.
Definition: URLBuilder.php:166
string $fragment
Stores the URL fragment/hash (#) (always changeable due to its usage)
Definition: URLBuilder.php:59
renderObject(array $tokens)
Renders a Javascript URLBuilder object with changeable parameters for all given tokens.
Definition: URLBuilder.php:203
const SEPARATOR
Separator for parts of a parameter's namespace.
Definition: URLBuilder.php:50
acquireParameter(array $namespace, string $name, ?string $initial_value=null)
Add a new parameter with a namespace and get its token for subsequent changes.
Definition: URLBuilder.php:119
URI $uri
Base URI for the URLBuilder.
Definition: URLBuilder.php:54
const URL_MAX_LENGTH
A maximum length of 8192 characters should be safe to use in most browsers, even though longer URLs w...
Definition: URLBuilder.php:46
checkLength(URI $uri)
Check the full length of the URI against URL_MAX_LENGTH.
Definition: URLBuilder.php:277
if($err=$client->getError()) $namespace
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$token
Definition: xapitoken.php:70