forked from thephpleague/uri
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUrn.php
More file actions
602 lines (534 loc) · 18.2 KB
/
Urn.php
File metadata and controls
602 lines (534 loc) · 18.2 KB
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
<?php
/**
* League.Uri (https://uri.thephpleague.com)
*
* (c) Ignace Nyamagana Butera <nyamsprod@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace League\Uri;
use BackedEnum;
use Closure;
use JsonSerializable;
use League\Uri\Contracts\Conditionable;
use League\Uri\Contracts\Transformable;
use League\Uri\Contracts\UriComponentInterface;
use League\Uri\Contracts\UriInterface;
use League\Uri\Exceptions\SyntaxError;
use League\Uri\UriTemplate\Template;
use Stringable;
use Uri\Rfc3986\Uri as Rfc3986Uri;
use Uri\WhatWg\Url as WhatWgUrl;
use function is_bool;
use function preg_match;
use function str_replace;
use function strtolower;
/**
* @phpstan-type UrnSerialize array{0: array{urn: non-empty-string}, 1: array{}}
* @phpstan-import-type InputComponentMap from UriString
* @phpstan-type UrnMap array{
* scheme: 'urn',
* nid: string,
* nss: string,
* r_component: ?string,
* q_component: ?string,
* f_component: ?string,
* }
*/
final class Urn implements Conditionable, Stringable, JsonSerializable, Transformable
{
/**
* RFC8141 regular expression URN splitter.
*
* The regexp does not perform any look-ahead.
* Not all invalid URN are caught. Some
* post-regexp-validation checks
* are mandatory.
*
* @link https://datatracker.ietf.org/doc/html/rfc8141#section-2
*
* @var string
*/
private const REGEXP_URN_PARTS = '/^
urn:
(?<nid>[a-z0-9](?:[a-z0-9-]{0,30}[a-z0-9])?): # NID
(?<nss>.*?) # NSS
(?<frc>\?\+(?<rcomponent>.*?))? # r-component
(?<fqc>\?\=(?<qcomponent>.*?))? # q-component
(?:\#(?<fcomponent>.*))? # f-component
$/xi';
/**
* RFC8141 namespace identifier regular expression.
*
* @link https://datatracker.ietf.org/doc/html/rfc8141#section-2
*
* @var string
*/
private const REGEX_NID_SEQUENCE = '/^[a-z0-9]([a-z0-9-]{0,30})[a-z0-9]$/xi';
/** @var non-empty-string */
private readonly string $uriString;
/** @var non-empty-string */
private readonly string $nid;
/** @var non-empty-string */
private readonly string $nss;
/** @var non-empty-string|null */
private readonly ?string $rComponent;
/** @var non-empty-string|null */
private readonly ?string $qComponent;
/** @var non-empty-string|null */
private readonly ?string $fComponent;
/**
* @param Rfc3986Uri|WhatWgUrl|BackedEnum|Stringable|string $urn the percent-encoded URN
*/
public static function parse(Rfc3986Uri|WhatWgUrl|BackedEnum|Stringable|string $urn): ?Urn
{
try {
return self::fromString($urn);
} catch (SyntaxError) {
return null;
}
}
/**
* @param Rfc3986Uri|WhatWgUrl|Stringable|string $urn the percent-encoded URN
* @see self::fromString()
*
* @throws SyntaxError if the URN is invalid
*/
public static function new(Rfc3986Uri|WhatWgUrl|Stringable|string $urn): self
{
return self::fromString($urn);
}
/**
* @param Rfc3986Uri|WhatWgUrl|BackedEnum|Stringable|string $urn the percent-encoded URN
*
* @throws SyntaxError if the URN is invalid
*/
public static function fromString(Rfc3986Uri|WhatWgUrl|BackedEnum|Stringable|string $urn): self
{
$urn = match (true) {
$urn instanceof Rfc3986Uri => $urn->toRawString(),
$urn instanceof WhatWgUrl => $urn->toAsciiString(),
$urn instanceof BackedEnum => (string) $urn->value,
default => (string) $urn,
};
UriString::containsRfc3986Chars($urn) || throw new SyntaxError('The URN is malformed, it contains invalid characters.');
1 === preg_match(self::REGEXP_URN_PARTS, $urn, $matches) || throw new SyntaxError('The URN string is invalid.');
return new self(
nid: $matches['nid'],
nss: $matches['nss'],
rComponent: (isset($matches['frc']) && '' !== $matches['frc']) ? $matches['rcomponent'] : null,
qComponent: (isset($matches['fqc']) && '' !== $matches['fqc']) ? $matches['qcomponent'] : null,
fComponent: $matches['fcomponent'] ?? null,
);
}
/**
* Create a new instance from a hash representation of the URI similar
* to PHP parse_url function result.
*
* @param InputComponentMap $components a hash representation of the URI similar to PHP parse_url function result
*/
public static function fromComponents(array $components = []): self
{
$components += [
'scheme' => null, 'user' => null, 'pass' => null, 'host' => null,
'port' => null, 'path' => '', 'query' => null, 'fragment' => null,
];
return self::fromString(UriString::build($components));
}
/**
* @param Stringable|string $nss the percent-encoded NSS
*
* @throws SyntaxError if the URN is invalid
*/
public static function fromRfc2141(BackedEnum|Stringable|string $nid, BackedEnum|Stringable|string $nss): self
{
if ($nid instanceof BackedEnum) {
$nid = $nid->value;
}
if ($nss instanceof BackedEnum) {
$nss = $nss->value;
}
return new self((string) $nid, (string) $nss);
}
/**
* @param string $nss the percent-encoded NSS
* @param ?string $rComponent the percent-encoded r-component
* @param ?string $qComponent the percent-encoded q-component
* @param ?string $fComponent the percent-encoded f-component
*
* @throws SyntaxError if one of the URN part is invalid
*/
private function __construct(
string $nid,
string $nss,
?string $rComponent = null,
?string $qComponent = null,
?string $fComponent = null,
) {
('' !== $nid && 1 === preg_match(self::REGEX_NID_SEQUENCE, $nid)) || throw new SyntaxError('The URN is malformed, the NID is invalid.');
('' !== $nss && Encoder::isPathEncoded($nss)) || throw new SyntaxError('The URN is malformed, the NSS is invalid.');
/** @param Closure(string): ?non-empty-string $closure */
$validateComponent = static fn (?string $value, Closure $closure, string $name): ?string => match (true) {
null === $value,
('' !== $value && 1 !== preg_match('/[#?]/', $value) && $closure($value)) => $value,
default => throw new SyntaxError('The URN is malformed, the `'.$name.'` component is invalid.'),
};
$this->nid = $nid;
$this->nss = $nss;
$this->rComponent = $validateComponent($rComponent, Encoder::isPathEncoded(...), 'r-component');
$this->qComponent = $validateComponent($qComponent, Encoder::isQueryEncoded(...), 'q-component');
$this->fComponent = $validateComponent($fComponent, Encoder::isFragmentEncoded(...), 'f-component');
$this->uriString = $this->setUriString();
}
/**
* @return non-empty-string
*/
private function setUriString(): string
{
$str = $this->toRfc2141();
if (null !== $this->rComponent) {
$str .= '?+'.$this->rComponent;
}
if (null !== $this->qComponent) {
$str .= '?='.$this->qComponent;
}
if (null !== $this->fComponent) {
$str .= '#'.$this->fComponent;
}
return $str;
}
/**
* Returns the NID.
*
* @return non-empty-string
*/
public function getNid(): string
{
return $this->nid;
}
/**
* Returns the percent-encoded NSS.
*
* @return non-empty-string
*/
public function getNss(): string
{
return $this->nss;
}
/**
* Returns the percent-encoded r-component string or null if it is not set.
*
* @return ?non-empty-string
*/
public function getRComponent(): ?string
{
return $this->rComponent;
}
/**
* Returns the percent-encoded q-component string or null if it is not set.
*
* @return ?non-empty-string
*/
public function getQComponent(): ?string
{
return $this->qComponent;
}
/**
* Returns the percent-encoded f-component string or null if it is not set.
*
* @return ?non-empty-string
*/
public function getFComponent(): ?string
{
return $this->fComponent;
}
/**
* Returns the RFC8141 URN string representation.
*
* @return non-empty-string
*/
public function toString(): string
{
return $this->uriString;
}
/**
* Returns the RFC2141 URN string representation.
*
* @return non-empty-string
*/
public function toRfc2141(): string
{
return 'urn:'.$this->nid.':'.$this->nss;
}
/**
* Returns the human-readable string representation of the URN as an IRI.
*
* @see https://datatracker.ietf.org/doc/html/rfc3987
*/
public function toDisplayString(): string
{
return UriString::toIriString($this->uriString);
}
/**
* Returns the RFC8141 URN string representation.
*
* @see self::toString()
*
* @return non-empty-string
*/
public function __toString(): string
{
return $this->toString();
}
/**
* Returns the RFC8141 URN string representation.
* @see self::toString()
*
* @return non-empty-string
*/
public function jsonSerialize(): string
{
return $this->toString();
}
/**
* Returns the RFC3986 representation of the current URN.
*
* If a template URI is used the following variables as present
* {nid} for the namespace identifier
* {nss} for the namespace specific string
* {r_component} for the r-component without its delimiter
* {q_component} for the q-component without its delimiter
* {f_component} for the f-component without its delimiter
*/
public function resolve(UriTemplate|Template|BackedEnum|string|null $template = null): UriInterface
{
return null !== $template ? Uri::fromTemplate($template, $this->toComponents()) : Uri::new($this->uriString);
}
public function hasRComponent(): bool
{
return null !== $this->rComponent;
}
public function hasQComponent(): bool
{
return null !== $this->qComponent;
}
public function hasFComponent(): bool
{
return null !== $this->fComponent;
}
public function hasOptionalComponent(): bool
{
return null !== $this->rComponent
|| null !== $this->qComponent
|| null !== $this->fComponent;
}
/**
* Return an instance with the specified NID.
*
* This method MUST retain the state of the current instance, and return
* an instance that contains the specified NID.
*
* @throws SyntaxError for invalid component or transformations
* that would result in an object in invalid state.
*/
public function withNid(BackedEnum|Stringable|string $nid): self
{
if ($nid instanceof BackedEnum) {
$nid = $nid->value;
}
$nid = (string) $nid;
return $this->nid === $nid ? $this : new self(
nid: $nid,
nss: $this->nss,
rComponent: $this->rComponent,
qComponent: $this->qComponent,
fComponent: $this->fComponent,
);
}
/**
* Return an instance with the specified NSS.
*
* This method MUST retain the state of the current instance, and return
* an instance that contains the specified NSS.
*
* @throws SyntaxError for invalid component or transformations
* that would result in an object in invalid state.
*/
public function withNss(BackedEnum|Stringable|string $nss): self
{
$nss = Encoder::encodePath($nss);
return $this->nss === $nss ? $this : new self(
nid: $this->nid,
nss: $nss,
rComponent: $this->rComponent,
qComponent: $this->qComponent,
fComponent: $this->fComponent,
);
}
/**
* Return an instance with the specified r-component.
*
* This method MUST retain the state of the current instance, and return
* an instance that contains the specified r-component.
*
* The component is removed if the value is null.
*
* @throws SyntaxError for invalid component or transformations
* that would result in an object in invalid state.
*/
public function withRComponent(BackedEnum|Stringable|string|null $component): self
{
if ($component instanceof BackedEnum) {
$component = (string) $component->value;
}
if ($component instanceof UriComponentInterface) {
$component = $component->value();
}
if (null !== $component) {
$component = self::formatComponent(Encoder::encodePath($component));
}
return $this->rComponent === $component ? $this : new self(
nid: $this->nid,
nss: $this->nss,
rComponent: $component,
qComponent: $this->qComponent,
fComponent: $this->fComponent,
);
}
private static function formatComponent(?string $component): ?string
{
return null === $component ? null : str_replace(['?', '#'], ['%3F', '%23'], $component);
}
/**
* Return an instance with the specified q-component.
*
* This method MUST retain the state of the current instance, and return
* an instance that contains the specified q-component.
*
* The component is removed if the value is null.
*
* @throws SyntaxError for invalid component or transformations
* that would result in an object in invalid state.
*/
public function withQComponent(BackedEnum|Stringable|string|null $component): self
{
if ($component instanceof UriComponentInterface) {
$component = $component->value();
}
$component = self::formatComponent(Encoder::encodeQueryOrFragment($component));
return $this->qComponent === $component ? $this : new self(
nid: $this->nid,
nss: $this->nss,
rComponent: $this->rComponent,
qComponent: $component,
fComponent: $this->fComponent,
);
}
/**
* Return an instance with the specified f-component.
*
* This method MUST retain the state of the current instance, and return
* an instance that contains the specified f-component.
*
* The component is removed if the value is null.
*
* @throws SyntaxError for invalid component or transformations
* that would result in an object in invalid state.
*/
public function withFComponent(BackedEnum|Stringable|string|null $component): self
{
if ($component instanceof UriComponentInterface) {
$component = $component->value();
}
$component = self::formatComponent(Encoder::encodeQueryOrFragment($component));
return $this->fComponent === $component ? $this : new self(
nid: $this->nid,
nss: $this->nss,
rComponent: $this->rComponent,
qComponent: $this->qComponent,
fComponent: $component,
);
}
public function normalize(): self
{
$copy = new self(
nid: strtolower($this->nid),
nss: (string) Encoder::normalizePath($this->nss),
rComponent: null === $this->rComponent ? $this->rComponent : Encoder::normalizePath($this->rComponent),
qComponent: Encoder::normalizeQuery($this->qComponent),
fComponent: Encoder::normalizeFragment($this->fComponent),
);
return $copy->uriString === $this->uriString ? $this : $copy;
}
public function equals(Urn|Rfc3986Uri|WhatWgUrl|BackedEnum|Stringable|string $other, UrnComparisonMode $urnComparisonMode = UrnComparisonMode::ExcludeComponents): bool
{
if (!$other instanceof Urn) {
$other = self::parse($other);
}
return (null !== $other) && match ($urnComparisonMode) {
UrnComparisonMode::ExcludeComponents => $other->normalize()->toRfc2141() === $this->normalize()->toRfc2141(),
UrnComparisonMode::IncludeComponents => $other->normalize()->toString() === $this->normalize()->toString(),
};
}
public function when(callable|bool $condition, callable $onSuccess, ?callable $onFail = null): static
{
if (!is_bool($condition)) {
$condition = $condition($this);
}
return match (true) {
$condition => $onSuccess($this),
null !== $onFail => $onFail($this),
default => $this,
} ?? $this;
}
public function transform(callable $callback): static
{
return $callback($this);
}
/**
* @return UrnSerialize
*/
public function __serialize(): array
{
return [['urn' => $this->toString()], []];
}
/**
* @param UrnSerialize $data
*
* @throws SyntaxError
*/
public function __unserialize(array $data): void
{
[$properties] = $data;
$uri = self::fromString($properties['urn'] ?? throw new SyntaxError('The `urn` property is missing from the serialized object.'));
$this->nid = $uri->nid;
$this->nss = $uri->nss;
$this->rComponent = $uri->rComponent;
$this->qComponent = $uri->qComponent;
$this->fComponent = $uri->fComponent;
$this->uriString = $uri->uriString;
}
/**
* @return UrnMap
*/
public function toComponents(): array
{
return [
'scheme' => 'urn',
'nid' => $this->nid,
'nss' => $this->nss,
'r_component' => $this->rComponent,
'q_component' => $this->qComponent,
'f_component' => $this->fComponent,
];
}
/**
* @return UrnMap
*/
public function __debugInfo(): array
{
return $this->toComponents();
}
}