@@ -102,10 +102,13 @@ discard block |
||
| 102 | 102 | foreach ($keys as $key) { |
| 103 | 103 | $hash = md5(json_encode($key)); |
| 104 | 104 | |
| 105 | - if (isset($matches[$hash])) // Match found. |
|
| 105 | + if (isset($matches[$hash])) { |
|
| 106 | + // Match found. |
|
| 106 | 107 | $allRows[] = $matches[$hash]; |
| 107 | - else // No match found. |
|
| 108 | + } else { |
|
| 109 | + // No match found. |
|
| 108 | 110 | $allRows[] = ['id' => NULL, 'key' => $key, 'value' => NULL]; |
| 111 | + } |
|
| 109 | 112 | } |
| 110 | 113 | |
| 111 | 114 | // Overrides the response, replacing rows. |
@@ -176,15 +179,18 @@ discard block |
||
| 176 | 179 | * @param[in] bool $excludeLocal Document path. |
| 177 | 180 | */ |
| 178 | 181 | public function validateDocPath($path, $excludeLocal = FALSE) { |
| 179 | - if (empty($path)) // STD_DOC_PATH |
|
| 182 | + if (empty($path)) { |
|
| 183 | + // STD_DOC_PATH |
|
| 180 | 184 | return; |
| 185 | + } |
|
| 181 | 186 | |
| 182 | - if ($path == self::DESIGN_DOC_PATH) |
|
| 183 | - return; |
|
| 184 | - elseif ($path == self::LOCAL_DOC_PATH && $excludeLocal) |
|
| 185 | - throw new \InvalidArgumentException("Local document doesn't have attachments."); |
|
| 186 | - else |
|
| 187 | - throw new \InvalidArgumentException("Invalid document path."); |
|
| 187 | + if ($path == self::DESIGN_DOC_PATH) { |
|
| 188 | + return; |
|
| 189 | + } elseif ($path == self::LOCAL_DOC_PATH && $excludeLocal) { |
|
| 190 | + throw new \InvalidArgumentException("Local document doesn't have attachments."); |
|
| 191 | + } else { |
|
| 192 | + throw new \InvalidArgumentException("Invalid document path."); |
|
| 193 | + } |
|
| 188 | 194 | } |
| 189 | 195 | |
| 190 | 196 | |
@@ -206,10 +212,11 @@ discard block |
||
| 206 | 212 | # One of the characters “_$()” «_$()» |
| 207 | 213 | # A character in the range between “+” and “/” «+-/» |
| 208 | 214 | # Assert position at the very end of the string «\z» |
| 209 | - if (preg_match('%\A[a-z][a-z\d_$()+-/]++\z%', $name)) |
|
| 210 | - return $name = rawurlencode($name); |
|
| 211 | - else |
|
| 212 | - throw new \InvalidArgumentException("Invalid database name."); |
|
| 215 | + if (preg_match('%\A[a-z][a-z\d_$()+-/]++\z%', $name)) { |
|
| 216 | + return $name = rawurlencode($name); |
|
| 217 | + } else { |
|
| 218 | + throw new \InvalidArgumentException("Invalid database name."); |
|
| 219 | + } |
|
| 213 | 220 | } |
| 214 | 221 | |
| 215 | 222 | |
@@ -220,10 +227,11 @@ discard block |
||
| 220 | 227 | * @param string $docId Document id. |
| 221 | 228 | */ |
| 222 | 229 | public function validateAndEncodeDocId(&$docId) { |
| 223 | - if (!empty($docId)) |
|
| 224 | - $docId = rawurlencode($docId); |
|
| 225 | - else |
|
| 226 | - throw new \InvalidArgumentException("\$docId must be a non-empty string."); |
|
| 230 | + if (!empty($docId)) { |
|
| 231 | + $docId = rawurlencode($docId); |
|
| 232 | + } else { |
|
| 233 | + throw new \InvalidArgumentException("\$docId must be a non-empty string."); |
|
| 234 | + } |
|
| 227 | 235 | } |
| 228 | 236 | |
| 229 | 237 | //!@} |
@@ -255,10 +263,10 @@ discard block |
||
| 255 | 263 | // before the client has received the entire response. To avoid problems, we trap the exception and we go on. |
| 256 | 264 | try { |
| 257 | 265 | $this->send($request); |
| 258 | - } |
|
| 259 | - catch (\Exception $e) { |
|
| 260 | - if ($e->getCode() > 0) |
|
| 261 | - throw $e; |
|
| 266 | + } catch (\Exception $e) { |
|
| 267 | + if ($e->getCode() > 0) { |
|
| 268 | + throw $e; |
|
| 269 | + } |
|
| 262 | 270 | } |
| 263 | 271 | } |
| 264 | 272 | |
@@ -295,10 +303,11 @@ discard block |
||
| 295 | 303 | public function getFavicon() { |
| 296 | 304 | $response = $this->send(new Request(Request::GET_METHOD, "/favicon.ico")); |
| 297 | 305 | |
| 298 | - if ($response->getHeaderFieldValue(Request::CONTENT_TYPE_HF) == "image/x-icon") |
|
| 299 | - return $response->getBody(); |
|
| 300 | - else |
|
| 301 | - throw new \InvalidArgumentException("Content-Type must be image/x-icon."); |
|
| 306 | + if ($response->getHeaderFieldValue(Request::CONTENT_TYPE_HF) == "image/x-icon") { |
|
| 307 | + return $response->getBody(); |
|
| 308 | + } else { |
|
| 309 | + throw new \InvalidArgumentException("Content-Type must be image/x-icon."); |
|
| 310 | + } |
|
| 302 | 311 | } |
| 303 | 312 | |
| 304 | 313 | |
@@ -334,8 +343,9 @@ discard block |
||
| 334 | 343 | |
| 335 | 344 | $request = new Request(Request::GET_METHOD, "/_db_updates"); |
| 336 | 345 | |
| 337 | - if (isset($opts)) |
|
| 338 | - $request->setMultipleQueryParamsAtOnce($opts->asArray()); |
|
| 346 | + if (isset($opts)) { |
|
| 347 | + $request->setMultipleQueryParamsAtOnce($opts->asArray()); |
|
| 348 | + } |
|
| 339 | 349 | |
| 340 | 350 | return $this->send($request)->getBodyAsArray(); |
| 341 | 351 | } |
@@ -364,9 +374,9 @@ discard block |
||
| 364 | 374 | $request = new Request(Request::GET_METHOD, "/_log"); |
| 365 | 375 | $request->setQueryParam("bytes", $bytes); |
| 366 | 376 | return $this->send($request)->getBody(); |
| 377 | + } else { |
|
| 378 | + throw new \InvalidArgumentException("\$bytes must be a positive integer."); |
|
| 367 | 379 | } |
| 368 | - else |
|
| 369 | - throw new \InvalidArgumentException("\$bytes must be a positive integer."); |
|
| 370 | 380 | } |
| 371 | 381 | |
| 372 | 382 | |
@@ -383,13 +393,15 @@ discard block |
||
| 383 | 393 | |
| 384 | 394 | $response = $this->send($request); |
| 385 | 395 | |
| 386 | - if ($count == 1) // We don't need to use === operator because, just above, we made a type checking. |
|
| 396 | + if ($count == 1) { |
|
| 397 | + // We don't need to use === operator because, just above, we made a type checking. |
|
| 387 | 398 | return $response->getBodyAsArray()['uuids'][0]; |
| 388 | - else |
|
| 389 | - return $response->getBodyAsArray()['uuids']; |
|
| 399 | + } else { |
|
| 400 | + return $response->getBodyAsArray()['uuids']; |
|
| 401 | + } |
|
| 402 | + } else { |
|
| 403 | + throw new \InvalidArgumentException("\$count must be a positive integer."); |
|
| 390 | 404 | } |
| 391 | - else |
|
| 392 | - throw new \InvalidArgumentException("\$count must be a positive integer."); |
|
| 393 | 405 | } |
| 394 | 406 | |
| 395 | 407 | //!@} |
@@ -403,8 +415,9 @@ discard block |
||
| 403 | 415 | private function endTransaction() { |
| 404 | 416 | if (is_array($this->transaction)) { |
| 405 | 417 | |
| 406 | - foreach ($this->transaction as $doc) |
|
| 407 | - unset($doc); |
|
| 418 | + foreach ($this->transaction as $doc) { |
|
| 419 | + unset($doc); |
|
| 420 | + } |
|
| 408 | 421 | |
| 409 | 422 | unset($this->transaction); |
| 410 | 423 | } |
@@ -416,10 +429,11 @@ discard block |
||
| 416 | 429 | * @brief Starts a new transaction. |
| 417 | 430 | */ |
| 418 | 431 | public function begin() { |
| 419 | - if (is_null($this->transaction)) |
|
| 420 | - $this->transaction = []; |
|
| 421 | - else |
|
| 422 | - throw new \RuntimeException("A transaction is already in progress."); |
|
| 432 | + if (is_null($this->transaction)) { |
|
| 433 | + $this->transaction = []; |
|
| 434 | + } else { |
|
| 435 | + throw new \RuntimeException("A transaction is already in progress."); |
|
| 436 | + } |
|
| 423 | 437 | } |
| 424 | 438 | |
| 425 | 439 | |
@@ -445,8 +459,7 @@ discard block |
||
| 445 | 459 | try { |
| 446 | 460 | $this->performBulkOperations($this->transaction, $immediately, TRUE, $newEdits); |
| 447 | 461 | $this->endTransaction(); |
| 448 | - } |
|
| 449 | - catch (\Exception $e) { |
|
| 462 | + } catch (\Exception $e) { |
|
| 450 | 463 | $this->rollback(); |
| 451 | 464 | throw $e; |
| 452 | 465 | } |
@@ -481,8 +494,9 @@ discard block |
||
| 481 | 494 | if (!empty($section)) { |
| 482 | 495 | $path .= "/".$section; |
| 483 | 496 | |
| 484 | - if (!empty($key)) |
|
| 485 | - $path .= "/".$key; |
|
| 497 | + if (!empty($key)) { |
|
| 498 | + $path .= "/".$key; |
|
| 499 | + } |
|
| 486 | 500 | } |
| 487 | 501 | |
| 488 | 502 | return $this->send(new Request(Request::GET_METHOD, $path))->getBodyAsArray(); |
@@ -497,14 +511,17 @@ discard block |
||
| 497 | 511 | * @see http://docs.couchdb.org/en/latest/api/server/configuration.html#put--_config-section-key |
| 498 | 512 | */ |
| 499 | 513 | public function setConfigKey($section, $key, $value) { |
| 500 | - if (!is_string($section) or empty($section)) |
|
| 501 | - throw new \InvalidArgumentException("\$section must be a not empty string."); |
|
| 514 | + if (!is_string($section) or empty($section)) { |
|
| 515 | + throw new \InvalidArgumentException("\$section must be a not empty string."); |
|
| 516 | + } |
|
| 502 | 517 | |
| 503 | - if (!is_string($key) or empty($key)) |
|
| 504 | - throw new \InvalidArgumentException("\$key must be a not empty string."); |
|
| 518 | + if (!is_string($key) or empty($key)) { |
|
| 519 | + throw new \InvalidArgumentException("\$key must be a not empty string."); |
|
| 520 | + } |
|
| 505 | 521 | |
| 506 | - if (is_null($value)) |
|
| 507 | - throw new \InvalidArgumentException("\$value cannot be null."); |
|
| 522 | + if (is_null($value)) { |
|
| 523 | + throw new \InvalidArgumentException("\$value cannot be null."); |
|
| 524 | + } |
|
| 508 | 525 | |
| 509 | 526 | $request = new Request(Request::PUT_METHOD, "/_config/".$section."/".$key); |
| 510 | 527 | $request->setHeaderField(Request::CONTENT_TYPE_HF, "application/json"); |
@@ -520,11 +537,13 @@ discard block |
||
| 520 | 537 | * @see http://docs.couchdb.org/en/latest/api/configuration.html#delete-config-section-key |
| 521 | 538 | */ |
| 522 | 539 | public function deleteConfigKey($section, $key) { |
| 523 | - if (!is_string($section) or empty($section)) |
|
| 524 | - throw new \InvalidArgumentException("\$section must be a not empty string."); |
|
| 540 | + if (!is_string($section) or empty($section)) { |
|
| 541 | + throw new \InvalidArgumentException("\$section must be a not empty string."); |
|
| 542 | + } |
|
| 525 | 543 | |
| 526 | - if (!is_string($key) or empty($key)) |
|
| 527 | - throw new \InvalidArgumentException("\$key must be a not empty string."); |
|
| 544 | + if (!is_string($key) or empty($key)) { |
|
| 545 | + throw new \InvalidArgumentException("\$key must be a not empty string."); |
|
| 546 | + } |
|
| 528 | 547 | |
| 529 | 548 | $this->send(new Request(Request::DELETE_METHOD, "/_config/".$section."/".$key)); |
| 530 | 549 | } |
@@ -551,11 +570,13 @@ discard block |
||
| 551 | 570 | * @see http://docs.couchdb.org/en/latest/api/server/authn.html#post--_session |
| 552 | 571 | */ |
| 553 | 572 | public function setSession($userName, $password) { |
| 554 | - if (!is_string($userName) or empty($userName)) |
|
| 555 | - throw new \InvalidArgumentException("\$userName must be a not empty string."); |
|
| 573 | + if (!is_string($userName) or empty($userName)) { |
|
| 574 | + throw new \InvalidArgumentException("\$userName must be a not empty string."); |
|
| 575 | + } |
|
| 556 | 576 | |
| 557 | - if (!is_string($password) or empty($password)) |
|
| 558 | - throw new \InvalidArgumentException("\$password must be a not empty string."); |
|
| 577 | + if (!is_string($password) or empty($password)) { |
|
| 578 | + throw new \InvalidArgumentException("\$password must be a not empty string."); |
|
| 579 | + } |
|
| 559 | 580 | |
| 560 | 581 | $request = new Request(Request::POST_METHOD, "/_session"); |
| 561 | 582 | |
@@ -650,8 +671,9 @@ discard block |
||
| 650 | 671 | * you are making a not supported call to CouchDB. |
| 651 | 672 | */ |
| 652 | 673 | public function checkForDb() { |
| 653 | - if (empty($this->dbName)) |
|
| 654 | - throw new \RuntimeException("No database selected."); |
|
| 674 | + if (empty($this->dbName)) { |
|
| 675 | + throw new \RuntimeException("No database selected."); |
|
| 676 | + } |
|
| 655 | 677 | } |
| 656 | 678 | |
| 657 | 679 | |
@@ -669,11 +691,12 @@ discard block |
||
| 669 | 691 | if ($name != $this->dbName) { |
| 670 | 692 | $this->send(new Request(Request::PUT_METHOD, "/".$name."/")); |
| 671 | 693 | |
| 672 | - if ($autoSelect) |
|
| 673 | - $this->dbName = $name; |
|
| 694 | + if ($autoSelect) { |
|
| 695 | + $this->dbName = $name; |
|
| 696 | + } |
|
| 697 | + } else { |
|
| 698 | + throw new \UnexpectedValueException("You can't create a database with the same name of the selected database."); |
|
| 674 | 699 | } |
| 675 | - else |
|
| 676 | - throw new \UnexpectedValueException("You can't create a database with the same name of the selected database."); |
|
| 677 | 700 | } |
| 678 | 701 | |
| 679 | 702 | |
@@ -687,10 +710,11 @@ discard block |
||
| 687 | 710 | public function deleteDb($name) { |
| 688 | 711 | $this->validateAndEncodeDbName($name); |
| 689 | 712 | |
| 690 | - if ($name != $this->dbName) |
|
| 691 | - $this->send(new Request(Request::DELETE_METHOD, "/".$name)); |
|
| 692 | - else |
|
| 693 | - throw new \UnexpectedValueException("You can't delete the selected database."); |
|
| 713 | + if ($name != $this->dbName) { |
|
| 714 | + $this->send(new Request(Request::DELETE_METHOD, "/".$name)); |
|
| 715 | + } else { |
|
| 716 | + throw new \UnexpectedValueException("You can't delete the selected database."); |
|
| 717 | + } |
|
| 694 | 718 | } |
| 695 | 719 | |
| 696 | 720 | |
@@ -718,8 +742,9 @@ discard block |
||
| 718 | 742 | |
| 719 | 743 | $request = new Request(Request::GET_METHOD, "/".$this->dbName."/_changes"); |
| 720 | 744 | |
| 721 | - if (isset($opts)) |
|
| 722 | - $request->setMultipleQueryParamsAtOnce($opts->asArray()); |
|
| 745 | + if (isset($opts)) { |
|
| 746 | + $request->setMultipleQueryParamsAtOnce($opts->asArray()); |
|
| 747 | + } |
|
| 723 | 748 | |
| 724 | 749 | return $this->send($request); |
| 725 | 750 | } |
@@ -914,40 +939,47 @@ discard block |
||
| 914 | 939 | is_string($targetDbUrl) && !empty($targetDbUrl)) { |
| 915 | 940 | $body["source"] = $sourceDbUrl; |
| 916 | 941 | $body["target"] = $targetDbUrl; |
| 942 | + } else { |
|
| 943 | + throw new \InvalidArgumentException("\$source_db_url and \$target_db_url must be non-empty strings."); |
|
| 917 | 944 | } |
| 918 | - else |
|
| 919 | - throw new \InvalidArgumentException("\$source_db_url and \$target_db_url must be non-empty strings."); |
|
| 920 | 945 | |
| 921 | - if (!is_bool($continuous)) |
|
| 922 | - throw new \InvalidArgumentException("\$continuous must be a bool."); |
|
| 923 | - elseif ($continuous) |
|
| 924 | - $body["continuous"] = $continuous; |
|
| 946 | + if (!is_bool($continuous)) { |
|
| 947 | + throw new \InvalidArgumentException("\$continuous must be a bool."); |
|
| 948 | + } elseif ($continuous) { |
|
| 949 | + $body["continuous"] = $continuous; |
|
| 950 | + } |
|
| 925 | 951 | |
| 926 | 952 | // Uses the specified proxy if any set. |
| 927 | - if (isset($proxy)) |
|
| 928 | - $body["proxy"] = $this->proxy; |
|
| 953 | + if (isset($proxy)) { |
|
| 954 | + $body["proxy"] = $this->proxy; |
|
| 955 | + } |
|
| 929 | 956 | |
| 930 | 957 | // create_target option |
| 931 | - if (!is_bool($createTargetDb)) |
|
| 932 | - throw new \InvalidArgumentException("\$createTargetDb must be a bool."); |
|
| 933 | - elseif ($createTargetDb) |
|
| 934 | - $body["create_target"] = $createTargetDb; |
|
| 958 | + if (!is_bool($createTargetDb)) { |
|
| 959 | + throw new \InvalidArgumentException("\$createTargetDb must be a bool."); |
|
| 960 | + } elseif ($createTargetDb) { |
|
| 961 | + $body["create_target"] = $createTargetDb; |
|
| 962 | + } |
|
| 935 | 963 | |
| 936 | 964 | if (!empty($filter)) { |
| 937 | - if (is_string($filter)) // filter option |
|
| 965 | + if (is_string($filter)) { |
|
| 966 | + // filter option |
|
| 938 | 967 | $body["filter"] = $filter; |
| 939 | - elseif (is_array($filter)) // doc_ids option |
|
| 968 | + } elseif (is_array($filter)) { |
|
| 969 | + // doc_ids option |
|
| 940 | 970 | $body["doc_ids"] = array_values($filter); |
| 941 | - else |
|
| 942 | - throw new \InvalidArgumentException("\$filter must be a string or an array."); |
|
| 971 | + } else { |
|
| 972 | + throw new \InvalidArgumentException("\$filter must be a string or an array."); |
|
| 973 | + } |
|
| 943 | 974 | } |
| 944 | 975 | |
| 945 | 976 | // queryParams option |
| 946 | 977 | if (!is_null($opts)) { |
| 947 | - if ($opts instanceof Opt\ViewQueryOpts) |
|
| 948 | - $body["query_params"] = get_object_vars($opts); |
|
| 949 | - else |
|
| 950 | - throw new \InvalidArgumentException("\$queryParams must be an instance of ViewQueryOpts class."); |
|
| 978 | + if ($opts instanceof Opt\ViewQueryOpts) { |
|
| 979 | + $body["query_params"] = get_object_vars($opts); |
|
| 980 | + } else { |
|
| 981 | + throw new \InvalidArgumentException("\$queryParams must be an instance of ViewQueryOpts class."); |
|
| 982 | + } |
|
| 951 | 983 | } |
| 952 | 984 | |
| 953 | 985 | $request = new Request(Request::POST_METHOD, "/_replicate"); |
@@ -964,8 +996,9 @@ discard block |
||
| 964 | 996 | * @see http://docs.couchdb.org/en/latest/api/server/common.html#canceling-continuous-replication |
| 965 | 997 | */ |
| 966 | 998 | public function stopReplication($replicationId) { |
| 967 | - if (is_null($replicationId)) |
|
| 968 | - throw new \InvalidArgumentException("You must provide a replication id."); |
|
| 999 | + if (is_null($replicationId)) { |
|
| 1000 | + throw new \InvalidArgumentException("You must provide a replication id."); |
|
| 1001 | + } |
|
| 969 | 1002 | |
| 970 | 1003 | $body["replication_id"] = $replicationId; |
| 971 | 1004 | $body["cancel"] = TRUE; |
@@ -1015,15 +1048,16 @@ discard block |
||
| 1015 | 1048 | public function queryAllDocs(array $keys = NULL, Opt\ViewQueryOpts $opts = NULL, Hook\IChunkHook $chunkHook = NULL) { |
| 1016 | 1049 | $this->checkForDb(); |
| 1017 | 1050 | |
| 1018 | - if (is_null($keys)) |
|
| 1019 | - $request = new Request(Request::GET_METHOD, "/".$this->dbName."/_all_docs"); |
|
| 1020 | - else { |
|
| 1051 | + if (is_null($keys)) { |
|
| 1052 | + $request = new Request(Request::GET_METHOD, "/".$this->dbName."/_all_docs"); |
|
| 1053 | + } else { |
|
| 1021 | 1054 | $request = new Request(Request::POST_METHOD, "/".$this->dbName."/_all_docs"); |
| 1022 | 1055 | $request->setBody(json_encode(['keys' => $keys])); |
| 1023 | 1056 | } |
| 1024 | 1057 | |
| 1025 | - if (isset($opts)) |
|
| 1026 | - $request->setMultipleQueryParamsAtOnce($opts->asArray()); |
|
| 1058 | + if (isset($opts)) { |
|
| 1059 | + $request->setMultipleQueryParamsAtOnce($opts->asArray()); |
|
| 1060 | + } |
|
| 1027 | 1061 | |
| 1028 | 1062 | $result = $this->send($request, $chunkHook)->getBodyAsArray(); |
| 1029 | 1063 | |
@@ -1052,12 +1086,13 @@ discard block |
||
| 1052 | 1086 | $this->checkForDb(); |
| 1053 | 1087 | $this->validateAndEncodeDocId($designDocName); |
| 1054 | 1088 | |
| 1055 | - if (empty($viewName)) |
|
| 1056 | - throw new \InvalidArgumentException("You must provide a valid \$viewName."); |
|
| 1089 | + if (empty($viewName)) { |
|
| 1090 | + throw new \InvalidArgumentException("You must provide a valid \$viewName."); |
|
| 1091 | + } |
|
| 1057 | 1092 | |
| 1058 | - if (empty($keys)) |
|
| 1059 | - $request = new Request(Request::GET_METHOD, "/".$this->dbName."/_design/".$designDocName."/_view/".$viewName); |
|
| 1060 | - else { |
|
| 1093 | + if (empty($keys)) { |
|
| 1094 | + $request = new Request(Request::GET_METHOD, "/".$this->dbName."/_design/".$designDocName."/_view/".$viewName); |
|
| 1095 | + } else { |
|
| 1061 | 1096 | $request = new Request(Request::POST_METHOD, "/".$this->dbName."/_design/".$designDocName."/_view/".$viewName); |
| 1062 | 1097 | $request->setBody(json_encode(['keys' => $keys])); |
| 1063 | 1098 | } |
@@ -1065,14 +1100,15 @@ discard block |
||
| 1065 | 1100 | if (isset($opts)) { |
| 1066 | 1101 | $request->setMultipleQueryParamsAtOnce($opts->asArray()); |
| 1067 | 1102 | $includeMissingKeys = $opts->issetIncludeMissingKeys(); |
| 1103 | + } else { |
|
| 1104 | + $includeMissingKeys = FALSE; |
|
| 1068 | 1105 | } |
| 1069 | - else |
|
| 1070 | - $includeMissingKeys = FALSE; |
|
| 1071 | 1106 | |
| 1072 | 1107 | $result = $this->send($request, $chunkHook)->getBodyAsArray(); |
| 1073 | 1108 | |
| 1074 | - if ($includeMissingKeys) |
|
| 1075 | - $this->addMissingRows($keys, $result['rows']); |
|
| 1109 | + if ($includeMissingKeys) { |
|
| 1110 | + $this->addMissingRows($keys, $result['rows']); |
|
| 1111 | + } |
|
| 1076 | 1112 | |
| 1077 | 1113 | return new Result\QueryResult($result); |
| 1078 | 1114 | } |
@@ -1100,30 +1136,33 @@ discard block |
||
| 1100 | 1136 | $handler = new Handler\ViewHandler('temp'); |
| 1101 | 1137 | $handler->language = $language; |
| 1102 | 1138 | $handler->mapFn = $mapFn; |
| 1103 | - if (!empty($reduce)) |
|
| 1104 | - $handler->reduceFn = $reduceFn; |
|
| 1139 | + if (!empty($reduce)) { |
|
| 1140 | + $handler->reduceFn = $reduceFn; |
|
| 1141 | + } |
|
| 1105 | 1142 | |
| 1106 | 1143 | $request = new Request(Request::POST_METHOD, "/".$this->dbName."/_temp_view"); |
| 1107 | 1144 | $request->setHeaderField(Request::CONTENT_TYPE_HF, "application/json"); |
| 1108 | 1145 | |
| 1109 | 1146 | $array = $handler->asArray(); |
| 1110 | 1147 | |
| 1111 | - if (!empty($keys)) |
|
| 1112 | - $array['keys'] = $keys; |
|
| 1148 | + if (!empty($keys)) { |
|
| 1149 | + $array['keys'] = $keys; |
|
| 1150 | + } |
|
| 1113 | 1151 | |
| 1114 | 1152 | $request->setBody(json_encode($array)); |
| 1115 | 1153 | |
| 1116 | 1154 | if (isset($opts)) { |
| 1117 | 1155 | $request->setMultipleQueryParamsAtOnce($opts->asArray()); |
| 1118 | 1156 | $includeMissingKeys = $opts->issetIncludeMissingKeys(); |
| 1157 | + } else { |
|
| 1158 | + $includeMissingKeys = FALSE; |
|
| 1119 | 1159 | } |
| 1120 | - else |
|
| 1121 | - $includeMissingKeys = FALSE; |
|
| 1122 | 1160 | |
| 1123 | 1161 | $result = $this->send($request, $chunkHook)->getBodyAsArray(); |
| 1124 | 1162 | |
| 1125 | - if ($includeMissingKeys) |
|
| 1126 | - $this->addMissingRows($keys, $result['rows']); |
|
| 1163 | + if ($includeMissingKeys) { |
|
| 1164 | + $this->addMissingRows($keys, $result['rows']); |
|
| 1165 | + } |
|
| 1127 | 1166 | |
| 1128 | 1167 | return new Result\QueryResult($result); |
| 1129 | 1168 | } |
@@ -1186,8 +1225,9 @@ discard block |
||
| 1186 | 1225 | public function setRevsLimit($revsLimit = self::REVS_LIMIT) { |
| 1187 | 1226 | $this->checkForDb(); |
| 1188 | 1227 | |
| 1189 | - if (!is_int($revsLimit) or ($revsLimit <= 0)) |
|
| 1190 | - throw new \InvalidArgumentException("\$revsLimit must be a positive integer."); |
|
| 1228 | + if (!is_int($revsLimit) or ($revsLimit <= 0)) { |
|
| 1229 | + throw new \InvalidArgumentException("\$revsLimit must be a positive integer."); |
|
| 1230 | + } |
|
| 1191 | 1231 | |
| 1192 | 1232 | $request = new Request(Request::PUT_METHOD, "/".$this->dbName."/_revs_limit"); |
| 1193 | 1233 | $request->setHeaderField(Request::CONTENT_TYPE_HF, "application/json"); |
@@ -1245,16 +1285,17 @@ discard block |
||
| 1245 | 1285 | $request = new Request(Request::GET_METHOD, $requestPath); |
| 1246 | 1286 | |
| 1247 | 1287 | // Retrieves the specific revision of the document. |
| 1248 | - if (!empty($rev)) |
|
| 1249 | - $request->setQueryParam("rev", (string)$rev); |
|
| 1288 | + if (!empty($rev)) { |
|
| 1289 | + $request->setQueryParam("rev", (string)$rev); |
|
| 1290 | + } |
|
| 1250 | 1291 | |
| 1251 | 1292 | // If there are any options, add them to the request. |
| 1252 | 1293 | if (isset($opts)) { |
| 1253 | 1294 | $request->setMultipleQueryParamsAtOnce($opts->asArray()); |
| 1254 | 1295 | $ignoreClass = $opts->issetIgnoreClass(); |
| 1296 | + } else { |
|
| 1297 | + $ignoreClass = FALSE; |
|
| 1255 | 1298 | } |
| 1256 | - else |
|
| 1257 | - $ignoreClass = FALSE; |
|
| 1258 | 1299 | |
| 1259 | 1300 | $response = $this->send($request); |
| 1260 | 1301 | $body = $response->getBodyAsArray(); |
@@ -1266,18 +1307,18 @@ discard block |
||
| 1266 | 1307 | if (!$ignoreClass && isset($body['class'])) { // Special document class inherited from Doc or LocalDoc. |
| 1267 | 1308 | $class = "\\".$body['class']; |
| 1268 | 1309 | $doc = new $class; |
| 1310 | + } elseif ($path == self::DESIGN_DOC_PATH) { |
|
| 1311 | + $doc = new Doc\DesignDoc; |
|
| 1312 | + } else { |
|
| 1313 | + $doc = NULL; |
|
| 1269 | 1314 | } |
| 1270 | - elseif ($path == self::DESIGN_DOC_PATH) |
|
| 1271 | - $doc = new Doc\DesignDoc; |
|
| 1272 | - else |
|
| 1273 | - $doc = NULL; |
|
| 1274 | 1315 | |
| 1275 | 1316 | if (is_object($doc)) { |
| 1276 | 1317 | $doc->assignArray($body); |
| 1277 | 1318 | return $doc; |
| 1319 | + } else { |
|
| 1320 | + return $response; |
|
| 1278 | 1321 | } |
| 1279 | - else |
|
| 1280 | - return $response; |
|
| 1281 | 1322 | } |
| 1282 | 1323 | |
| 1283 | 1324 | |
@@ -1304,8 +1345,9 @@ discard block |
||
| 1304 | 1345 | |
| 1305 | 1346 | // Whether the document has an id we use a different HTTP method. Using POST CouchDB generates an id for the doc |
| 1306 | 1347 | // using PUT we need to specify one. We can still use the function getUuids() to ask CouchDB for some ids. |
| 1307 | - if (!$doc->issetId()) |
|
| 1308 | - $doc->setId(Generator\UUID::generate(Generator\UUID::UUID_RANDOM, Generator\UUID::FMT_STRING)); |
|
| 1348 | + if (!$doc->issetId()) { |
|
| 1349 | + $doc->setId(Generator\UUID::generate(Generator\UUID::UUID_RANDOM, Generator\UUID::FMT_STRING)); |
|
| 1350 | + } |
|
| 1309 | 1351 | |
| 1310 | 1352 | $this->setDocInfo($doc); |
| 1311 | 1353 | |
@@ -1319,8 +1361,9 @@ discard block |
||
| 1319 | 1361 | $request->setBody($doc->asJson()); |
| 1320 | 1362 | |
| 1321 | 1363 | // Enables batch mode. |
| 1322 | - if ($batchMode) |
|
| 1323 | - $request->setQueryParam("batch", "ok"); |
|
| 1364 | + if ($batchMode) { |
|
| 1365 | + $request->setQueryParam("batch", "ok"); |
|
| 1366 | + } |
|
| 1324 | 1367 | |
| 1325 | 1368 | $this->send($request); |
| 1326 | 1369 | } |
@@ -1373,10 +1416,11 @@ discard block |
||
| 1373 | 1416 | // This request uses the special method COPY. |
| 1374 | 1417 | $request = new Request(Request::COPY_METHOD, $path); |
| 1375 | 1418 | |
| 1376 | - if (empty($rev)) |
|
| 1377 | - $request->setHeaderField(Request::DESTINATION_HF, $targetDocId); |
|
| 1378 | - else |
|
| 1379 | - $request->setHeaderField(Request::DESTINATION_HF, $targetDocId."?rev=".(string)$rev); |
|
| 1419 | + if (empty($rev)) { |
|
| 1420 | + $request->setHeaderField(Request::DESTINATION_HF, $targetDocId); |
|
| 1421 | + } else { |
|
| 1422 | + $request->setHeaderField(Request::DESTINATION_HF, $targetDocId."?rev=".(string)$rev); |
|
| 1423 | + } |
|
| 1380 | 1424 | |
| 1381 | 1425 | $this->send($request); |
| 1382 | 1426 | } |
@@ -1406,8 +1450,9 @@ discard block |
||
| 1406 | 1450 | $request = new Request(Request::POST_METHOD, $path); |
| 1407 | 1451 | |
| 1408 | 1452 | $purge = []; |
| 1409 | - foreach ($refs as $ref) |
|
| 1410 | - $purge[] = $ref->asArray(); |
|
| 1453 | + foreach ($refs as $ref) { |
|
| 1454 | + $purge[] = $ref->asArray(); |
|
| 1455 | + } |
|
| 1411 | 1456 | |
| 1412 | 1457 | $request->setBody(json_encode($purge)); |
| 1413 | 1458 | |
@@ -1440,26 +1485,30 @@ discard block |
||
| 1440 | 1485 | public function performBulkOperations(array $docs, $fullCommit = FALSE, $allOrNothing = FALSE, $newEdits = TRUE) { |
| 1441 | 1486 | $this->checkForDb(); |
| 1442 | 1487 | |
| 1443 | - if (count($docs) == 0) |
|
| 1444 | - throw new \InvalidArgumentException("The \$docs array cannot be empty."); |
|
| 1445 | - else |
|
| 1446 | - $operations = []; |
|
| 1488 | + if (count($docs) == 0) { |
|
| 1489 | + throw new \InvalidArgumentException("The \$docs array cannot be empty."); |
|
| 1490 | + } else { |
|
| 1491 | + $operations = []; |
|
| 1492 | + } |
|
| 1447 | 1493 | |
| 1448 | 1494 | $path = "/".$this->dbName."/_bulk_docs"; |
| 1449 | 1495 | |
| 1450 | 1496 | $request = new Request(Request::POST_METHOD, $path); |
| 1451 | 1497 | $request->setHeaderField(Request::CONTENT_TYPE_HF, "application/json"); |
| 1452 | 1498 | |
| 1453 | - if ($fullCommit) |
|
| 1454 | - $request->setHeaderField(Request::X_COUCHDB_FULL_COMMIT_HF, "full_commit"); |
|
| 1455 | - else |
|
| 1456 | - $request->setHeaderField(Request::X_COUCHDB_FULL_COMMIT_HF, "delay_commit"); |
|
| 1499 | + if ($fullCommit) { |
|
| 1500 | + $request->setHeaderField(Request::X_COUCHDB_FULL_COMMIT_HF, "full_commit"); |
|
| 1501 | + } else { |
|
| 1502 | + $request->setHeaderField(Request::X_COUCHDB_FULL_COMMIT_HF, "delay_commit"); |
|
| 1503 | + } |
|
| 1457 | 1504 | |
| 1458 | - if ($allOrNothing) |
|
| 1459 | - $operations['all_or_nothing'] = 'true'; |
|
| 1505 | + if ($allOrNothing) { |
|
| 1506 | + $operations['all_or_nothing'] = 'true'; |
|
| 1507 | + } |
|
| 1460 | 1508 | |
| 1461 | - if (!$newEdits) |
|
| 1462 | - $operations['new_edits'] = 'false'; |
|
| 1509 | + if (!$newEdits) { |
|
| 1510 | + $operations['new_edits'] = 'false'; |
|
| 1511 | + } |
|
| 1463 | 1512 | |
| 1464 | 1513 | foreach ($docs as $doc) { |
| 1465 | 1514 | $this->setDocInfo($doc); |
@@ -1497,8 +1546,9 @@ discard block |
||
| 1497 | 1546 | $request = new Request(Request::HEAD_METHOD, $path); |
| 1498 | 1547 | |
| 1499 | 1548 | // In case we want retrieve a specific document revision. |
| 1500 | - if (!empty($rev)) |
|
| 1501 | - $request->setQueryParam("rev", (string)$rev); |
|
| 1549 | + if (!empty($rev)) { |
|
| 1550 | + $request->setQueryParam("rev", (string)$rev); |
|
| 1551 | + } |
|
| 1502 | 1552 | |
| 1503 | 1553 | return $this->send($request); |
| 1504 | 1554 | } |
@@ -1524,8 +1574,9 @@ discard block |
||
| 1524 | 1574 | $request = new Request(Request::GET_METHOD, $path); |
| 1525 | 1575 | |
| 1526 | 1576 | // In case we want retrieve a specific document revision. |
| 1527 | - if (!empty($rev)) |
|
| 1528 | - $request->setQueryParam("rev", (string)$rev); |
|
| 1577 | + if (!empty($rev)) { |
|
| 1578 | + $request->setQueryParam("rev", (string)$rev); |
|
| 1579 | + } |
|
| 1529 | 1580 | |
| 1530 | 1581 | return $this->send($request)->getBody(); |
| 1531 | 1582 | } |
@@ -1554,8 +1605,9 @@ discard block |
||
| 1554 | 1605 | $request->setBody(base64_encode($attachment->getData())); |
| 1555 | 1606 | |
| 1556 | 1607 | // In case of adding or updating an existence document. |
| 1557 | - if (!empty($rev)) |
|
| 1558 | - $request->setQueryParam("rev", (string)$rev); |
|
| 1608 | + if (!empty($rev)) { |
|
| 1609 | + $request->setQueryParam("rev", (string)$rev); |
|
| 1610 | + } |
|
| 1559 | 1611 | |
| 1560 | 1612 | return $this->send($request); |
| 1561 | 1613 | } |