Repository navigation
Expand file tree
/
Copy pathUrlPatternBuilder.php
More file actions
248 lines (201 loc) · 7.3 KB
/
Copy pathUrlPatternBuilder.php
File metadata and controls
248 lines (201 loc) · 7.3 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
<?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\Conditionable;
use League\Uri\UrlPattern\Component;
use League\Uri\UrlPattern\MatchMode;
use League\Uri\UrlPattern\Parser;
use Stringable;
use Uri\Rfc3986\Uri as Rfc3986Uri;
use Uri\WhatWg\Url as WhatWgUrl;
use function array_map;
use function is_bool;
use function strrpos;
use function substr;
final class UrlPatternBuilder implements Conditionable
{
private ?Component $scheme = null;
private ?Component $username = null;
private ?Component $password = null;
private ?Component $host = null;
private ?Component $port = null;
private ?Component $path = null;
private ?Component $query = null;
private ?Component $fragment = null;
private MatchMode $matchMode = MatchMode::CaseSensitive;
public function __construct()
{
$this->reset();
}
public function reset(): void
{
$this->scheme = null;
$this->username = null;
$this->password = null;
$this->host = null;
$this->port = null;
$this->path = null;
$this->query = null;
$this->fragment = null;
$this->matchMode = MatchMode::CaseSensitive;
}
public static function from(Stringable|string $pattern, MatchMode $matchMode = MatchMode::CaseSensitive): self
{
$components = (new Parser((string) $pattern))->components();
return (new self())
->scheme($components['scheme'] ?? null)
->username($components['username'] ?? null)
->password($components['password'] ?? null)
->host($components['host'] ?? null)
->port($components['port'] ?? null)
->path($components['path'] ?? null)
->query($components['query'] ?? null)
->fragment($components['fragment'] ?? null)
->when(
MatchMode::CaseInsensitive == $matchMode,
static fn (self $builder): self => $builder->ignoreCase(),
static fn (self $builder): self => $builder->preserveCase(),
);
}
public function build(Rfc3986Uri|WhatWgUrl|BackedEnum|Stringable|string|null $baseUrl = null): UrlPattern
{
$components = [
'scheme' => $this->scheme ?? null,
'username' => $this->username ?? null,
'password' => $this->password ?? null,
'host' => $this->host ?? null,
'port' => $this->port ?? null,
'path' => $this->path ?? null,
'query' => $this->query ?? null,
'fragment' => $this->fragment ?? null,
];
if (null !== $baseUrl) {
$components = self::applyBaseUrl($components, $baseUrl);
}
$components = array_map(
static fn (?Component $component): Component => $component ?? Component::fromAsterisk(),
$components
);
return new UrlPattern($components, $this->matchMode);
}
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;
}
private static function applyBaseUrl(array $components, Rfc3986Uri|WhatWgUrl|BackedEnum|Stringable|string $baseUrl): array
{
$baseComponents = UriString::parse(match (true) {
$baseUrl instanceof Rfc3986Uri => $baseUrl->toRawString(),
$baseUrl instanceof WhatWgUrl => $baseUrl->toAsciiString(),
default => $baseUrl,
});
$hasScheme = isset($components['scheme']);
$hasHost = isset($components['host']);
$hasPort = isset($components['port']);
if (! $hasScheme) {
$components['scheme'] = null !== $baseComponents['scheme']
? Component::fromPattern($baseComponents['scheme'])
: Component::fromAsterisk();
}
if (! $hasScheme && ! $hasHost) {
$components['host'] = null !== $baseComponents['host']
? Component::fromPattern($baseComponents['host'])
: Component::fromAsterisk();
}
if (! $hasScheme && ! $hasHost && ! $hasPort) {
$components['port'] = null !== $baseComponents['port']
? Component::fromPattern((string) $baseComponents['port'])
: Component::fromAsterisk();
}
$components['path'] = match (true) {
!isset($components['path']) => null !== $baseComponents['path'] ? Component::fromPattern($baseComponents['path']) : Component::fromAsterisk(),
default => self::resolvePathname($components['path'], $baseComponents['path']),
};
return $components;
}
private static function resolvePathname(Component $pathname, string $basePathname): Component
{
if (str_starts_with($pathname->pattern, '/')) {
return $pathname;
}
$slash = strrpos($basePathname, '/');
$pattern = false === $slash
? $pathname->pattern
: substr($basePathname, 0, $slash + 1).$pathname->pattern;
return Component::fromPattern($pattern);
}
private static function filter(Component|Stringable|string|null $component): ?Component
{
return match (true) {
$component instanceof Component => $component,
null === $component => null,
default => Component::fromPattern((string) $component),
};
}
public function scheme(Component|Stringable|string|null $scheme): self
{
$this->scheme = self::filter($scheme);
return $this;
}
public function username(Component|Stringable|string|null $username): self
{
$this->username = self::filter($username);
return $this;
}
public function password(Component|Stringable|string|null $password): self
{
$this->password = self::filter($password);
return $this;
}
public function host(Component|Stringable|string|null $host): self
{
$this->host = self::filter($host);
return $this;
}
public function port(Component|Stringable|string|null $port): self
{
$this->path = self::filter($port);
return $this;
}
public function path(Component|Stringable|string|null $path): self
{
$this->path = self::filter($path);
return $this;
}
public function query(Component|Stringable|string|null $query): self
{
$this->query = self::filter($query);
return $this;
}
public function fragment(Component|Stringable|string|null $fragment): self
{
$this->fragment = self::filter($fragment);
return $this;
}
public function ignoreCase(): self
{
$this->matchMode = MatchMode::CaseInsensitive;
return $this;
}
public function preserveCase(): self
{
$this->matchMode = MatchMode::CaseSensitive;
return $this;
}
}