1 | <?php |
||
27 | class Query implements QueryInterface |
||
28 | { |
||
29 | use ImmutableComponentTrait; |
||
30 | |||
31 | use ImmutableCollectionTrait; |
||
32 | |||
33 | /** |
||
34 | * Key/pair separator character |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected static $separator = '&'; |
||
39 | |||
40 | /** |
||
41 | * Preserve the delimiter |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $preserveDelimiter = false; |
||
46 | |||
47 | /** |
||
48 | * a new instance |
||
49 | * |
||
50 | * @param string $data |
||
51 | */ |
||
52 | 767 | public function __construct($data = null) |
|
59 | |||
60 | /** |
||
61 | * sanitize the submitted data |
||
62 | * |
||
63 | * @param string $str |
||
64 | * |
||
65 | * @throws InvalidArgumentException If reserved characters are used |
||
66 | * |
||
67 | * @return array |
||
68 | */ |
||
69 | 611 | protected function validate($str) |
|
78 | |||
79 | /** |
||
80 | * return a new Query instance from an Array or a traversable object |
||
81 | * |
||
82 | * @param \Traversable|array $data |
||
83 | * |
||
84 | * @return static |
||
85 | */ |
||
86 | 102 | public static function createFromArray($data) |
|
96 | |||
97 | /** |
||
98 | * @inheritdoc |
||
99 | */ |
||
100 | 1 | public function __debugInfo() |
|
104 | |||
105 | /** |
||
106 | * @inheritdoc |
||
107 | */ |
||
108 | 12 | public static function __set_state(array $properties) |
|
115 | |||
116 | /** |
||
117 | * Returns the component literal value. The return type can be |
||
118 | * <ul> |
||
119 | * <li> null: If the component is not defined |
||
120 | * <li> string: Otherwise |
||
121 | * </ul> |
||
122 | * |
||
123 | * @return string|int|null |
||
124 | */ |
||
125 | 674 | public function getContent() |
|
126 | { |
||
127 | 674 | if ([] === $this->data && false === $this->preserveDelimiter) { |
|
128 | 345 | return null; |
|
129 | } |
||
130 | |||
131 | 521 | return (new QueryParser())->build($this->data, static::$separator, PHP_QUERY_RFC3986); |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * Returns the instance string representation; If the |
||
136 | * instance is not defined an empty string is returned |
||
137 | * |
||
138 | * @return string |
||
139 | */ |
||
140 | 674 | public function __toString() |
|
144 | |||
145 | /** |
||
146 | * Returns the instance string representation |
||
147 | * with its optional URI delimiters |
||
148 | * |
||
149 | * @return string |
||
150 | */ |
||
151 | 625 | public function getUriComponent() |
|
160 | |||
161 | /** |
||
162 | * Returns an instance with the specified string |
||
163 | * |
||
164 | * This method MUST retain the state of the current instance, and return |
||
165 | * an instance that contains the modified data |
||
166 | * |
||
167 | * @param string $value |
||
168 | * |
||
169 | * @return static |
||
170 | */ |
||
171 | 162 | public function modify($value) |
|
183 | |||
184 | /** |
||
185 | * Retrieves a single query parameter. |
||
186 | * |
||
187 | * Retrieves a single query parameter. If the parameter has not been set, |
||
188 | * returns the default value provided. |
||
189 | * |
||
190 | * @param string $offset the parameter name |
||
191 | * @param mixed $default Default value to return if the parameter does not exist. |
||
192 | * |
||
193 | * @return mixed |
||
194 | */ |
||
195 | 9 | public function getValue($offset, $default = null) |
|
204 | |||
205 | /** |
||
206 | * Returns an instance merge with the specified query |
||
207 | * |
||
208 | * This method MUST retain the state of the current instance, and return |
||
209 | * an instance that contains the modified query |
||
210 | * |
||
211 | * @param Query|string $query the data to be merged query can be |
||
212 | * - another Interfaces\Query object |
||
213 | * - a string or a Stringable object |
||
214 | * |
||
215 | * @return static |
||
216 | */ |
||
217 | 24 | public function merge($query) |
|
229 | |||
230 | /** |
||
231 | * Sort the query string by offset, maintaining offset to data correlations. |
||
232 | * |
||
233 | * This method MUST retain the state of the current instance, and return |
||
234 | * an instance that contains the modified query |
||
235 | * |
||
236 | * @param callable|int $sort a PHP sort flag constant or a comparaison function |
||
237 | * which must return an integer less than, equal to, |
||
238 | * or greater than zero if the first argument is |
||
239 | * considered to be respectively less than, equal to, |
||
240 | * or greater than the second. |
||
241 | * |
||
242 | * @return static |
||
243 | */ |
||
244 | 45 | public function ksort($sort = SORT_REGULAR) |
|
255 | |||
256 | /** |
||
257 | * Return a new instance when needed |
||
258 | * |
||
259 | * @param array $data |
||
260 | * |
||
261 | * @return static |
||
262 | */ |
||
263 | 42 | protected function newCollectionInstance(array $data) |
|
271 | } |
||
272 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.