1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* League.Uri (https://uri.thephpleague.com) |
5
|
|
|
* |
6
|
|
|
* (c) Ignace Nyamagana Butera <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace League\Uri; |
15
|
|
|
|
16
|
|
|
use League\Uri\Contracts\UriException; |
17
|
|
|
use League\Uri\Contracts\UriInterface; |
18
|
|
|
use League\Uri\Exceptions\SyntaxError; |
19
|
|
|
use League\Uri\Exceptions\TemplateCanNotBeExpanded; |
20
|
|
|
use League\Uri\UriTemplate\Template; |
21
|
|
|
use League\Uri\UriTemplate\VariableBag; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Defines the URI Template syntax and the process for expanding a URI Template into a URI reference. |
25
|
|
|
* |
26
|
|
|
* @link https://tools.ietf.org/html/rfc6570 |
27
|
|
|
* @package League\Uri |
28
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
29
|
|
|
* @since 6.1.0 |
30
|
|
|
* |
31
|
|
|
* Based on GuzzleHttp\UriTemplate class in Guzzle v6.5. |
32
|
|
|
* @link https://github.com/guzzle/guzzle/blob/6.5/src/UriTemplate.php |
33
|
|
|
*/ |
34
|
|
|
final class UriTemplate |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var Template |
38
|
|
|
*/ |
39
|
|
|
private $template; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var VariableBag |
43
|
|
|
*/ |
44
|
|
|
private $defaultVariables; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param object|string $template a string or an object with the __toString method |
48
|
|
|
* |
49
|
|
|
* @throws \TypeError if the template is not a string or an object with the __toString method |
50
|
|
|
* @throws SyntaxError if the template syntax is invalid |
51
|
|
|
* @throws TemplateCanNotBeExpanded if the template variables are invalid |
52
|
|
|
*/ |
53
|
6 |
|
public function __construct($template, array $defaultVariables = []) |
54
|
|
|
{ |
55
|
6 |
|
$this->template = Template::createFromString($template); |
56
|
6 |
|
$this->defaultVariables = $this->filterVariables($defaultVariables); |
57
|
6 |
|
} |
58
|
|
|
|
59
|
2 |
|
public static function __set_state(array $properties): self |
60
|
|
|
{ |
61
|
2 |
|
return new self($properties['template']->toString(), $properties['defaultVariables']->all()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Filters out variables for the given template. |
66
|
|
|
* |
67
|
|
|
* @param array<string,string|array<string>> $variables |
68
|
|
|
*/ |
69
|
10 |
|
private function filterVariables(array $variables): VariableBag |
70
|
|
|
{ |
71
|
10 |
|
$output = new VariableBag(); |
72
|
10 |
|
foreach ($this->template->variableNames() as $name) { |
73
|
10 |
|
if (isset($variables[$name])) { |
74
|
10 |
|
$output->assign($name, $variables[$name]); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
10 |
|
return $output; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* The template string. |
83
|
|
|
*/ |
84
|
2 |
|
public function getTemplate(): string |
85
|
|
|
{ |
86
|
2 |
|
return $this->template->toString(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Returns the names of the variables in the template, in order. |
91
|
|
|
* |
92
|
|
|
* @return string[] |
93
|
|
|
*/ |
94
|
8 |
|
public function getVariableNames(): array |
95
|
|
|
{ |
96
|
8 |
|
return $this->template->variableNames(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Returns the default values used to expand the template. |
101
|
|
|
* |
102
|
|
|
* The returned list only contains variables whose name is part of the current template. |
103
|
|
|
* |
104
|
|
|
* @return array<string,string|array> |
105
|
|
|
*/ |
106
|
2 |
|
public function getDefaultVariables(): array |
107
|
|
|
{ |
108
|
2 |
|
return $this->defaultVariables->all(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Returns a new instance with the updated default variables. |
113
|
|
|
* |
114
|
|
|
* This method MUST retain the state of the current instance, and return |
115
|
|
|
* an instance that contains the modified default variables. |
116
|
|
|
* |
117
|
|
|
* If present, variables whose name is not part of the current template |
118
|
|
|
* possible variable names are removed. |
119
|
|
|
*/ |
120
|
2 |
|
public function withDefaultVariables(array $defaultDefaultVariables): self |
121
|
|
|
{ |
122
|
2 |
|
$clone = clone $this; |
123
|
2 |
|
$clone->defaultVariables = $this->filterVariables($defaultDefaultVariables); |
124
|
|
|
|
125
|
2 |
|
return $clone; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @throws TemplateCanNotBeExpanded if the variable contains nested array values |
130
|
|
|
* @throws UriException if the resulting expansion can not be converted to a UriInterface instance |
131
|
|
|
*/ |
132
|
158 |
|
public function expand(array $variables = []): UriInterface |
133
|
|
|
{ |
134
|
158 |
|
$uriString = $this->template->expand( |
135
|
158 |
|
$this->filterVariables($variables)->replace($this->defaultVariables) |
136
|
|
|
); |
137
|
|
|
|
138
|
158 |
|
return Uri::createFromString($uriString); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|