Skip to content

Commit f4dc734

Browse files
committed
Support params to alldocs, and returning an array if include_docs=false
1 parent 0a8445a commit f4dc734

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

src/PHPCouchDB/Database.php

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,48 @@ public function __debugInfo()
6565
*
6666
* @param array $options Any modifiers needed for the query These include:
6767
* - include_docs Defaults to true
68-
* @return array The array contains `PHPCouchDB\Document` objects
68+
* @return array The array contains `PHPCouchDB\Document` objects if the
69+
* include_docs parameter was set; otherwise an array of arrays with
70+
* "id" and "rev" keys
6971
*/
7072
public function getAllDocs($options = []) : array
7173
{
7274
$endpoint = "/" . $this->db_name . "/_all_docs";
73-
$query = ["include_docs" => "true"];
75+
76+
// grab extra params
77+
$query = $options;
78+
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";
83+
} else {
84+
// needs to be a string and this is our chosen default value
85+
$query['include_docs'] = "true";
86+
}
87+
7488
$response = $this->client->request("GET", $endpoint, ["query" => $query]);
7589
if ($response->getStatusCode() == 200) {
7690
// try to decode JSON
7791
if ($json_data = json_decode($response->getBody(), true)) {
78-
// we have some data - extract the docs to return
79-
$docs = [];
80-
foreach ($json_data["rows"] as $document) {
81-
$docs[] = new Document($this, $document["doc"]);
92+
if (isset($json_data['rows'][0]['doc'])) {
93+
// we have some data - extract the docs to return
94+
$docs = [];
95+
foreach ($json_data["rows"] as $document) {
96+
$docs[] = new Document($this, $document["doc"]);
97+
}
98+
return $docs;
99+
} else {
100+
// no docs, just return some basic info
101+
$results = [];
102+
foreach ($json_data['rows'] as $document) {
103+
$row = [];
104+
$row['id'] = $document['id'];
105+
$row['rev'] = $document['value']['rev'];
106+
$results[] = $row;
107+
}
108+
return $results;
82109
}
83-
return $docs;
84110
} else {
85111
throw new Exception\ServerException('JSON response not received or not understood');
86112
}

0 commit comments

Comments
 (0)