@@ -76,41 +76,17 @@ public function getAllDocs($options = []) : array
76
76
// grab extra params
77
77
$ query = $ options ;
78
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 " ;
79
+ // convert data and set some defaults
80
+ if (isset ($ query ['include_docs ' ])) {
81
+ $ query ['include_docs ' ] = $ this ->boolToString ($ query ['include_docs ' ]);
83
82
} else {
84
83
// needs to be a string and this is our chosen default value
85
84
$ query ['include_docs ' ] = "true " ;
86
85
}
87
86
88
87
$ 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
- }
88
+ $ data = $ this ->handleServerResponse ($ response );
89
+ return $ data ;
114
90
}
115
91
116
92
/**
@@ -187,4 +163,97 @@ public function getDocById($id) : Document
187
163
}
188
164
}
189
165
}
166
+
167
+ public function getView ($ options = []) : array
168
+ {
169
+ // check we have ddoc and view name
170
+ if (!isset ($ options ['ddoc ' ])) {
171
+ throw new Exception \ServerException (
172
+ 'ddoc is a required parameter for getView '
173
+ );
174
+ }
175
+ if (!isset ($ options ['view ' ])) {
176
+ throw new Exception \ServerException (
177
+ 'view is a required parameter for getView '
178
+ );
179
+ }
180
+
181
+ $ endpoint = "/ " . $ this ->db_name . "/_design/ " . $ options ['ddoc ' ]
182
+ . "/_view/ " . $ options ['view ' ];
183
+
184
+ // grab extra params
185
+ $ query = [];
186
+ foreach ($ options as $ key => $ value ) {
187
+ // skip the values we need for the URL, pass the rest through
188
+ if (!in_array ($ key , ["ddoc " , "view " ])) {
189
+ $ query [$ key ] = $ value ;
190
+ }
191
+ }
192
+
193
+ // convert data and set some defaults
194
+ if (isset ($ query ['include_docs ' ])) {
195
+ $ query ['include_docs ' ] = $ this ->boolToString ($ query ['include_docs ' ]);
196
+ } else {
197
+ // needs to be a string and this is our chosen default value
198
+ $ query ['include_docs ' ] = "false " ;
199
+ }
200
+
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
+
208
+ $ response = $ this ->client ->request ("GET " , $ endpoint , ["query " => $ query ]);
209
+ $ data = $ this ->handleServerResponse ($ response );
210
+ return $ data ;
211
+ }
212
+
213
+ protected function handleServerResponse ($ response ) : array
214
+ {
215
+ if ($ response ->getStatusCode () == 200 ) {
216
+ // try to decode JSON
217
+ if ($ json_data = json_decode ($ response ->getBody (), true )) {
218
+ if (isset ($ json_data ['rows ' ][0 ]['doc ' ])) {
219
+ // we have some data - extract the docs to return
220
+ $ docs = [];
221
+ foreach ($ json_data ['rows ' ] as $ document ) {
222
+ $ docs [] = new Document ($ this , $ document ["doc " ]);
223
+ }
224
+ return $ docs ;
225
+ } elseif (isset ($ json_data ['rows ' ][0 ]['value ' ]['rev ' ])) {
226
+ // assume these are doc signposts
227
+ $ docs = [];
228
+ foreach ($ json_data ['rows ' ] as $ item ) {
229
+ $ doc = [];
230
+ $ doc ['id ' ] = $ item ['id ' ];
231
+ $ doc ['rev ' ] = $ item ['value ' ]['rev ' ];
232
+ $ docs [] = $ doc ;
233
+ }
234
+ return $ docs ;
235
+ } else {
236
+ // no docs, just return some basic info
237
+ return $ json_data ["rows " ];
238
+ }
239
+ } else {
240
+ throw new Exception \ServerException ('JSON response not received or not understood ' );
241
+ }
242
+ }
243
+ }
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
+ }
190
259
}
0 commit comments