ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
URI.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 namespace ILIAS\Data;
6 
17 class 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 schema() : 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 authority() : string
227  {
228  $port = $this->port();
229  if ($port === null) {
230  return $this->host();
231  }
232  return $this->host() . ':' . $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 port()
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 host() : 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 path()
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 query()
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 fragment()
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 baseURI() : string
377  {
378  $path = $this->path();
379  if ($path === null) {
380  return $this->schema() . '://' . $this->authority();
381  }
382  return $this->schema() . '://' . $this->authority() . '/' . $path;
383  }
384 }
const UNRESERVED
Definition: URI.php:69
const HOST
Definition: URI.php:79
const BASEURI_PCHAR
Definition: URI.php:73
baseURI()
Get a well-formed URI consisting only out of schema, authority and port.
Definition: URI.php:376
const UNRESERVED_NO_DOT
Definition: URI.php:70
withFragment(string $fragment=null)
Get URI with modified fragment.
Definition: URI.php:362
withHost(string $host)
Get URI with modified host.
Definition: URI.php:295
const PIMP
point|minus|plus to be used in schema.
Definition: URI.php:57
const ALPHA
Relevant character-groups as defined in RFC 3986 Appendix 1.
Definition: URI.php:49
withPath(string $path=null)
Get URI with modified path.
Definition: URI.php:318
const ALPHA_DIGIT
Definition: URI.php:51
const HOST_REG_NAME
Definition: URI.php:77
withQuery(string $query=null)
Get URI with modified query.
Definition: URI.php:340
digestQuery(string $query=null)
Check query formating.
Definition: URI.php:159
const PATH
Definition: proxy_ylocal.php:8
const HEXDIG
Definition: URI.php:52
const PCHAR
Definition: URI.php:72
withSchema(string $schema)
Get URI with modified schema.
Definition: URI.php:214
const DOMAIN_LABEL
Definition: URI.php:76
const PCTENCODED
Definition: URI.php:53
const SUBDELIMS
valid subdelims according to RFC 3986 Appendix 1: "!" "$" "&" "&#39;" "(" ")" "*" "+" "...
Definition: URI.php:63
const QUERY
Definition: URI.php:82
const FRAGMENT
Definition: URI.php:83
const HOST_IPV4
Definition: URI.php:78
digestHost(string $host)
Check host formating.
Definition: URI.php:114
__construct(string $uri_string)
Definition: URI.php:85
const DIGIT
Definition: URI.php:50
The scope of this class is split ilias-conform URI&#39;s into components.
Definition: URI.php:17
checkCorrectFormatOrThrow(string $regexp, string $string)
Check wether a string fits a regexp.
Definition: URI.php:192
const PATH
Definition: URI.php:81
digestPath(string $path=null)
Check path formating.
Definition: URI.php:140
digestPort(int $port=null)
Check port formating.
Definition: URI.php:125
const PORT
Definition: URI.php:80
digestSchema(string $schema)
Check schema formating.
Definition: URI.php:102
withPort(int $port=null)
Get URI with modified port.
Definition: URI.php:273
digestFragment(string $fragment=null)
Check fragment formating.
Definition: URI.php:174
const SCHEMA
Definition: URI.php:75
$authority
const BASEURI_SUBDELIMS
subdelims without jsf**k characters +!() and =
Definition: URI.php:67
withAuthority(string $authority)
Get URI with modified authority.
Definition: URI.php:242
const PATH_DELIM
Definition: URI.php:44