Chris@0
|
1 <?php
|
Chris@0
|
2 namespace GuzzleHttp;
|
Chris@0
|
3
|
Chris@0
|
4 /**
|
Chris@0
|
5 * Expands URI templates. Userland implementation of PECL uri_template.
|
Chris@0
|
6 *
|
Chris@0
|
7 * @link http://tools.ietf.org/html/rfc6570
|
Chris@0
|
8 */
|
Chris@0
|
9 class UriTemplate
|
Chris@0
|
10 {
|
Chris@0
|
11 /** @var string URI template */
|
Chris@0
|
12 private $template;
|
Chris@0
|
13
|
Chris@0
|
14 /** @var array Variables to use in the template expansion */
|
Chris@0
|
15 private $variables;
|
Chris@0
|
16
|
Chris@0
|
17 /** @var array Hash for quick operator lookups */
|
Chris@0
|
18 private static $operatorHash = [
|
Chris@0
|
19 '' => ['prefix' => '', 'joiner' => ',', 'query' => false],
|
Chris@0
|
20 '+' => ['prefix' => '', 'joiner' => ',', 'query' => false],
|
Chris@0
|
21 '#' => ['prefix' => '#', 'joiner' => ',', 'query' => false],
|
Chris@0
|
22 '.' => ['prefix' => '.', 'joiner' => '.', 'query' => false],
|
Chris@0
|
23 '/' => ['prefix' => '/', 'joiner' => '/', 'query' => false],
|
Chris@0
|
24 ';' => ['prefix' => ';', 'joiner' => ';', 'query' => true],
|
Chris@0
|
25 '?' => ['prefix' => '?', 'joiner' => '&', 'query' => true],
|
Chris@0
|
26 '&' => ['prefix' => '&', 'joiner' => '&', 'query' => true]
|
Chris@0
|
27 ];
|
Chris@0
|
28
|
Chris@0
|
29 /** @var array Delimiters */
|
Chris@0
|
30 private static $delims = [':', '/', '?', '#', '[', ']', '@', '!', '$',
|
Chris@0
|
31 '&', '\'', '(', ')', '*', '+', ',', ';', '='];
|
Chris@0
|
32
|
Chris@0
|
33 /** @var array Percent encoded delimiters */
|
Chris@0
|
34 private static $delimsPct = ['%3A', '%2F', '%3F', '%23', '%5B', '%5D',
|
Chris@0
|
35 '%40', '%21', '%24', '%26', '%27', '%28', '%29', '%2A', '%2B', '%2C',
|
Chris@0
|
36 '%3B', '%3D'];
|
Chris@0
|
37
|
Chris@0
|
38 public function expand($template, array $variables)
|
Chris@0
|
39 {
|
Chris@0
|
40 if (false === strpos($template, '{')) {
|
Chris@0
|
41 return $template;
|
Chris@0
|
42 }
|
Chris@0
|
43
|
Chris@0
|
44 $this->template = $template;
|
Chris@0
|
45 $this->variables = $variables;
|
Chris@0
|
46
|
Chris@0
|
47 return preg_replace_callback(
|
Chris@0
|
48 '/\{([^\}]+)\}/',
|
Chris@0
|
49 [$this, 'expandMatch'],
|
Chris@0
|
50 $this->template
|
Chris@0
|
51 );
|
Chris@0
|
52 }
|
Chris@0
|
53
|
Chris@0
|
54 /**
|
Chris@0
|
55 * Parse an expression into parts
|
Chris@0
|
56 *
|
Chris@0
|
57 * @param string $expression Expression to parse
|
Chris@0
|
58 *
|
Chris@0
|
59 * @return array Returns an associative array of parts
|
Chris@0
|
60 */
|
Chris@0
|
61 private function parseExpression($expression)
|
Chris@0
|
62 {
|
Chris@0
|
63 $result = [];
|
Chris@0
|
64
|
Chris@0
|
65 if (isset(self::$operatorHash[$expression[0]])) {
|
Chris@0
|
66 $result['operator'] = $expression[0];
|
Chris@0
|
67 $expression = substr($expression, 1);
|
Chris@0
|
68 } else {
|
Chris@0
|
69 $result['operator'] = '';
|
Chris@0
|
70 }
|
Chris@0
|
71
|
Chris@0
|
72 foreach (explode(',', $expression) as $value) {
|
Chris@0
|
73 $value = trim($value);
|
Chris@0
|
74 $varspec = [];
|
Chris@0
|
75 if ($colonPos = strpos($value, ':')) {
|
Chris@0
|
76 $varspec['value'] = substr($value, 0, $colonPos);
|
Chris@0
|
77 $varspec['modifier'] = ':';
|
Chris@0
|
78 $varspec['position'] = (int) substr($value, $colonPos + 1);
|
Chris@0
|
79 } elseif (substr($value, -1) === '*') {
|
Chris@0
|
80 $varspec['modifier'] = '*';
|
Chris@0
|
81 $varspec['value'] = substr($value, 0, -1);
|
Chris@0
|
82 } else {
|
Chris@0
|
83 $varspec['value'] = (string) $value;
|
Chris@0
|
84 $varspec['modifier'] = '';
|
Chris@0
|
85 }
|
Chris@0
|
86 $result['values'][] = $varspec;
|
Chris@0
|
87 }
|
Chris@0
|
88
|
Chris@0
|
89 return $result;
|
Chris@0
|
90 }
|
Chris@0
|
91
|
Chris@0
|
92 /**
|
Chris@0
|
93 * Process an expansion
|
Chris@0
|
94 *
|
Chris@0
|
95 * @param array $matches Matches met in the preg_replace_callback
|
Chris@0
|
96 *
|
Chris@0
|
97 * @return string Returns the replacement string
|
Chris@0
|
98 */
|
Chris@0
|
99 private function expandMatch(array $matches)
|
Chris@0
|
100 {
|
Chris@0
|
101 static $rfc1738to3986 = ['+' => '%20', '%7e' => '~'];
|
Chris@0
|
102
|
Chris@0
|
103 $replacements = [];
|
Chris@0
|
104 $parsed = self::parseExpression($matches[1]);
|
Chris@0
|
105 $prefix = self::$operatorHash[$parsed['operator']]['prefix'];
|
Chris@0
|
106 $joiner = self::$operatorHash[$parsed['operator']]['joiner'];
|
Chris@0
|
107 $useQuery = self::$operatorHash[$parsed['operator']]['query'];
|
Chris@0
|
108
|
Chris@0
|
109 foreach ($parsed['values'] as $value) {
|
Chris@0
|
110 if (!isset($this->variables[$value['value']])) {
|
Chris@0
|
111 continue;
|
Chris@0
|
112 }
|
Chris@0
|
113
|
Chris@0
|
114 $variable = $this->variables[$value['value']];
|
Chris@0
|
115 $actuallyUseQuery = $useQuery;
|
Chris@0
|
116 $expanded = '';
|
Chris@0
|
117
|
Chris@0
|
118 if (is_array($variable)) {
|
Chris@0
|
119 $isAssoc = $this->isAssoc($variable);
|
Chris@0
|
120 $kvp = [];
|
Chris@0
|
121 foreach ($variable as $key => $var) {
|
Chris@0
|
122 if ($isAssoc) {
|
Chris@0
|
123 $key = rawurlencode($key);
|
Chris@0
|
124 $isNestedArray = is_array($var);
|
Chris@0
|
125 } else {
|
Chris@0
|
126 $isNestedArray = false;
|
Chris@0
|
127 }
|
Chris@0
|
128
|
Chris@0
|
129 if (!$isNestedArray) {
|
Chris@0
|
130 $var = rawurlencode($var);
|
Chris@0
|
131 if ($parsed['operator'] === '+' ||
|
Chris@0
|
132 $parsed['operator'] === '#'
|
Chris@0
|
133 ) {
|
Chris@0
|
134 $var = $this->decodeReserved($var);
|
Chris@0
|
135 }
|
Chris@0
|
136 }
|
Chris@0
|
137
|
Chris@0
|
138 if ($value['modifier'] === '*') {
|
Chris@0
|
139 if ($isAssoc) {
|
Chris@0
|
140 if ($isNestedArray) {
|
Chris@0
|
141 // Nested arrays must allow for deeply nested
|
Chris@0
|
142 // structures.
|
Chris@0
|
143 $var = strtr(
|
Chris@0
|
144 http_build_query([$key => $var]),
|
Chris@0
|
145 $rfc1738to3986
|
Chris@0
|
146 );
|
Chris@0
|
147 } else {
|
Chris@0
|
148 $var = $key . '=' . $var;
|
Chris@0
|
149 }
|
Chris@0
|
150 } elseif ($key > 0 && $actuallyUseQuery) {
|
Chris@0
|
151 $var = $value['value'] . '=' . $var;
|
Chris@0
|
152 }
|
Chris@0
|
153 }
|
Chris@0
|
154
|
Chris@0
|
155 $kvp[$key] = $var;
|
Chris@0
|
156 }
|
Chris@0
|
157
|
Chris@0
|
158 if (empty($variable)) {
|
Chris@0
|
159 $actuallyUseQuery = false;
|
Chris@0
|
160 } elseif ($value['modifier'] === '*') {
|
Chris@0
|
161 $expanded = implode($joiner, $kvp);
|
Chris@0
|
162 if ($isAssoc) {
|
Chris@0
|
163 // Don't prepend the value name when using the explode
|
Chris@0
|
164 // modifier with an associative array.
|
Chris@0
|
165 $actuallyUseQuery = false;
|
Chris@0
|
166 }
|
Chris@0
|
167 } else {
|
Chris@0
|
168 if ($isAssoc) {
|
Chris@0
|
169 // When an associative array is encountered and the
|
Chris@0
|
170 // explode modifier is not set, then the result must be
|
Chris@0
|
171 // a comma separated list of keys followed by their
|
Chris@0
|
172 // respective values.
|
Chris@0
|
173 foreach ($kvp as $k => &$v) {
|
Chris@0
|
174 $v = $k . ',' . $v;
|
Chris@0
|
175 }
|
Chris@0
|
176 }
|
Chris@0
|
177 $expanded = implode(',', $kvp);
|
Chris@0
|
178 }
|
Chris@0
|
179 } else {
|
Chris@0
|
180 if ($value['modifier'] === ':') {
|
Chris@0
|
181 $variable = substr($variable, 0, $value['position']);
|
Chris@0
|
182 }
|
Chris@0
|
183 $expanded = rawurlencode($variable);
|
Chris@0
|
184 if ($parsed['operator'] === '+' || $parsed['operator'] === '#') {
|
Chris@0
|
185 $expanded = $this->decodeReserved($expanded);
|
Chris@0
|
186 }
|
Chris@0
|
187 }
|
Chris@0
|
188
|
Chris@0
|
189 if ($actuallyUseQuery) {
|
Chris@0
|
190 if (!$expanded && $joiner !== '&') {
|
Chris@0
|
191 $expanded = $value['value'];
|
Chris@0
|
192 } else {
|
Chris@0
|
193 $expanded = $value['value'] . '=' . $expanded;
|
Chris@0
|
194 }
|
Chris@0
|
195 }
|
Chris@0
|
196
|
Chris@0
|
197 $replacements[] = $expanded;
|
Chris@0
|
198 }
|
Chris@0
|
199
|
Chris@0
|
200 $ret = implode($joiner, $replacements);
|
Chris@0
|
201 if ($ret && $prefix) {
|
Chris@0
|
202 return $prefix . $ret;
|
Chris@0
|
203 }
|
Chris@0
|
204
|
Chris@0
|
205 return $ret;
|
Chris@0
|
206 }
|
Chris@0
|
207
|
Chris@0
|
208 /**
|
Chris@0
|
209 * Determines if an array is associative.
|
Chris@0
|
210 *
|
Chris@0
|
211 * This makes the assumption that input arrays are sequences or hashes.
|
Chris@0
|
212 * This assumption is a tradeoff for accuracy in favor of speed, but it
|
Chris@0
|
213 * should work in almost every case where input is supplied for a URI
|
Chris@0
|
214 * template.
|
Chris@0
|
215 *
|
Chris@0
|
216 * @param array $array Array to check
|
Chris@0
|
217 *
|
Chris@0
|
218 * @return bool
|
Chris@0
|
219 */
|
Chris@0
|
220 private function isAssoc(array $array)
|
Chris@0
|
221 {
|
Chris@0
|
222 return $array && array_keys($array)[0] !== 0;
|
Chris@0
|
223 }
|
Chris@0
|
224
|
Chris@0
|
225 /**
|
Chris@0
|
226 * Removes percent encoding on reserved characters (used with + and #
|
Chris@0
|
227 * modifiers).
|
Chris@0
|
228 *
|
Chris@0
|
229 * @param string $string String to fix
|
Chris@0
|
230 *
|
Chris@0
|
231 * @return string
|
Chris@0
|
232 */
|
Chris@0
|
233 private function decodeReserved($string)
|
Chris@0
|
234 {
|
Chris@0
|
235 return str_replace(self::$delimsPct, self::$delims, $string);
|
Chris@0
|
236 }
|
Chris@0
|
237 }
|