Repository navigation
Expand file tree
/
Copy pathUrlPattern.php
More file actions
219 lines (193 loc) · 7.45 KB
/
Copy pathUrlPattern.php
File metadata and controls
219 lines (193 loc) · 7.45 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
<?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 League\Uri\Contracts\UriInterface;
use League\Uri\Exceptions\SyntaxError;
use League\Uri\UrlPattern\Component;
use League\Uri\UrlPattern\ComponentName;
use League\Uri\UrlPattern\ComponentResult;
use League\Uri\UrlPattern\MatchMode;
use League\Uri\UrlPattern\PatternType;
use League\Uri\UrlPattern\Result;
use Psr\Http\Message\UriInterface as Psr7UriInterface;
use Stringable;
use Throwable;
use Uri\Rfc3986\Uri as Rfc3986Uri;
use Uri\WhatWg\Url as WhatWgUrl;
use ValueError;
use function array_key_exists;
use function array_map;
use function get_debug_type;
/**
* @property-read ?string $scheme
* @property-read ?string $username
* @property-read ?string $password
* @property-read ?string $host
* @property-read ?string $port
* @property-read ?string $path
* @property-read ?string $query
* @property-read ?string $fragment
*/
final class UrlPattern
{
/** @var array<value-of<ComponentName>, Component> $components */
private readonly array $components;
public readonly MatchMode $matchMode;
public readonly bool $hasRegexpGroup;
private readonly bool $hasVariable;
public readonly PatternType $type;
/**
* @param array<non-empty-string, mixed> $patternComponents
*/
public function __construct(array $patternComponents, MatchMode $matchMode = MatchMode::CaseSensitive)
{
$hasRegexpGroup = false;
$hasVariable = false;
$components = [];
foreach (ComponentName::cases() as $name) {
if (!array_key_exists($name->value, $patternComponents)) {
if (ComponentName::Port !== $name) {
$components[$name->value] = Component::fromAsterisk();
continue;
}
$components[ComponentName::Port->value] = array_key_exists(ComponentName::Host->value, $patternComponents)
? Component::fromEmpty()
: Component::fromAsterisk();
continue;
}
$component = $patternComponents[$name->value];
$component instanceof Component || throw new ValueError('the component must be a "'.Component::class.'"; '.get_debug_type($component).' given.');
$components[$name->value] = $component;
$hasRegexpGroup = $hasRegexpGroup || $component->hasRegexpGroup;
$hasVariable = $hasVariable || $component->hasVariable;
}
$this->components = $components;
$this->matchMode = $matchMode;
$this->hasRegexpGroup = $hasRegexpGroup;
$this->hasVariable = $hasVariable;
$this->type = '*' !== $this->components[ComponentName::Scheme->value]->pattern ? PatternType::Absolute : PatternType::Relative;
}
public static function parse(
Stringable|string $pattern,
Rfc3986Uri|WhatWgUrl|BackedEnum|Stringable|string|null $baseUrl = null,
MatchMode $matchMode = MatchMode::CaseSensitive
): ?self {
try {
return self::from($pattern, $baseUrl, $matchMode);
} catch (Throwable) {
return null;
}
}
/**
* @throws SyntaxError
*/
public static function from(
Stringable|string $pattern,
Rfc3986Uri|WhatWgUrl|BackedEnum|Stringable|string|null $baseUrl = null,
MatchMode $matchMode = MatchMode::CaseSensitive
): self {
return UrlPatternBuilder::from($pattern, $baseUrl)
->build(matchMode: $matchMode, type: PatternType::Absolute);
}
public function match(Rfc3986Uri|WhatWgUrl|BackedEnum|Stringable|string $input): bool
{
try {
return $this->extract($input) instanceof Result;
} catch (Throwable) {
return false;
}
}
/**
* @throws SyntaxError
*/
public function extract(Rfc3986Uri|WhatWgUrl|BackedEnum|Stringable|string $input): ?Result
{
$components = self::components($input);
$result = [];
foreach ($this->components as $name => $component) {
$found = ComponentResult::extract($components[$name], $component, ComponentName::from($name), $this->matchMode);
if (null === $found) {
return null;
}
$result[$name] = $found;
}
return Result::tryFrom($result, $input);
}
public function hasVariable(): bool
{
return $this->hasVariable;
}
public function __get(string $name): string
{
$key = ComponentName::tryFrom($name)?->value;
null !== $key || throw new ValueError('the property named "'.$name.'" does not exist.');
return $this->components[$key]->pattern;
}
/**
* @return array<value-of<ComponentName>, string>
*/
public function __debugInfo(): array
{
return array_map(static fn (Component $component): string => $component->pattern, $this->components);
}
/**
* @throws SyntaxError
*
* @return array{
* scheme: string,
* username: string,
* password: string,
* host: string,
* port: string,
* path: string,
* query: string,
* fragment: string
* }
*/
private static function components(WhatWgUrl|Rfc3986Uri|Stringable|BackedEnum|string $input): array
{
if ($input instanceof WhatWgUrl || $input instanceof UriInterface || $input instanceof Rfc3986Uri) {
return [
ComponentName::Scheme->value => (string) $input->getScheme(),
ComponentName::Username->value => (string) $input->getUsername(),
ComponentName::Password->value => (string) $input->getPassword(),
ComponentName::Host->value => (string) ($input instanceof WhatWgUrl ? $input->getAsciiHost() : $input->getHost()),
ComponentName::Port->value => (string) $input->getPort(),
ComponentName::Path->value => $input->getPath(),
ComponentName::Query->value => (string) $input->getQuery(),
ComponentName::Fragment->value => (string) $input->getFragment(),
];
}
if ($input instanceof Psr7UriInterface) {
$username = '';
$password = '';
$userInfo = $input->getUserInfo();
if ('' !== $userInfo) {
[$username, $password] = explode(':', $userInfo, 2) + [1 => ''];
}
return [
ComponentName::Scheme->value => $input->getScheme(),
ComponentName::Username->value => $username,
ComponentName::Password->value => $password,
ComponentName::Host->value => $input->getHost(),
ComponentName::Port->value => (string) $input->getPort(),
ComponentName::Path->value => $input->getPath(),
ComponentName::Query->value => $input->getQuery(),
ComponentName::Fragment->value => $input->getFragment(),
];
}
$components = array_map(static fn (string|int|null $value): string => (string)$value, UriString::parse($input));
$components[ComponentName::Username->value] = $components['user'];
$components[ComponentName::Password->value] = $components['pass'];
return $components;
}
}