This repository was archived by the owner on Jun 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathUriBuilderBase.php
More file actions
95 lines (87 loc) · 2.38 KB
/
Copy pathUriBuilderBase.php
File metadata and controls
95 lines (87 loc) · 2.38 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
<?hh // strict
/*
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
namespace Facebook\HackRouter;
abstract class UriBuilderBase {
protected ImmVector<UriPatternPart> $parts;
protected ImmMap<string, RequestParameter> $parameters;
private Map<string, string> $values = Map {};
public function __construct(Traversable<UriPatternPart> $parts) {
$this->parts = new ImmVector($parts);
$parameters = Map {};
foreach ($parts as $part) {
if (!$part is RequestParameter) {
continue;
}
$parameters[$part->getName()] = $part;
}
$this->parameters = $parameters->immutable();
}
final protected function getPathImpl(): string {
$uri = '';
foreach ($this->parts as $part) {
if ($part is UriPatternLiteral) {
$uri .= $part->getValue();
continue;
}
invariant(
$part is RequestParameter,
'expecting all UriPatternParts to be literals or parameters, got %s',
\get_class($part),
);
if ($uri === '') {
$uri = '/';
}
$name = $part->getName();
invariant(
$this->values->containsKey($name),
'Parameter "%s" must be set',
$name,
);
$uri .= $this->values->at($name);
}
invariant(
\substr($uri, 0, 1) === '/',
"Path '%s' does not start with '/'",
$uri,
);
return $uri;
}
final protected function setValue<T>(
classname<TypedUriParameter<T>> $parameter_type,
string $name,
T $value,
): this {
$part = $this->parameters[$name] ?? null;
invariant(
$part !== null,
'%s is not a valid parameter - expected one of [%s]',
$name,
\implode(', ', $this->parameters->keys()->map($x ==> "'".$x."'")),
);
invariant(
\is_a($part, $parameter_type),
'Expected %s to be a %s, got a %s',
$name,
$parameter_type,
\get_class($part),
);
$part = \HH\FIXME\UNSAFE_CAST<RequestParameter, TypedUriParameter<T>>(
$part,
'is_a($part, $parameter_type) ~= $part is TypedUriParameter<T>',
);
invariant(
!$this->values->containsKey($name),
'trying to set %s twice',
$name,
);
$this->values[$name] = $part->getUriFragment($value);
return $this;
}
}