@@ -93,7 +93,16 @@ public function get($collectionId, $documentId, array $options = array())
93
93
*/
94
94
public function getById ($ collectionId , $ documentId , array $ options = array ())
95
95
{
96
- $ url = UrlHelper::buildUrl (Urls::URL_DOCUMENT , array ($ collectionId , $ documentId ));
96
+ $ data = $ this ->getDocument (Urls::URL_DOCUMENT , $ collectionId , $ documentId , $ options );
97
+ $ options ['_isNew ' ] = false ;
98
+
99
+ return $ this ->createFromArrayWithContext ($ data , $ options );
100
+ }
101
+
102
+
103
+ protected function getDocument ($ url , $ collectionId , $ documentId , array $ options = array ())
104
+ {
105
+ $ url = UrlHelper::buildUrl ($ url , array ($ collectionId , $ documentId ));
97
106
$ headerElements = array ();
98
107
if (array_key_exists ("ifMatch " , $ options ) && array_key_exists ("revision " , $ options )) {
99
108
if ($ options ["ifMatch " ] === true ) {
@@ -109,11 +118,7 @@ public function getById($collectionId, $documentId, array $options = array())
109
118
throw new ClientException ('Document has not changed. ' );
110
119
}
111
120
112
- $ data = $ response ->getJson ();
113
-
114
- $ options ['_isNew ' ] = false ;
115
-
116
- return $ this ->createFromArrayWithContext ($ data , $ options );
121
+ return $ response ->getJson ();
117
122
}
118
123
119
124
@@ -125,16 +130,21 @@ public function getById($collectionId, $documentId, array $options = array())
125
130
*
126
131
* @throws Exception
127
132
*
128
- * @param mixed $collectionId - collection id as a string or number
129
- * @param mixed $documentId - document identifier
130
- * @param ifMatch' - boolean if given revision should match or not</li>
131
- * @param revision' - The document is returned if it matches/not matches revision.</li>
133
+ * @param mixed $collectionId - collection id as a string or number.
134
+ * @param mixed $documentId - document identifier.
135
+ * @param ifMatch - boolean if given revision should match or not.
136
+ * @param revision - The document is returned if it matches/not matches revision.
132
137
*
133
138
* @return array - an array containing the complete header including the key httpCode.
134
139
*/
135
140
public function getHead ($ collectionId , $ documentId , $ revision = null , $ ifMatch = null )
136
141
{
137
- $ url = UrlHelper::buildUrl (Urls::URL_DOCUMENT , array ($ collectionId , $ documentId ));
142
+ return $ this ->head (Urls::URL_DOCUMENT , $ collectionId , $ documentId , $ revision , $ ifMatch );
143
+ }
144
+
145
+
146
+ protected function head ($ url , $ collectionId , $ documentId , $ revision = null , $ ifMatch = null ) {
147
+ $ url = UrlHelper::buildUrl ($ url , array ($ collectionId , $ documentId ));
138
148
$ headerElements = array ();
139
149
if ($ revision != null && $ ifMatch !== null ) {
140
150
if ($ ifMatch ) {
@@ -427,29 +437,36 @@ public function update(Document $document, $options = array())
427
437
* @return bool - always true, will throw if there is an error
428
438
*/
429
439
public function updateById ($ collectionId , $ documentId , Document $ document , $ options = array ())
440
+ {
441
+ return $ this ->patch (Urls::URL_DOCUMENT , $ collectionId , $ documentId , $ document , $ options );
442
+ }
443
+
444
+
445
+
446
+ protected function patch ($ url , $ collectionId , $ documentId , Document $ document , $ options = array ())
430
447
{
431
448
// This preserves compatibility for the old policy parameter.
432
449
$ params = array ();
433
450
$ params = $ this ->validateAndIncludeOldSingleParameterInParams (
434
- $ options ,
435
- $ params ,
436
- ConnectionOptions::OPTION_UPDATE_POLICY
451
+ $ options ,
452
+ $ params ,
453
+ ConnectionOptions::OPTION_UPDATE_POLICY
437
454
);
438
455
$ params = $ this ->includeOptionsInParams (
439
- $ options ,
440
- $ params ,
441
- array (
442
- 'waitForSync ' => $ this ->getConnectionOption (ConnectionOptions::OPTION_WAIT_SYNC ),
443
- 'keepNull ' => true ,
444
- )
456
+ $ options ,
457
+ $ params ,
458
+ array (
459
+ 'waitForSync ' => $ this ->getConnectionOption (ConnectionOptions::OPTION_WAIT_SYNC ),
460
+ 'keepNull ' => true ,
461
+ )
445
462
);
446
463
447
464
$ revision = $ document ->getRevision ();
448
465
if (!is_null ($ revision )) {
449
466
$ params [ConnectionOptions::OPTION_REVISION ] = $ revision ;
450
467
}
451
468
452
- $ url = UrlHelper::buildUrl (Urls:: URL_DOCUMENT , array ($ collectionId , $ documentId ));
469
+ $ url = UrlHelper::buildUrl ($ url , array ($ collectionId , $ documentId ));
453
470
$ url = UrlHelper::appendParamsUrl ($ url , $ params );
454
471
$ result = $ this ->getConnection ()->patch ($ url , $ this ->json_encode_wrapper ($ document ->getAll ()));
455
472
$ json = $ result ->getJson ();
@@ -515,18 +532,23 @@ public function replace(Document $document, $options = array())
515
532
* @return bool - always true, will throw if there is an error
516
533
*/
517
534
public function replaceById ($ collectionId , $ documentId , Document $ document , $ options = array ())
535
+ {
536
+ return $ this ->put (Urls::URL_DOCUMENT , $ collectionId , $ documentId , $ document , $ options );
537
+ }
538
+
539
+ protected function put ($ url , $ collectionId , $ documentId , Document $ document , $ options = array ())
518
540
{
519
541
// This preserves compatibility for the old policy parameter.
520
542
$ params = array ();
521
543
$ params = $ this ->validateAndIncludeOldSingleParameterInParams (
522
- $ options ,
523
- $ params ,
524
- ConnectionOptions::OPTION_REPLACE_POLICY
544
+ $ options ,
545
+ $ params ,
546
+ ConnectionOptions::OPTION_REPLACE_POLICY
525
547
);
526
548
$ params = $ this ->includeOptionsInParams (
527
- $ options ,
528
- $ params ,
529
- array ('waitForSync ' => ConnectionOptions::OPTION_WAIT_SYNC )
549
+ $ options ,
550
+ $ params ,
551
+ array ('waitForSync ' => ConnectionOptions::OPTION_WAIT_SYNC )
530
552
);
531
553
532
554
$ revision = $ document ->getRevision ();
@@ -535,7 +557,7 @@ public function replaceById($collectionId, $documentId, Document $document, $opt
535
557
}
536
558
537
559
$ data = $ document ->getAll ();
538
- $ url = UrlHelper::buildUrl (Urls:: URL_DOCUMENT , array ($ collectionId , $ documentId ));
560
+ $ url = UrlHelper::buildUrl ($ url , array ($ collectionId , $ documentId ));
539
561
$ url = UrlHelper::appendParamsUrl ($ url , $ params );
540
562
$ result = $ this ->getConnection ()->put ($ url , $ this ->json_encode_wrapper ($ data ));
541
563
$ json = $ result ->getJson ();
@@ -544,7 +566,6 @@ public function replaceById($collectionId, $documentId, Document $document, $opt
544
566
return true ;
545
567
}
546
568
547
-
548
569
/**
549
570
* Delete a document from a collection, identified by the document itself
550
571
*
@@ -636,25 +657,31 @@ public function deleteById($collectionId, $documentId, $revision = null, $option
636
657
* @return bool - always true, will throw if there is an error
637
658
*/
638
659
public function removeById ($ collectionId , $ documentId , $ revision = null , $ options = array ())
660
+ {
661
+ return $ this ->erase (Urls::URL_DOCUMENT , $ collectionId , $ documentId , $ revision , $ options );
662
+ }
663
+
664
+
665
+ protected function erase ($ url , $ collectionId , $ documentId , $ revision = null , $ options = array ())
639
666
{
640
667
// This preserves compatibility for the old policy parameter.
641
668
$ params = array ();
642
669
$ params = $ this ->validateAndIncludeOldSingleParameterInParams (
643
- $ options ,
644
- $ params ,
645
- ConnectionOptions::OPTION_DELETE_POLICY
670
+ $ options ,
671
+ $ params ,
672
+ ConnectionOptions::OPTION_DELETE_POLICY
646
673
);
647
674
$ params = $ this ->includeOptionsInParams (
648
- $ options ,
649
- $ params ,
650
- array ('waitForSync ' => ConnectionOptions::OPTION_WAIT_SYNC )
675
+ $ options ,
676
+ $ params ,
677
+ array ('waitForSync ' => ConnectionOptions::OPTION_WAIT_SYNC )
651
678
);
652
679
653
680
if (!is_null ($ revision )) {
654
681
$ params [ConnectionOptions::OPTION_REVISION ] = $ revision ;
655
682
}
656
683
657
- $ url = UrlHelper::buildUrl (Urls:: URL_DOCUMENT , array ($ collectionId , $ documentId ));
684
+ $ url = UrlHelper::buildUrl ($ url , array ($ collectionId , $ documentId ));
658
685
$ url = UrlHelper::appendParamsUrl ($ url , $ params );
659
686
$ this ->getConnection ()->delete ($ url );
660
687
0 commit comments