ILIAS  release_7 Revision v7.30-3-g800a261c036
URI.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
5namespace ILIAS\Data;
6
17class URI
18{
22 protected $schema;
26 protected $host;
30 protected $port;
34 protected $path;
38 protected $query;
42 protected $fragment;
43
44 const PATH_DELIM = '/';
45
49 const ALPHA = '[A-Za-z]';
50 const DIGIT = '[0-9]';
51 const ALPHA_DIGIT = '[A-Za-z0-9]';
52 const HEXDIG = '[0-9A-Fa-f]';
53 const PCTENCODED = '%' . self::HEXDIG . self::HEXDIG;
57 const PIMP = '[\\+\\-\\.]';
58
63 const SUBDELIMS = '[\\$,;=!&\'\\(\\)\\*\\+]';
67 const BASEURI_SUBDELIMS = '[\\$,;&\'\\*]';
68
69 const UNRESERVED = self::ALPHA_DIGIT . '|[\\-\\._~]';
70 const UNRESERVED_NO_DOT = self::ALPHA_DIGIT . '|[\\-_~]';
71
72 const PCHAR = self::UNRESERVED . '|' . self::SUBDELIMS . '|' . self::PCTENCODED . '|:|@';
73 const BASEURI_PCHAR = self::UNRESERVED . '|' . self::BASEURI_SUBDELIMS . '|' . self::PCTENCODED . '|:|@';
74
75 const SCHEMA = '#^' . self::ALPHA . '(' . self::ALPHA_DIGIT . '|' . self::PIMP . ')*$#';
76 const DOMAIN_LABEL = self::ALPHA_DIGIT . '((' . self::UNRESERVED_NO_DOT . '|' . self::PCTENCODED . '|' . self::BASEURI_SUBDELIMS . ')*' . self::ALPHA_DIGIT . ')*';
77 const HOST_REG_NAME = '^' . self::DOMAIN_LABEL . '(\\.' . self::DOMAIN_LABEL . ')*$';
78 const HOST_IPV4 = '^(' . self::DIGIT . '{1,3})(\\.' . self::DIGIT . '{1,3}){3}$';
79 const HOST = '#' . self::HOST_IPV4 . '|' . self::HOST_REG_NAME . '#';
80 const PORT = '#^' . self::DIGIT . '+$#';
81 const PATH = '#^(?!//)(?!:)(' . self::PCHAR . '|' . self::PATH_DELIM . ')+$#';
82 const QUERY = '#^(' . self::PCHAR . '|' . self::PATH_DELIM . '|\\?)+$#';
83 const FRAGMENT = '#^(' . self::PCHAR . '|' . self::PATH_DELIM . '|\\?|\\#)+$#';
84
85 public function __construct(string $uri_string)
86 {
87 $this->schema = $this->digestSchema(parse_url($uri_string, PHP_URL_SCHEME));
88 $this->host = $this->digestHost(parse_url($uri_string, PHP_URL_HOST));
89 $this->port = $this->digestPort(parse_url($uri_string, PHP_URL_PORT));
90 $this->path = $this->digestPath(parse_url($uri_string, PHP_URL_PATH));
91 $this->query = $this->digestQuery(parse_url($uri_string, PHP_URL_QUERY));
92 $this->fragment = $this->digestFragment(parse_url($uri_string, PHP_URL_FRAGMENT));
93 }
94
102 protected function digestSchema(string $schema) : string
103 {
104 return $this->checkCorrectFormatOrThrow(self::SCHEMA, (string) $schema);
105 }
106
114 protected function digestHost(string $host) : string
115 {
116 return $this->checkCorrectFormatOrThrow(self::HOST, (string) $host);
117 }
118
125 protected function digestPort(int $port = null)
126 {
127 if ($port === null) {
128 return null;
129 }
130 return $port;
131 }
132
140 protected function digestPath(string $path = null)
141 {
142 if ($path === null) {
143 return null;
144 }
145 $path = trim($this->checkCorrectFormatOrThrow(self::PATH, $path), self::PATH_DELIM);
146 if ($path === '') {
147 $path = null;
148 }
149 return $path;
150 }
151
159 protected function digestQuery(string $query = null)
160 {
161 if ($query === null) {
162 return null;
163 }
164 return $this->checkCorrectFormatOrThrow(self::QUERY, $query);
165 }
166
174 protected function digestFragment(string $fragment = null)
175 {
176 if ($fragment === null) {
177 return null;
178 }
179 return $this->checkCorrectFormatOrThrow(self::FRAGMENT, $fragment);
180 }
181
182
192 protected function checkCorrectFormatOrThrow(string $regexp, string $string)
193 {
194 if (preg_match($regexp, (string) $string) === 1) {
195 return $string;
196 }
197 throw new \InvalidArgumentException('ill-formated component "' . $string . '" expected "' . $regexp . '"');
198 }
199
203 public function getSchema() : string
204 {
205 return $this->schema;
206 }
207
214 public function withSchema(string $schema) : URI
215 {
216 $shema = $this->digestSchema($schema);
217 $other = clone $this;
218 $other->schema = $schema;
219 return $other;
220 }
221
222
226 public function getAuthority() : string
227 {
228 $port = $this->getPort();
229 if ($port === null) {
230 return $this->getHost();
231 }
232 return $this->getHost() . ':' . $port;
233 }
234
235
242 public function withAuthority(string $authority) : URI
243 {
244 $parts = explode(':', $authority);
245 if (count($parts) > 2) {
246 throw new \InvalidArgumentException('ill-formated component ' . $authority);
247 }
248 $host = $this->digestHost($parts[0]);
249 $port = null;
250 if (array_key_exists(1, $parts)) {
251 $port = (int) $this->checkCorrectFormatOrThrow(self::PORT, (string) $parts[1]);
252 }
253 $other = clone $this;
254 $other->host = $host;
255 $other->port = $port;
256 return $other;
257 }
258
262 public function getPort()
263 {
264 return $this->port;
265 }
266
273 public function withPort(int $port = null) : URI
274 {
275 $port = $this->digestPort($port);
276 $other = clone $this;
277 $other->port = $port;
278 return $other;
279 }
280
284 public function getHost() : string
285 {
286 return $this->host;
287 }
288
295 public function withHost(string $host) : URI
296 {
297 $host = $this->digestHost($host);
298 $other = clone $this;
299 $other->host = $host;
300 return $other;
301 }
302
303
307 public function getPath()
308 {
309 return $this->path;
310 }
311
318 public function withPath(string $path = null) : URI
319 {
320 $path = $this->digestPath($path);
321 $other = clone $this;
322 $other->path = $path;
323 return $other;
324 }
325
329 public function getQuery()
330 {
331 return $this->query;
332 }
333
340 public function withQuery(string $query = null) : URI
341 {
342 $query = $this->digestQuery($query);
343 $other = clone $this;
344 $other->query = $query;
345 return $other;
346 }
347
351 public function getFragment()
352 {
353 return $this->fragment;
354 }
355
362 public function withFragment(string $fragment = null) : URI
363 {
365 $other = clone $this;
366 $other->fragment = $fragment;
367 return $other;
368 }
369
376 public function getBaseURI() : string
377 {
378 $path = $this->getPath();
379 if ($path === null) {
380 return $this->getSchema() . '://' . $this->getAuthority();
381 }
382 return $this->getSchema() . '://' . $this->getAuthority() . '/' . $path;
383 }
384
385 public function __toString() : string
386 {
387 $uri = $this->getBaseURI();
388 $query = $this->getQuery();
389 if ($query) {
390 $uri .= '?' . $query;
391 }
392 $fragment = $this->getFragment();
393 if ($fragment) {
394 $uri .= '#' . $fragment;
395 }
396 return $uri;
397 }
398
402 public function getParameters() : array
403 {
404 $params = [];
405 $query = $this->getQuery();
406 if (!is_null($query)) {
407 parse_str($query, $params);
408 }
409 return $params;
410 }
411
416 public function getParameter(string $param)
417 {
418 $params = $this->getParameters();
419 if (!array_key_exists($param, $params)) {
420 return null;
421 }
422 return $params[$param];
423 }
424
428 public function withParameters(array $parameters) : URI
429 {
430 return $this->withQuery(
431 http_build_query($parameters)
432 );
433 }
434
438 public function withParameter(string $key, $value) : URI
439 {
440 $params = $this->getParameters();
441 $params[$key] = $value;
442 return $this->withParameters($params);
443 }
444}
An exception for terminatinating execution or to throw for unit testing.
The scope of this class is split ilias-conform URI's into components.
Definition: URI.php:18
withParameters(array $parameters)
Get URI with modified parameters.
Definition: URI.php:428
__construct(string $uri_string)
Definition: URI.php:85
getParameter(string $param)
Get the value of the given parameter (or null)
Definition: URI.php:416
withPath(string $path=null)
Get URI with modified path.
Definition: URI.php:318
withPort(int $port=null)
Get URI with modified port.
Definition: URI.php:273
digestPath(string $path=null)
Check path formating.
Definition: URI.php:140
const PORT
Definition: URI.php:80
const BASEURI_PCHAR
Definition: URI.php:73
__toString()
Definition: URI.php:385
withSchema(string $schema)
Get URI with modified schema.
Definition: URI.php:214
withQuery(string $query=null)
Get URI with modified query.
Definition: URI.php:340
const ALPHA_DIGIT
Definition: URI.php:51
const PATH
Definition: URI.php:81
const PATH_DELIM
Definition: URI.php:44
const BASEURI_SUBDELIMS
subdelims without jsf**k characters +!() and =
Definition: URI.php:67
const HOST_REG_NAME
Definition: URI.php:77
digestPort(int $port=null)
Check port formating.
Definition: URI.php:125
digestHost(string $host)
Check host formating.
Definition: URI.php:114
digestFragment(string $fragment=null)
Check fragment formating.
Definition: URI.php:174
const FRAGMENT
Definition: URI.php:83
const DOMAIN_LABEL
Definition: URI.php:76
digestSchema(string $schema)
Check schema formating.
Definition: URI.php:102
const ALPHA
Relevant character-groups as defined in RFC 3986 Appendix 1.
Definition: URI.php:49
const PCHAR
Definition: URI.php:72
checkCorrectFormatOrThrow(string $regexp, string $string)
Check wether a string fits a regexp.
Definition: URI.php:192
withFragment(string $fragment=null)
Get URI with modified fragment.
Definition: URI.php:362
const PIMP
point|minus|plus to be used in schema.
Definition: URI.php:57
withParameter(string $key, $value)
Get URI with modified parameters.
Definition: URI.php:438
getFragment()
Definition: URI.php:351
const HOST
Definition: URI.php:79
digestQuery(string $query=null)
Check query formating.
Definition: URI.php:159
const HOST_IPV4
Definition: URI.php:78
getBaseURI()
Get a well-formed URI consisting only of schema, authority and port.
Definition: URI.php:376
const UNRESERVED
Definition: URI.php:69
const HEXDIG
Definition: URI.php:52
const UNRESERVED_NO_DOT
Definition: URI.php:70
const QUERY
Definition: URI.php:82
getParameters()
Get all parameters as associative array.
Definition: URI.php:402
const SCHEMA
Definition: URI.php:75
withHost(string $host)
Get URI with modified host.
Definition: URI.php:295
const DIGIT
Definition: URI.php:50
withAuthority(string $authority)
Get URI with modified authority.
Definition: URI.php:242
const PCTENCODED
Definition: URI.php:53
getAuthority()
Definition: URI.php:226
const SUBDELIMS
valid subdelims according to RFC 3986 Appendix 1: "!" "$" "&" "'" "(" ")" "*" "+" ",...
Definition: URI.php:63
const PATH
Definition: proxy_ylocal.php:8
$param
Definition: xapitoken.php:29