1 | <?php |
||
21 | class QueryParser |
||
22 | { |
||
23 | /** |
||
24 | * Parse a query string into an associative array |
||
25 | * |
||
26 | * Multiple identical key will generate an array. This function |
||
27 | * differ from PHP parse_str as: |
||
28 | * - it does not modify or remove parameters keys |
||
29 | * - it does not create nested array |
||
30 | * |
||
31 | * @param string $str The query string to parse |
||
32 | * @param string $separator The query string separator |
||
33 | * @param int|false $encodingType The query string encoding mechanism |
||
34 | * |
||
35 | * @return array |
||
36 | */ |
||
37 | 422 | public function parse($str, $separator = '&', $encodingType = PHP_QUERY_RFC3986) |
|
51 | |||
52 | /** |
||
53 | * validate the encoding type for the query related methods |
||
54 | * |
||
55 | * @param int|false $encodingType |
||
56 | * |
||
57 | * @return int|false |
||
58 | */ |
||
59 | 530 | protected function validateEncodingType($encodingType) |
|
67 | |||
68 | /** |
||
69 | * Parse a query string pair |
||
70 | * |
||
71 | * @param array $res The associative array to add the pair to |
||
72 | * @param callable $decoder a Callable to decode the query string pair |
||
73 | * @param string $pair The query string pair |
||
74 | * |
||
75 | * @return array |
||
76 | */ |
||
77 | 402 | protected function parsePair(array $res, callable $decoder, $pair) |
|
98 | |||
99 | /** |
||
100 | * Build a query string from an associative array |
||
101 | * |
||
102 | * The method expects the return value from Query::parse to build |
||
103 | * a valid query string. This method differs from PHP http_build_query as: |
||
104 | * |
||
105 | * - it does not modify parameters keys |
||
106 | * |
||
107 | * @param array $arr Query string parameters |
||
108 | * @param string $separator Query string separator |
||
109 | * @param int|false $encodingType Query string encoding |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | 460 | public function build(array $arr, $separator = '&', $encodingType = PHP_QUERY_RFC3986) |
|
128 | |||
129 | /** |
||
130 | * Build a query key/pair association |
||
131 | * |
||
132 | * @param callable $encoder a callable to encode the key/pair association |
||
133 | * @param array $value The query string value |
||
134 | * @param string $key The query string key |
||
135 | * |
||
136 | * @return string |
||
137 | */ |
||
138 | 348 | protected function buildPair(callable $encoder, array $value, $key) |
|
152 | |||
153 | /** |
||
154 | * Return the query string decoding mechanism |
||
155 | * |
||
156 | * @param int|false $encodingType |
||
157 | * |
||
158 | 402 | * @return callable |
|
159 | */ |
||
160 | 402 | protected function getDecoder($encodingType) |
|
178 | |||
179 | /** |
||
180 | * Return the query string encoding mechanism |
||
181 | * |
||
182 | * @param int|false $encodingType |
||
183 | * |
||
184 | 460 | * @return callable |
|
185 | */ |
||
186 | 460 | protected function getEncoder($encodingType) |
|
204 | } |
||
205 |