Skip to content

Commit 8bc03f0

Browse files
committed
Share the logic for turning booleans to strings
1 parent e6c9799 commit 8bc03f0

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

src/PHPCouchDB/Database.php

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,9 @@ public function getAllDocs($options = []) : array
7676
// grab extra params
7777
$query = $options;
7878

79-
// set some defaults
80-
if (isset($query['include_docs']) && $query['include_docs'] == false) {
81-
// needs to be a string
82-
$query['include_docs'] = "false";
79+
// convert data and set some defaults
80+
if (isset($query['include_docs'])) {
81+
$query['include_docs'] = $this->boolToString($query['include_docs']);
8382
} else {
8483
// needs to be a string and this is our chosen default value
8584
$query['include_docs'] = "true";
@@ -191,15 +190,21 @@ public function getView($options = []) : array
191190
}
192191
}
193192

194-
// set some defaults
195-
if (isset($query['include_docs']) && $query['include_docs'] == true) {
196-
// needs to be a string
197-
$query['include_docs'] = "true";
193+
// convert data and set some defaults
194+
if (isset($query['include_docs'])) {
195+
$query['include_docs'] = $this->boolToString($query['include_docs']);
198196
} else {
199197
// needs to be a string and this is our chosen default value
200198
$query['include_docs'] = "false";
201199
}
202200

201+
if (isset($query['reduce'])) {
202+
$query['reduce'] = $this->boolToString($query['reduce']);
203+
} else {
204+
// needs to be a string and this is our chosen default value
205+
$query['reduce'] = "true";
206+
}
207+
203208
$response = $this->client->request("GET", $endpoint, ["query" => $query]);
204209
$data = $this->handleServerResponse($response);
205210
return $data;
@@ -236,4 +241,19 @@ protected function handleServerResponse($response) : array
236241
}
237242
}
238243
}
244+
245+
/**
246+
* Convert truthy things to "true" and the rest to "false" because
247+
* Guzzle doesn't send booleans as words
248+
*
249+
* @param mixed $value The value to use
250+
* @return A string either "true" or "false"
251+
*/
252+
protected function boolToString($value) {
253+
if($value) {
254+
return "true";
255+
} else {
256+
return "false";
257+
}
258+
}
239259
}

0 commit comments

Comments
 (0)