Skip to content

Commit 2a581ff

Browse files
committed
:octocat:
1 parent 0420f09 commit 2a581ff

File tree

2 files changed

+61
-8
lines changed

2 files changed

+61
-8
lines changed

src/Psr7/message_helpers.php

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace chillerlan\HTTP\Psr7;
1111

1212
use InvalidArgumentException;
13+
use Psr\Http\Message\ResponseInterface;
1314
use Psr\Http\Message\UploadedFileInterface;
1415

1516
const PSR7_INCLUDES = true;
@@ -124,23 +125,46 @@ function build_http_query(array $params, bool $urlencode = null, string $delimit
124125
return implode($delimiter ?? '&', $pairs);
125126
}
126127

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;
127132
/**
128133
* @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
130141
*
131142
* @return array
132143
*/
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{
134145
$p = [];
146+
$bool_cast = $bool_cast ?? BOOLEANS_AS_BOOL;
147+
$remove_empty = $remove_empty ?? true;
135148

136149
foreach($params as $key => $value){
137150

138151
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+
142166
}
143-
elseif($value === null || (!is_numeric($value) && empty($value))){
167+
elseif($remove_empty === true && ($value === null || (!is_numeric($value) && empty($value)))){
144168
continue;
145169
}
146170
else{
@@ -250,3 +274,26 @@ function normalize_nested_file_spec(array $files = []):array{
250274

251275
return $normalizedFiles;
252276
}
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+
}

tests/Psr7/MessageHelpersTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,14 @@ public function testNormalizeHeaders(array $header, array $normalized){
4343
public function testCheckParams(){
4444
$data = ['foo' => 'bar', 'whatever' => null, 'nope' => '', 'true' => true, 'false' => false];
4545

46-
$this->assertSame(['foo' => 'bar', 'true' => '1', 'false' => '0'], Psr7\clean_query_params($data));
47-
$this->assertSame(['foo' => 'bar', 'true' => 'true', 'false' => 'false'], Psr7\clean_query_params($data, true));
46+
// don't remove empty values
47+
$this->assertSame(['foo' => 'bar', 'whatever' => null, 'nope' => '', 'true' => true, 'false' => false], Psr7\clean_query_params($data, Psr7\BOOLEANS_AS_BOOL, false));
48+
49+
// bool cast to types
50+
$this->assertSame(['foo' => 'bar', 'true' => true, 'false' => false], Psr7\clean_query_params($data, Psr7\BOOLEANS_AS_BOOL));
51+
$this->assertSame(['foo' => 'bar', 'true' => 1, 'false' => 0], Psr7\clean_query_params($data, Psr7\BOOLEANS_AS_INT));
52+
$this->assertSame(['foo' => 'bar', 'true' => '1', 'false' => '0'], Psr7\clean_query_params($data, Psr7\BOOLEANS_AS_INT_STRING));
53+
$this->assertSame(['foo' => 'bar', 'true' => 'true', 'false' => 'false'], Psr7\clean_query_params($data, Psr7\BOOLEANS_AS_STRING));
4854
}
4955

5056

0 commit comments

Comments
 (0)