@@ -107,10 +107,13 @@ discard block |
||
107 | 107 | foreach ($keys as $key) { |
108 | 108 | $hash = md5(json_encode($key)); |
109 | 109 | |
110 | - if (isset($matches[$hash])) // Match found. |
|
110 | + if (isset($matches[$hash])) { |
|
111 | + // Match found. |
|
111 | 112 | $allRows[] = $matches[$hash]; |
112 | - else // No match found. |
|
113 | + } else { |
|
114 | + // No match found. |
|
113 | 115 | $allRows[] = ['id' => NULL, 'key' => $key, 'value' => NULL]; |
116 | + } |
|
114 | 117 | } |
115 | 118 | |
116 | 119 | // Overrides the response, replacing rows. |
@@ -173,15 +176,18 @@ discard block |
||
173 | 176 | * @param bool $excludeLocal Document path. |
174 | 177 | */ |
175 | 178 | public function validateDocPath($path, $excludeLocal = FALSE) { |
176 | - if (empty($path)) // STD_DOC_PATH |
|
179 | + if (empty($path)) { |
|
180 | + // STD_DOC_PATH |
|
177 | 181 | return; |
182 | + } |
|
178 | 183 | |
179 | - if ($path == self::DESIGN_DOC_PATH) |
|
180 | - return; |
|
181 | - elseif ($path == self::LOCAL_DOC_PATH && $excludeLocal) |
|
182 | - throw new \InvalidArgumentException("Local document doesn't have attachments."); |
|
183 | - else |
|
184 | - throw new \InvalidArgumentException("Invalid document path."); |
|
184 | + if ($path == self::DESIGN_DOC_PATH) { |
|
185 | + return; |
|
186 | + } elseif ($path == self::LOCAL_DOC_PATH && $excludeLocal) { |
|
187 | + throw new \InvalidArgumentException("Local document doesn't have attachments."); |
|
188 | + } else { |
|
189 | + throw new \InvalidArgumentException("Invalid document path."); |
|
190 | + } |
|
185 | 191 | } |
186 | 192 | |
187 | 193 | |
@@ -192,10 +198,11 @@ discard block |
||
192 | 198 | * @param string $docId Document id. |
193 | 199 | */ |
194 | 200 | public function validateAndEncodeDocId(&$docId) { |
195 | - if (!empty($docId)) |
|
196 | - $docId = rawurlencode($docId); |
|
197 | - else |
|
198 | - throw new \InvalidArgumentException("\$docId must be a non-empty string."); |
|
201 | + if (!empty($docId)) { |
|
202 | + $docId = rawurlencode($docId); |
|
203 | + } else { |
|
204 | + throw new \InvalidArgumentException("\$docId must be a non-empty string."); |
|
205 | + } |
|
199 | 206 | } |
200 | 207 | |
201 | 208 | //!@} |
@@ -227,10 +234,10 @@ discard block |
||
227 | 234 | // before the client has received the entire response. To avoid problems, we trap the exception and we go on. |
228 | 235 | try { |
229 | 236 | $this->send($request); |
230 | - } |
|
231 | - catch (\Exception $e) { |
|
232 | - if ($e->getCode() > 0) |
|
233 | - throw $e; |
|
237 | + } catch (\Exception $e) { |
|
238 | + if ($e->getCode() > 0) { |
|
239 | + throw $e; |
|
240 | + } |
|
234 | 241 | } |
235 | 242 | } |
236 | 243 | |
@@ -267,10 +274,11 @@ discard block |
||
267 | 274 | public function getFavicon() { |
268 | 275 | $response = $this->send(new Request(Request::GET_METHOD, "/favicon.ico")); |
269 | 276 | |
270 | - if ($response->getHeaderFieldValue(Request::CONTENT_TYPE_HF) == "image/x-icon") |
|
271 | - return $response->getBody(); |
|
272 | - else |
|
273 | - throw new \InvalidArgumentException("Content-Type must be image/x-icon."); |
|
277 | + if ($response->getHeaderFieldValue(Request::CONTENT_TYPE_HF) == "image/x-icon") { |
|
278 | + return $response->getBody(); |
|
279 | + } else { |
|
280 | + throw new \InvalidArgumentException("Content-Type must be image/x-icon."); |
|
281 | + } |
|
274 | 282 | } |
275 | 283 | |
276 | 284 | |
@@ -304,8 +312,9 @@ discard block |
||
304 | 312 | public function getDbUpdates(Opt\DbUpdatesFeedOpts $opts = NULL) { |
305 | 313 | $request = new Request(Request::GET_METHOD, "/_db_updates"); |
306 | 314 | |
307 | - if (isset($opts)) |
|
308 | - $request->setMultipleQueryParamsAtOnce($opts->asArray()); |
|
315 | + if (isset($opts)) { |
|
316 | + $request->setMultipleQueryParamsAtOnce($opts->asArray()); |
|
317 | + } |
|
309 | 318 | |
310 | 319 | return $this->send($request)->getBodyAsArray(); |
311 | 320 | } |
@@ -334,9 +343,9 @@ discard block |
||
334 | 343 | $request = new Request(Request::GET_METHOD, "/_log"); |
335 | 344 | $request->setQueryParam("bytes", $bytes); |
336 | 345 | return $this->send($request)->getBody(); |
346 | + } else { |
|
347 | + throw new \InvalidArgumentException("\$bytes must be a positive integer."); |
|
337 | 348 | } |
338 | - else |
|
339 | - throw new \InvalidArgumentException("\$bytes must be a positive integer."); |
|
340 | 349 | } |
341 | 350 | |
342 | 351 | |
@@ -353,13 +362,15 @@ discard block |
||
353 | 362 | |
354 | 363 | $response = $this->send($request); |
355 | 364 | |
356 | - if ($count == 1) // We don't need to use === operator because, just above, we made a type checking. |
|
365 | + if ($count == 1) { |
|
366 | + // We don't need to use === operator because, just above, we made a type checking. |
|
357 | 367 | return $response->getBodyAsArray()['uuids'][0]; |
358 | - else |
|
359 | - return $response->getBodyAsArray()['uuids']; |
|
368 | + } else { |
|
369 | + return $response->getBodyAsArray()['uuids']; |
|
370 | + } |
|
371 | + } else { |
|
372 | + throw new \InvalidArgumentException("\$count must be a positive integer."); |
|
360 | 373 | } |
361 | - else |
|
362 | - throw new \InvalidArgumentException("\$count must be a positive integer."); |
|
363 | 374 | } |
364 | 375 | |
365 | 376 | //!@} |
@@ -383,8 +394,9 @@ discard block |
||
383 | 394 | if (!empty($section)) { |
384 | 395 | $path .= "/".$section; |
385 | 396 | |
386 | - if (!empty($key)) |
|
387 | - $path .= "/".$key; |
|
397 | + if (!empty($key)) { |
|
398 | + $path .= "/".$key; |
|
399 | + } |
|
388 | 400 | } |
389 | 401 | |
390 | 402 | return $this->send(new Request(Request::GET_METHOD, $path))->getBodyAsArray(); |
@@ -399,14 +411,17 @@ discard block |
||
399 | 411 | * @see http://docs.couchdb.org/en/latest/api/server/configuration.html#put--_config-section-key |
400 | 412 | */ |
401 | 413 | public function setConfigKey($section, $key, $value) { |
402 | - if (!is_string($section) or empty($section)) |
|
403 | - throw new \InvalidArgumentException("\$section must be a not empty string."); |
|
414 | + if (!is_string($section) or empty($section)) { |
|
415 | + throw new \InvalidArgumentException("\$section must be a not empty string."); |
|
416 | + } |
|
404 | 417 | |
405 | - if (!is_string($key) or empty($key)) |
|
406 | - throw new \InvalidArgumentException("\$key must be a not empty string."); |
|
418 | + if (!is_string($key) or empty($key)) { |
|
419 | + throw new \InvalidArgumentException("\$key must be a not empty string."); |
|
420 | + } |
|
407 | 421 | |
408 | - if (is_null($value)) |
|
409 | - throw new \InvalidArgumentException("\$value cannot be null."); |
|
422 | + if (is_null($value)) { |
|
423 | + throw new \InvalidArgumentException("\$value cannot be null."); |
|
424 | + } |
|
410 | 425 | |
411 | 426 | $request = new Request(Request::PUT_METHOD, "/_config/".$section."/".$key); |
412 | 427 | $request->setHeaderField(Request::CONTENT_TYPE_HF, "application/json"); |
@@ -422,11 +437,13 @@ discard block |
||
422 | 437 | * @see http://docs.couchdb.org/en/latest/api/configuration.html#delete-config-section-key |
423 | 438 | */ |
424 | 439 | public function deleteConfigKey($section, $key) { |
425 | - if (!is_string($section) or empty($section)) |
|
426 | - throw new \InvalidArgumentException("\$section must be a not empty string."); |
|
440 | + if (!is_string($section) or empty($section)) { |
|
441 | + throw new \InvalidArgumentException("\$section must be a not empty string."); |
|
442 | + } |
|
427 | 443 | |
428 | - if (!is_string($key) or empty($key)) |
|
429 | - throw new \InvalidArgumentException("\$key must be a not empty string."); |
|
444 | + if (!is_string($key) or empty($key)) { |
|
445 | + throw new \InvalidArgumentException("\$key must be a not empty string."); |
|
446 | + } |
|
430 | 447 | |
431 | 448 | $this->send(new Request(Request::DELETE_METHOD, "/_config/".$section."/".$key)); |
432 | 449 | } |
@@ -453,11 +470,13 @@ discard block |
||
453 | 470 | * @see http://docs.couchdb.org/en/latest/api/server/authn.html#post--_session |
454 | 471 | */ |
455 | 472 | public function setSession($userName, $password) { |
456 | - if (!is_string($userName) or empty($userName)) |
|
457 | - throw new \InvalidArgumentException("\$userName must be a not empty string."); |
|
473 | + if (!is_string($userName) or empty($userName)) { |
|
474 | + throw new \InvalidArgumentException("\$userName must be a not empty string."); |
|
475 | + } |
|
458 | 476 | |
459 | - if (!is_string($password) or empty($password)) |
|
460 | - throw new \InvalidArgumentException("\$password must be a not empty string."); |
|
477 | + if (!is_string($password) or empty($password)) { |
|
478 | + throw new \InvalidArgumentException("\$password must be a not empty string."); |
|
479 | + } |
|
461 | 480 | |
462 | 481 | $request = new Request(Request::POST_METHOD, "/_session"); |
463 | 482 | |
@@ -544,8 +563,9 @@ discard block |
||
544 | 563 | # One of the characters “_$()” «_$()» |
545 | 564 | # A character in the range between “+” and “/” «+-/» |
546 | 565 | # Assert position at the very end of the string «\z» |
547 | - if (preg_match('%\A[a-z][a-z\d_$()+-/]++\z%', $name) === FALSE) |
|
548 | - throw new \InvalidArgumentException("Invalid database name."); |
|
566 | + if (preg_match('%\A[a-z][a-z\d_$()+-/]++\z%', $name) === FALSE) { |
|
567 | + throw new \InvalidArgumentException("Invalid database name."); |
|
568 | + } |
|
549 | 569 | |
550 | 570 | $this->send(new Request(Request::PUT_METHOD, "/".rawurlencode($this->prefix.$name)."/")); |
551 | 571 | } |
@@ -583,8 +603,9 @@ discard block |
||
583 | 603 | public function getDbChanges($name, Opt\ChangesFeedOpts $opts = NULL) { |
584 | 604 | $request = new Request(Request::GET_METHOD, "/".rawurlencode($this->prefix.$name)."/_changes"); |
585 | 605 | |
586 | - if (isset($opts)) |
|
587 | - $request->setMultipleQueryParamsAtOnce($opts->asArray()); |
|
606 | + if (isset($opts)) { |
|
607 | + $request->setMultipleQueryParamsAtOnce($opts->asArray()); |
|
608 | + } |
|
588 | 609 | |
589 | 610 | return $this->send($request); |
590 | 611 | } |
@@ -773,40 +794,47 @@ discard block |
||
773 | 794 | is_string($targetDbUrl) && !empty($targetDbUrl)) { |
774 | 795 | $body["source"] = $sourceDbUrl; |
775 | 796 | $body["target"] = $targetDbUrl; |
797 | + } else { |
|
798 | + throw new \InvalidArgumentException("\$source_db_url and \$target_db_url must be non-empty strings."); |
|
776 | 799 | } |
777 | - else |
|
778 | - throw new \InvalidArgumentException("\$source_db_url and \$target_db_url must be non-empty strings."); |
|
779 | 800 | |
780 | - if (!is_bool($continuous)) |
|
781 | - throw new \InvalidArgumentException("\$continuous must be a bool."); |
|
782 | - elseif ($continuous) |
|
783 | - $body["continuous"] = $continuous; |
|
801 | + if (!is_bool($continuous)) { |
|
802 | + throw new \InvalidArgumentException("\$continuous must be a bool."); |
|
803 | + } elseif ($continuous) { |
|
804 | + $body["continuous"] = $continuous; |
|
805 | + } |
|
784 | 806 | |
785 | 807 | // Uses the specified proxy if any set. |
786 | - if (isset($proxy)) |
|
787 | - $body["proxy"] = $proxy; |
|
808 | + if (isset($proxy)) { |
|
809 | + $body["proxy"] = $proxy; |
|
810 | + } |
|
788 | 811 | |
789 | 812 | // create_target option |
790 | - if (!is_bool($createTargetDb)) |
|
791 | - throw new \InvalidArgumentException("\$createTargetDb must be a bool."); |
|
792 | - elseif ($createTargetDb) |
|
793 | - $body["create_target"] = $createTargetDb; |
|
813 | + if (!is_bool($createTargetDb)) { |
|
814 | + throw new \InvalidArgumentException("\$createTargetDb must be a bool."); |
|
815 | + } elseif ($createTargetDb) { |
|
816 | + $body["create_target"] = $createTargetDb; |
|
817 | + } |
|
794 | 818 | |
795 | 819 | if (!empty($filter)) { |
796 | - if (is_string($filter)) // filter option |
|
820 | + if (is_string($filter)) { |
|
821 | + // filter option |
|
797 | 822 | $body["filter"] = $filter; |
798 | - elseif (is_array($filter)) // doc_ids option |
|
823 | + } elseif (is_array($filter)) { |
|
824 | + // doc_ids option |
|
799 | 825 | $body["doc_ids"] = array_values($filter); |
800 | - else |
|
801 | - throw new \InvalidArgumentException("\$filter must be a string or an array."); |
|
826 | + } else { |
|
827 | + throw new \InvalidArgumentException("\$filter must be a string or an array."); |
|
828 | + } |
|
802 | 829 | } |
803 | 830 | |
804 | 831 | // queryParams option |
805 | 832 | if (!is_null($opts)) { |
806 | - if ($opts instanceof Opt\ViewQueryOpts) |
|
807 | - $body["query_params"] = get_object_vars($opts); |
|
808 | - else |
|
809 | - throw new \InvalidArgumentException("\$queryParams must be an instance of ViewQueryOpts class."); |
|
833 | + if ($opts instanceof Opt\ViewQueryOpts) { |
|
834 | + $body["query_params"] = get_object_vars($opts); |
|
835 | + } else { |
|
836 | + throw new \InvalidArgumentException("\$queryParams must be an instance of ViewQueryOpts class."); |
|
837 | + } |
|
810 | 838 | } |
811 | 839 | |
812 | 840 | $request = new Request(Request::POST_METHOD, "/_replicate"); |
@@ -823,8 +851,9 @@ discard block |
||
823 | 851 | * @see http://docs.couchdb.org/en/latest/api/server/common.html#canceling-continuous-replication |
824 | 852 | */ |
825 | 853 | public function stopReplication($replicationId) { |
826 | - if (is_null($replicationId)) |
|
827 | - throw new \InvalidArgumentException("You must provide a replication id."); |
|
854 | + if (is_null($replicationId)) { |
|
855 | + throw new \InvalidArgumentException("You must provide a replication id."); |
|
856 | + } |
|
828 | 857 | |
829 | 858 | $body["replication_id"] = $replicationId; |
830 | 859 | $body["cancel"] = TRUE; |
@@ -873,15 +902,16 @@ discard block |
||
873 | 902 | * @see http://docs.couchdb.org/en/latest/api/database/bulk-api.html#post--db-_all_docs |
874 | 903 | */ |
875 | 904 | public function queryAllDocs($dbName, array $keys = NULL, Opt\ViewQueryOpts $opts = NULL, IChunkHook $chunkHook = NULL) { |
876 | - if (is_null($keys)) |
|
877 | - $request = new Request(Request::GET_METHOD, "/".rawurlencode($this->prefix.$dbName)."/_all_docs"); |
|
878 | - else { |
|
905 | + if (is_null($keys)) { |
|
906 | + $request = new Request(Request::GET_METHOD, "/".rawurlencode($this->prefix.$dbName)."/_all_docs"); |
|
907 | + } else { |
|
879 | 908 | $request = new Request(Request::POST_METHOD, "/".rawurlencode($this->prefix.$dbName)."/_all_docs"); |
880 | 909 | $request->setBody(json_encode(['keys' => $keys])); |
881 | 910 | } |
882 | 911 | |
883 | - if (isset($opts)) |
|
884 | - $request->setMultipleQueryParamsAtOnce($opts->asArray()); |
|
912 | + if (isset($opts)) { |
|
913 | + $request->setMultipleQueryParamsAtOnce($opts->asArray()); |
|
914 | + } |
|
885 | 915 | |
886 | 916 | $result = $this->send($request, $chunkHook)->getBodyAsArray(); |
887 | 917 | |
@@ -910,12 +940,13 @@ discard block |
||
910 | 940 | public function queryView($dbName, $designDocName, $viewName, array $keys = NULL, Opt\ViewQueryOpts $opts = NULL, IChunkHook $chunkHook = NULL) { |
911 | 941 | $this->validateAndEncodeDocId($designDocName); |
912 | 942 | |
913 | - if (empty($viewName)) |
|
914 | - throw new \InvalidArgumentException("You must provide a valid \$viewName."); |
|
943 | + if (empty($viewName)) { |
|
944 | + throw new \InvalidArgumentException("You must provide a valid \$viewName."); |
|
945 | + } |
|
915 | 946 | |
916 | - if (empty($keys)) |
|
917 | - $request = new Request(Request::GET_METHOD, "/".rawurlencode($this->prefix.$dbName)."/_design/".$designDocName."/_view/".$viewName); |
|
918 | - else { |
|
947 | + if (empty($keys)) { |
|
948 | + $request = new Request(Request::GET_METHOD, "/".rawurlencode($this->prefix.$dbName)."/_design/".$designDocName."/_view/".$viewName); |
|
949 | + } else { |
|
919 | 950 | $request = new Request(Request::POST_METHOD, "/".rawurlencode($this->prefix.$dbName)."/_design/".$designDocName."/_view/".$viewName); |
920 | 951 | $request->setBody(json_encode(['keys' => $keys])); |
921 | 952 | } |
@@ -926,14 +957,15 @@ discard block |
||
926 | 957 | if (isset($opts)) { |
927 | 958 | $request->setMultipleQueryParamsAtOnce($opts->asArray()); |
928 | 959 | $includeMissingKeys = $opts->issetIncludeMissingKeys(); |
960 | + } else { |
|
961 | + $includeMissingKeys = FALSE; |
|
929 | 962 | } |
930 | - else |
|
931 | - $includeMissingKeys = FALSE; |
|
932 | 963 | |
933 | 964 | $result = $this->send($request, $chunkHook)->getBodyAsArray(); |
934 | 965 | |
935 | - if ($includeMissingKeys) |
|
936 | - $this->addMissingRows($keys, $result['rows']); |
|
966 | + if ($includeMissingKeys) { |
|
967 | + $this->addMissingRows($keys, $result['rows']); |
|
968 | + } |
|
937 | 969 | |
938 | 970 | return new Result\QueryResult($result); |
939 | 971 | } |
@@ -960,30 +992,33 @@ discard block |
||
960 | 992 | $handler = new Handler\ViewHandler('temp'); |
961 | 993 | $handler->language = $language; |
962 | 994 | $handler->mapFn = $mapFn; |
963 | - if (!empty($reduce)) |
|
964 | - $handler->reduceFn = $reduceFn; |
|
995 | + if (!empty($reduce)) { |
|
996 | + $handler->reduceFn = $reduceFn; |
|
997 | + } |
|
965 | 998 | |
966 | 999 | $request = new Request(Request::POST_METHOD, "/".rawurlencode($this->prefix.$dbName)."/_temp_view"); |
967 | 1000 | $request->setHeaderField(Request::CONTENT_TYPE_HF, "application/json"); |
968 | 1001 | |
969 | 1002 | $array = $handler->asArray(); |
970 | 1003 | |
971 | - if (!empty($keys)) |
|
972 | - $array['keys'] = $keys; |
|
1004 | + if (!empty($keys)) { |
|
1005 | + $array['keys'] = $keys; |
|
1006 | + } |
|
973 | 1007 | |
974 | 1008 | $request->setBody(json_encode($array)); |
975 | 1009 | |
976 | 1010 | if (isset($opts)) { |
977 | 1011 | $request->setMultipleQueryParamsAtOnce($opts->asArray()); |
978 | 1012 | $includeMissingKeys = $opts->issetIncludeMissingKeys(); |
1013 | + } else { |
|
1014 | + $includeMissingKeys = FALSE; |
|
979 | 1015 | } |
980 | - else |
|
981 | - $includeMissingKeys = FALSE; |
|
982 | 1016 | |
983 | 1017 | $result = $this->send($request, $chunkHook)->getBodyAsArray(); |
984 | 1018 | |
985 | - if ($includeMissingKeys) |
|
986 | - $this->addMissingRows($keys, $result['rows']); |
|
1019 | + if ($includeMissingKeys) { |
|
1020 | + $this->addMissingRows($keys, $result['rows']); |
|
1021 | + } |
|
987 | 1022 | |
988 | 1023 | return new Result\QueryResult($result); |
989 | 1024 | } |
@@ -1042,8 +1077,9 @@ discard block |
||
1042 | 1077 | * @see http://docs.couchdb.org/en/latest/api/database/misc.html#put--db-_revs_limit |
1043 | 1078 | */ |
1044 | 1079 | public function setRevsLimit($dbName, $revsLimit = self::REVS_LIMIT) { |
1045 | - if (!is_int($revsLimit) or ($revsLimit <= 0)) |
|
1046 | - throw new \InvalidArgumentException("\$revsLimit must be a positive integer."); |
|
1080 | + if (!is_int($revsLimit) or ($revsLimit <= 0)) { |
|
1081 | + throw new \InvalidArgumentException("\$revsLimit must be a positive integer."); |
|
1082 | + } |
|
1047 | 1083 | |
1048 | 1084 | $request = new Request(Request::PUT_METHOD, "/".rawurlencode($this->prefix.$dbName)."/_revs_limit"); |
1049 | 1085 | $request->setHeaderField(Request::CONTENT_TYPE_HF, "application/json"); |
@@ -1102,16 +1138,17 @@ discard block |
||
1102 | 1138 | $request = new Request(Request::GET_METHOD, $requestPath); |
1103 | 1139 | |
1104 | 1140 | // Retrieves the specific revision of the document. |
1105 | - if (!empty($rev)) |
|
1106 | - $request->setQueryParam("rev", (string)$rev); |
|
1141 | + if (!empty($rev)) { |
|
1142 | + $request->setQueryParam("rev", (string)$rev); |
|
1143 | + } |
|
1107 | 1144 | |
1108 | 1145 | // If there are any options, add them to the request. |
1109 | 1146 | if (isset($opts)) { |
1110 | 1147 | $request->setMultipleQueryParamsAtOnce($opts->asArray()); |
1111 | 1148 | $ignoreClass = $opts->issetIgnoreClass(); |
1149 | + } else { |
|
1150 | + $ignoreClass = FALSE; |
|
1112 | 1151 | } |
1113 | - else |
|
1114 | - $ignoreClass = FALSE; |
|
1115 | 1152 | |
1116 | 1153 | $response = $this->send($request); |
1117 | 1154 | $body = $response->getBodyAsArray(); |
@@ -1123,18 +1160,18 @@ discard block |
||
1123 | 1160 | if (!$ignoreClass && isset($body['class'])) { // Special document class inherited from Doc or LocalDoc. |
1124 | 1161 | $class = "\\".$body['class']; |
1125 | 1162 | $doc = new $class; |
1163 | + } elseif ($path == self::DESIGN_DOC_PATH) { |
|
1164 | + $doc = new Doc\DesignDoc; |
|
1165 | + } else { |
|
1166 | + $doc = NULL; |
|
1126 | 1167 | } |
1127 | - elseif ($path == self::DESIGN_DOC_PATH) |
|
1128 | - $doc = new Doc\DesignDoc; |
|
1129 | - else |
|
1130 | - $doc = NULL; |
|
1131 | 1168 | |
1132 | 1169 | if (is_object($doc)) { |
1133 | 1170 | $doc->assignArray($body); |
1134 | 1171 | return $doc; |
1172 | + } else { |
|
1173 | + return $response; |
|
1135 | 1174 | } |
1136 | - else |
|
1137 | - return $response; |
|
1138 | 1175 | } |
1139 | 1176 | |
1140 | 1177 | |
@@ -1154,8 +1191,9 @@ discard block |
||
1154 | 1191 | public function saveDoc($dbName, Doc\IDoc $doc, $batchMode = FALSE) { |
1155 | 1192 | // Whether the document has an id we use a different HTTP method. Using POST CouchDB generates an id for the doc |
1156 | 1193 | // using PUT we need to specify one. We can still use the function getUuids() to ask CouchDB for some ids. |
1157 | - if (!$doc->issetId()) |
|
1158 | - $doc->setId(Generator\UUID::generate(Generator\UUID::UUID_RANDOM, Generator\UUID::FMT_STRING)); |
|
1194 | + if (!$doc->issetId()) { |
|
1195 | + $doc->setId(Generator\UUID::generate(Generator\UUID::UUID_RANDOM, Generator\UUID::FMT_STRING)); |
|
1196 | + } |
|
1159 | 1197 | |
1160 | 1198 | $this->setDocInfo($doc); |
1161 | 1199 | |
@@ -1169,8 +1207,9 @@ discard block |
||
1169 | 1207 | $request->setBody($doc->asJson()); |
1170 | 1208 | |
1171 | 1209 | // Enables batch mode. |
1172 | - if ($batchMode) |
|
1173 | - $request->setQueryParam("batch", "ok"); |
|
1210 | + if ($batchMode) { |
|
1211 | + $request->setQueryParam("batch", "ok"); |
|
1212 | + } |
|
1174 | 1213 | |
1175 | 1214 | $this->send($request); |
1176 | 1215 | } |
@@ -1223,10 +1262,11 @@ discard block |
||
1223 | 1262 | // This request uses the special method COPY. |
1224 | 1263 | $request = new Request(Request::COPY_METHOD, $path); |
1225 | 1264 | |
1226 | - if (empty($rev)) |
|
1227 | - $request->setHeaderField(Request::DESTINATION_HF, $targetDocId); |
|
1228 | - else |
|
1229 | - $request->setHeaderField(Request::DESTINATION_HF, $targetDocId."?rev=".(string)$rev); |
|
1265 | + if (empty($rev)) { |
|
1266 | + $request->setHeaderField(Request::DESTINATION_HF, $targetDocId); |
|
1267 | + } else { |
|
1268 | + $request->setHeaderField(Request::DESTINATION_HF, $targetDocId."?rev=".(string)$rev); |
|
1269 | + } |
|
1230 | 1270 | |
1231 | 1271 | $this->send($request); |
1232 | 1272 | } |
@@ -1255,8 +1295,9 @@ discard block |
||
1255 | 1295 | $request = new Request(Request::POST_METHOD, $path); |
1256 | 1296 | |
1257 | 1297 | $purge = []; |
1258 | - foreach ($refs as $ref) |
|
1259 | - $purge[] = $ref->asArray(); |
|
1298 | + foreach ($refs as $ref) { |
|
1299 | + $purge[] = $ref->asArray(); |
|
1300 | + } |
|
1260 | 1301 | |
1261 | 1302 | $request->setBody(json_encode($purge)); |
1262 | 1303 | |
@@ -1288,26 +1329,30 @@ discard block |
||
1288 | 1329 | * @see http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API |
1289 | 1330 | */ |
1290 | 1331 | public function performBulkOperations($dbName, array $docs, $fullCommit = FALSE, $allOrNothing = FALSE, $newEdits = TRUE) { |
1291 | - if (count($docs) == 0) |
|
1292 | - throw new \InvalidArgumentException("The \$docs array cannot be empty."); |
|
1293 | - else |
|
1294 | - $operations = []; |
|
1332 | + if (count($docs) == 0) { |
|
1333 | + throw new \InvalidArgumentException("The \$docs array cannot be empty."); |
|
1334 | + } else { |
|
1335 | + $operations = []; |
|
1336 | + } |
|
1295 | 1337 | |
1296 | 1338 | $path = "/".rawurlencode($this->prefix.$dbName)."/_bulk_docs"; |
1297 | 1339 | |
1298 | 1340 | $request = new Request(Request::POST_METHOD, $path); |
1299 | 1341 | $request->setHeaderField(Request::CONTENT_TYPE_HF, "application/json"); |
1300 | 1342 | |
1301 | - if ($fullCommit) |
|
1302 | - $request->setHeaderField(Request::X_COUCHDB_FULL_COMMIT_HF, "full_commit"); |
|
1303 | - else |
|
1304 | - $request->setHeaderField(Request::X_COUCHDB_FULL_COMMIT_HF, "delay_commit"); |
|
1343 | + if ($fullCommit) { |
|
1344 | + $request->setHeaderField(Request::X_COUCHDB_FULL_COMMIT_HF, "full_commit"); |
|
1345 | + } else { |
|
1346 | + $request->setHeaderField(Request::X_COUCHDB_FULL_COMMIT_HF, "delay_commit"); |
|
1347 | + } |
|
1305 | 1348 | |
1306 | - if ($allOrNothing) |
|
1307 | - $operations['all_or_nothing'] = 'true'; |
|
1349 | + if ($allOrNothing) { |
|
1350 | + $operations['all_or_nothing'] = 'true'; |
|
1351 | + } |
|
1308 | 1352 | |
1309 | - if (!$newEdits) |
|
1310 | - $operations['new_edits'] = 'false'; |
|
1353 | + if (!$newEdits) { |
|
1354 | + $operations['new_edits'] = 'false'; |
|
1355 | + } |
|
1311 | 1356 | |
1312 | 1357 | foreach ($docs as $doc) { |
1313 | 1358 | $this->setDocInfo($doc); |
@@ -1345,8 +1390,9 @@ discard block |
||
1345 | 1390 | $request = new Request(Request::HEAD_METHOD, $path); |
1346 | 1391 | |
1347 | 1392 | // In case we want retrieve a specific document revision. |
1348 | - if (!empty($rev)) |
|
1349 | - $request->setQueryParam("rev", (string)$rev); |
|
1393 | + if (!empty($rev)) { |
|
1394 | + $request->setQueryParam("rev", (string)$rev); |
|
1395 | + } |
|
1350 | 1396 | |
1351 | 1397 | return $this->send($request)->getHeaderAsArray(); |
1352 | 1398 | } |
@@ -1372,8 +1418,9 @@ discard block |
||
1372 | 1418 | $request = new Request(Request::GET_METHOD, $path); |
1373 | 1419 | |
1374 | 1420 | // In case we want retrieve a specific document revision. |
1375 | - if (!empty($rev)) |
|
1376 | - $request->setQueryParam("rev", (string)$rev); |
|
1421 | + if (!empty($rev)) { |
|
1422 | + $request->setQueryParam("rev", (string)$rev); |
|
1423 | + } |
|
1377 | 1424 | |
1378 | 1425 | return $this->send($request)->getBody(); |
1379 | 1426 | } |
@@ -1402,8 +1449,9 @@ discard block |
||
1402 | 1449 | $request->setBody(base64_encode($attachment->getData())); |
1403 | 1450 | |
1404 | 1451 | // In case of adding or updating an existence document. |
1405 | - if (!empty($rev)) |
|
1406 | - $request->setQueryParam("rev", (string)$rev); |
|
1452 | + if (!empty($rev)) { |
|
1453 | + $request->setQueryParam("rev", (string)$rev); |
|
1454 | + } |
|
1407 | 1455 | |
1408 | 1456 | return $this->send($request); |
1409 | 1457 | } |