Skip to content

Commit 3dcf5ff

Browse files
committed
made two explicit methods for id fetching
1 parent e4e9107 commit 3dcf5ff

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

lib/triagens/Avocado/DocumentHandler.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ public function add($collectionId, Document $document, $create = NULL) {
154154
* @return bool - always true, will throw if there is an error
155155
*/
156156
public function update($collectionId, Document $document, $policy = NULL) {
157-
$documentId = $this->getDocumentId($document);
157+
$collectionId = $this->getCollectionId($document);
158+
$documentId = $this->getDocumentId($document);
158159

159160
if ($policy === NULL) {
160161
$policy = $this->getConnection()->getOption(ConnectionOptions::OPTION_UPDATE_POLICY);
@@ -193,10 +194,10 @@ public function delete($collectionId, $document) {
193194
* @param mixed $document - document id OR document to be updated
194195
* @return mixed - document id, will throw if there is an error
195196
*/
196-
197197
private function getDocumentId($document) {
198198
if ($document instanceof Document) {
199-
$documentId = $document->getId();
199+
$id = $document->getId();
200+
list(, $documentId) = explode('/', $document->getId(), 2);
200201
}
201202
else {
202203
$documentId = $document;
@@ -208,5 +209,24 @@ private function getDocumentId($document) {
208209

209210
return $documentId;
210211
}
212+
213+
/**
214+
* Helper function to get a collection id from a document
215+
*
216+
* @throws ClientException
217+
* @param Document $document - document id
218+
* @return mixed - collection id, will throw if there is an error
219+
*/
220+
private function getCollectionId(Document $document) {
221+
$id = $document->getId();
222+
223+
list($collectionId,) = explode('/', $id, 2);
224+
225+
if (!$collectionId || !(is_string($collectionId) || is_double($collectionId) || is_int($collectionId))) {
226+
throw new ClientException('Cannot alter a document without a document id');
227+
}
228+
229+
return $collectionId;
230+
}
211231

212232
}

0 commit comments

Comments
 (0)