Skip to content

Commit 17d8f1e

Browse files
committed
Add a shared handler for alldocs and view fetching
1 parent 8b1b5e8 commit 17d8f1e

File tree

1 file changed

+64
-25
lines changed

1 file changed

+64
-25
lines changed

src/PHPCouchDB/Database.php

Lines changed: 64 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -86,31 +86,8 @@ public function getAllDocs($options = []) : array
8686
}
8787

8888
$response = $this->client->request("GET", $endpoint, ["query" => $query]);
89-
if ($response->getStatusCode() == 200) {
90-
// try to decode JSON
91-
if ($json_data = json_decode($response->getBody(), true)) {
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;
109-
}
110-
} else {
111-
throw new Exception\ServerException('JSON response not received or not understood');
112-
}
113-
}
89+
$data = $this->handleServerResponse($response);
90+
return $data;
11491
}
11592

11693
/**
@@ -187,4 +164,66 @@ public function getDocById($id) : Document
187164
}
188165
}
189166
}
167+
168+
public function getView($options = []) : array
169+
{
170+
// check we have ddoc and view name
171+
if(!isset($options['ddoc'])) {
172+
throw new Exception\ServerException(
173+
'ddoc is a required parameter for getView'
174+
);
175+
}
176+
if(!isset($options['view'])) {
177+
throw new Exception\ServerException(
178+
'view is a required parameter for getView'
179+
);
180+
}
181+
182+
$endpoint = "/" . $this->db_name . "/_design/" . $options['ddoc']
183+
. "/_view/" . $options['view'];
184+
185+
// grab extra params
186+
$query = [];
187+
foreach($options as $key => $value) {
188+
// skip the values we need for the URL, pass the rest through
189+
if(!in_array($key, ["ddoc", "view"])) {
190+
$query[$key] = $value;
191+
}
192+
}
193+
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";
198+
} else {
199+
// needs to be a string and this is our chosen default value
200+
$query['include_docs'] = "false";
201+
}
202+
203+
$response = $this->client->request("GET", $endpoint, ["query" => $query]);
204+
$data = $this->handleServerResponse($response);
205+
return $data;
206+
}
207+
208+
protected function handleServerResponse($response) : array {
209+
if ($response->getStatusCode() == 200) {
210+
// try to decode JSON
211+
if ($json_data = json_decode($response->getBody(), true)) {
212+
if (isset($json_data['rows'][0]['doc'])) {
213+
// we have some data - extract the docs to return
214+
$docs = [];
215+
foreach ($json_data["rows"] as $document) {
216+
$docs[] = new Document($this, $document["doc"]);
217+
}
218+
return $docs;
219+
} else {
220+
// no docs, just return some basic info
221+
return $json_data["rows"];
222+
}
223+
} else {
224+
throw new Exception\ServerException('JSON response not received or not understood');
225+
}
226+
}
227+
}
228+
190229
}

0 commit comments

Comments
 (0)