forked from ILIAS-eLearning/ILIAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathURI.php
346 lines (307 loc) · 9.54 KB
/
URI.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
declare(strict_types=1);
namespace ILIAS\Data;
/**
* The scope of this class is split ilias-conform URI's into components.
* Please refer to RFC 3986 for details.
* Notice, ilias-confor URI's will form a SUBSET of RFC 3986:
* - Notice the restrictions on baseuri-subdelims.
* - We require a schema and an authority to be present.
* - If any part is located and it is invalid an exception will be thrown
* instead of just omiting it.
* - IPv6 is currently not supported.
*/
class URI
{
private const PATH_DELIM = '/';
/**
* Relevant character-groups as defined in RFC 3986 Appendix 1
*/
private const ALPHA = '[A-Za-z]';
private const DIGIT = '[0-9]';
private const ALPHA_DIGIT = '[A-Za-z0-9]';
private const HEXDIG = '[0-9A-Fa-f]';
private const PCTENCODED = '%' . self::HEXDIG . self::HEXDIG;
/**
* point|minus|plus to be used in schema.
*/
private const PIMP = '[\\+\\-\\.]';
/**
* valid subdelims according to RFC 3986 Appendix 1:
* "!" "$" "&" "'" "(" ")" "*" "+" "," ";" "="
*/
private const SUBDELIMS = '[\\$,;=!&\'\\(\\)\\*\\+]';
/**
* subdelims without jsf**k characters +!() and =
*/
private const BASEURI_SUBDELIMS = '[\\$,;&\'\\*]';
private const UNRESERVED = self::ALPHA_DIGIT . '|[\\-\\._~]';
private const UNRESERVED_NO_DOT = self::ALPHA_DIGIT . '|[\\-_~]';
private const PCHAR = self::UNRESERVED . '|' . self::SUBDELIMS . '|' . self::PCTENCODED . '|:|@';
private const BASEURI_PCHAR = self::UNRESERVED . '|' . self::BASEURI_SUBDELIMS . '|' . self::PCTENCODED . '|:|@';
private const SCHEMA = '#^' . self::ALPHA . '(' . self::ALPHA_DIGIT . '|' . self::PIMP . ')*$#';
private const DOMAIN_LABEL = self::ALPHA_DIGIT . '((' . self::UNRESERVED_NO_DOT . '|' . self::PCTENCODED . '|' . self::BASEURI_SUBDELIMS . ')*' . self::ALPHA_DIGIT . ')*';
private const HOST_REG_NAME = '^' . self::DOMAIN_LABEL . '(\\.' . self::DOMAIN_LABEL . ')*$';
private const HOST_IPV4 = '^(' . self::DIGIT . '{1,3})(\\.' . self::DIGIT . '{1,3}){3}$';
private const HOST = '#' . self::HOST_IPV4 . '|' . self::HOST_REG_NAME . '#';
private const PORT = '#^' . self::DIGIT . '+$#';
private const PATH = '#^(?!//)(?!:)(' . self::PCHAR . '|' . self::PATH_DELIM . ')+$#';
private const QUERY = '#^(' . self::PCHAR . '|' . self::PATH_DELIM . '|\\?)+$#';
private const FRAGMENT = '#^(' . self::PCHAR . '|' . self::PATH_DELIM . '|\\?|\\#)+$#';
protected string $schema;
protected string $host;
protected ?int $port;
protected ?string $path;
protected ?string $query;
protected ?string $fragment;
public function __construct(string $uri_string)
{
$this->schema = $this->digestSchema(parse_url($uri_string, PHP_URL_SCHEME));
$this->host = $this->digestHost(parse_url($uri_string, PHP_URL_HOST));
$this->port = $this->digestPort(parse_url($uri_string, PHP_URL_PORT));
$this->path = $this->digestPath(parse_url($uri_string, PHP_URL_PATH));
$this->query = $this->digestQuery(parse_url($uri_string, PHP_URL_QUERY));
$this->fragment = $this->digestFragment(parse_url($uri_string, PHP_URL_FRAGMENT));
}
/**
* Check schema formating. Return it in case of success.
*/
protected function digestSchema(string $schema): string
{
return $this->checkCorrectFormatOrThrow(self::SCHEMA, $schema);
}
/**
* Check host formating. Return it in case of success.
*/
protected function digestHost(string $host): string
{
return $this->checkCorrectFormatOrThrow(self::HOST, $host);
}
/**
* Check port formating. Return it in case of success.
*/
protected function digestPort(int $port = null): ?int
{
return $port ?? null;
}
/**
* Check path formating. Return it in case of success.
*/
protected function digestPath(string $path = null): ?string
{
if ($path === null) {
return null;
}
$path = trim($this->checkCorrectFormatOrThrow(self::PATH, $path), self::PATH_DELIM);
if ($path === '') {
$path = null;
}
return $path;
}
/**
* Check query formating. Return it in case of success.
*/
protected function digestQuery(string $query = null): ?string
{
if ($query === null) {
return null;
}
return $this->checkCorrectFormatOrThrow(self::QUERY, $query);
}
/**
* Check fragment formating. Return it in case of success.
*/
protected function digestFragment(string $fragment = null): ?string
{
if ($fragment === null) {
return null;
}
return $this->checkCorrectFormatOrThrow(self::FRAGMENT, $fragment);
}
/**
* Check wether a string fits a regexp. Return it, if so,
* throw otherwise.
*/
protected function checkCorrectFormatOrThrow(string $regexp, string $string): string
{
if (preg_match($regexp, $string) === 1) {
return $string;
}
throw new \InvalidArgumentException('ill-formated component "' . $string . '" expected "' . $regexp . '"');
}
public function getSchema(): string
{
return $this->schema;
}
/**
* Get URI with modified schema
*/
public function withSchema(string $schema): URI
{
$shema = $this->digestSchema($schema);
$other = clone $this;
$other->schema = $schema;
return $other;
}
public function getAuthority(): string
{
$port = $this->getPort();
if ($port === null) {
return $this->getHost();
}
return $this->getHost() . ':' . $port;
}
/**
* Get URI with modified authority
*/
public function withAuthority(string $authority): URI
{
$parts = explode(':', $authority);
if (count($parts) > 2) {
throw new \InvalidArgumentException('ill-formated component ' . $authority);
}
$host = $this->digestHost($parts[0]);
$port = null;
if (array_key_exists(1, $parts)) {
$port = (int) $this->checkCorrectFormatOrThrow(self::PORT, (string) $parts[1]);
}
$other = clone $this;
$other->host = $host;
$other->port = $port;
return $other;
}
public function getPort(): ?int
{
return $this->port;
}
/**
* Get URI with modified port
*/
public function withPort(int $port = null): URI
{
$port = $this->digestPort($port);
$other = clone $this;
$other->port = $port;
return $other;
}
public function getHost(): string
{
return $this->host;
}
/**
* Get URI with modified host
*/
public function withHost(string $host): URI
{
$host = $this->digestHost($host);
$other = clone $this;
$other->host = $host;
return $other;
}
public function getPath(): ?string
{
return $this->path;
}
/**
* Get URI with modified path
*/
public function withPath(string $path = null): URI
{
$path = $this->digestPath($path);
$other = clone $this;
$other->path = $path;
return $other;
}
public function getQuery(): ?string
{
return $this->query;
}
/**
* Get URI with modified query
*/
public function withQuery(string $query = null): URI
{
$query = $this->digestQuery($query);
$other = clone $this;
$other->query = $query;
return $other;
}
public function getFragment(): ?string
{
return $this->fragment;
}
/**
* Get URI with modified fragment
*/
public function withFragment(string $fragment = null): URI
{
$fragment = $this->digestFragment($fragment);
$other = clone $this;
$other->fragment = $fragment;
return $other;
}
/**
* Get a well-formed URI consisting only of
* schema, authority and port.
*/
public function getBaseURI(): string
{
$path = $this->getPath();
if ($path === null) {
return $this->getSchema() . '://' . $this->getAuthority();
}
return $this->getSchema() . '://' . $this->getAuthority() . '/' . $path;
}
public function __toString(): string
{
$uri = $this->getBaseURI();
$query = $this->getQuery();
if ($query) {
$uri .= '?' . $query;
}
$fragment = $this->getFragment();
if ($fragment) {
$uri .= '#' . $fragment;
}
return $uri;
}
/**
* Get all parameters as associative array
*/
public function getParameters(): array
{
$params = [];
$query = $this->getQuery();
if (!is_null($query)) {
parse_str($query, $params);
}
return $params;
}
/**
* Get the value of the given parameter (or null)
* @return mixed|null
*/
public function getParameter(string $param)
{
$params = $this->getParameters();
return $params[$param] ?? null;
}
/**
* Get URI with modified parameters
*/
public function withParameters(array $parameters): URI
{
return $this->withQuery(
http_build_query($parameters)
);
}
/**
* Get URI with modified parameters
*/
public function withParameter(string $key, $value): URI
{
$params = $this->getParameters();
$params[$key] = $value;
return $this->withParameters($params);
}
}