|
10 | 10 | namespace chillerlan\HTTP\Psr7; |
11 | 11 |
|
12 | 12 | use InvalidArgumentException; |
| 13 | +use Psr\Http\Message\ResponseInterface; |
13 | 14 | use Psr\Http\Message\UploadedFileInterface; |
14 | 15 |
|
15 | 16 | const PSR7_INCLUDES = true; |
@@ -124,23 +125,46 @@ function build_http_query(array $params, bool $urlencode = null, string $delimit |
124 | 125 | return implode($delimiter ?? '&', $pairs); |
125 | 126 | } |
126 | 127 |
|
| 128 | +const BOOLEANS_AS_BOOL = 0; |
| 129 | +const BOOLEANS_AS_INT = 1; |
| 130 | +const BOOLEANS_AS_STRING = 2; |
| 131 | +const BOOLEANS_AS_INT_STRING = 3; |
127 | 132 | /** |
128 | 133 | * @param iterable $params |
129 | | - * @param bool|null $booleans_as_string - converts booleans to "true"/"false" strings if set to true, "0"/"1" otherwise. |
| 134 | + * @param int|null $bool_cast converts booleans to a type determined like following: |
| 135 | + * BOOLEANS_AS_BOOL : unchanged boolean value (default) |
| 136 | + * BOOLEANS_AS_INT : integer values 0 or 1 |
| 137 | + * BOOLEANS_AS_STRING : "true"/"false" strings |
| 138 | + * BOOLEANS_AS_INT_STRING: "0"/"1" |
| 139 | + * |
| 140 | + * @param bool|null $remove_empty remove empty and NULL values |
130 | 141 | * |
131 | 142 | * @return array |
132 | 143 | */ |
133 | | -function clean_query_params(iterable $params, bool $booleans_as_string = null):array{ |
| 144 | +function clean_query_params(iterable $params, int $bool_cast = null, bool $remove_empty = null):array{ |
134 | 145 | $p = []; |
| 146 | + $bool_cast = $bool_cast ?? BOOLEANS_AS_BOOL; |
| 147 | + $remove_empty = $remove_empty ?? true; |
135 | 148 |
|
136 | 149 | foreach($params as $key => $value){ |
137 | 150 |
|
138 | 151 | if(is_bool($value)){ |
139 | | - $p[$key] = $booleans_as_string === true |
140 | | - ? ($value ? 'true' : 'false') |
141 | | - : (string)(int)$value; |
| 152 | + |
| 153 | + if($bool_cast === BOOLEANS_AS_BOOL){ |
| 154 | + $p[$key] = $value; |
| 155 | + } |
| 156 | + elseif($bool_cast === BOOLEANS_AS_INT){ |
| 157 | + $p[$key] = (int)$value; |
| 158 | + } |
| 159 | + elseif($bool_cast === BOOLEANS_AS_STRING){ |
| 160 | + $p[$key] = $value ? 'true' : 'false'; |
| 161 | + } |
| 162 | + elseif($bool_cast === BOOLEANS_AS_INT_STRING){ |
| 163 | + $p[$key] = (string)(int)$value; |
| 164 | + } |
| 165 | + |
142 | 166 | } |
143 | | - elseif($value === null || (!is_numeric($value) && empty($value))){ |
| 167 | + elseif($remove_empty === true && ($value === null || (!is_numeric($value) && empty($value)))){ |
144 | 168 | continue; |
145 | 169 | } |
146 | 170 | else{ |
@@ -250,3 +274,26 @@ function normalize_nested_file_spec(array $files = []):array{ |
250 | 274 |
|
251 | 275 | return $normalizedFiles; |
252 | 276 | } |
| 277 | + |
| 278 | +/** |
| 279 | + * @todo |
| 280 | + * |
| 281 | + * @param \Psr\Http\Message\ResponseInterface $response |
| 282 | + * @param bool|null $assoc |
| 283 | + * |
| 284 | + * @return mixed |
| 285 | + */ |
| 286 | +function get_json(ResponseInterface $response, bool $assoc = null){ |
| 287 | + return json_decode($response->getBody()->getContents(), $assoc); |
| 288 | +} |
| 289 | + |
| 290 | +/** |
| 291 | + * @todo |
| 292 | + * |
| 293 | + * @param \Psr\Http\Message\ResponseInterface $response |
| 294 | + * |
| 295 | + * @return \SimpleXMLElement |
| 296 | + */ |
| 297 | +function get_xml(ResponseInterface $response){ |
| 298 | + return simplexml_load_string($response->getBody()->getContents()); |
| 299 | +} |
0 commit comments