@@ -65,22 +65,48 @@ public function __debugInfo()
65
65
*
66
66
* @param array $options Any modifiers needed for the query These include:
67
67
* - 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
69
71
*/
70
72
public function getAllDocs ($ options = []) : array
71
73
{
72
74
$ 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
+
74
88
$ response = $ this ->client ->request ("GET " , $ endpoint , ["query " => $ query ]);
75
89
if ($ response ->getStatusCode () == 200 ) {
76
90
// try to decode JSON
77
91
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 ;
82
109
}
83
- return $ docs ;
84
110
} else {
85
111
throw new Exception \ServerException ('JSON response not received or not understood ' );
86
112
}
0 commit comments