/** * add * * @param array|Phly_Couch_Document $document * @return Phly_Couch_DocumentSet */ public function add($document) { if (is_array($document)) { require_once 'Phly/Couch/Document.php'; $document = new Phly_Couch_Document($document); } elseif (!$document instanceof Phly_Couch_Document) { require_once 'Phly/Couch/Exception.php'; throw new Phly_Couch_Exception('Invalid document provided'); } $id = $document->getId(); if (null === $id) { $this->_documents[] = $document; } else { $this->_documents[$id] = $document; } return $this; }
/** * Save a document * * @param string|array|Phly_Couch_Document $document * @param null|string $id * @param null|string $db * @return Phly_Couch_Result * @throws Phly_Couch_Exception on failure */ public function docSave($document, $id = null, $db = null) { $db = $this->_verifyDb($db); $path = $db . '/'; $method = 'POST'; if (null !== $id) { $method = 'PUT'; $path .= $id; } if (is_string($document)) { if ('{' != substr($document, 0, 1)) { require_once 'Phly/Couch/Exception.php'; throw new Phly_Couch_Exception('Invalid document provided'); } require_once 'Phly/Couch/Document.php'; $document = new Phly_Couch_Document($document); } elseif (is_array($document)) { require_once 'Phly/Couch/Document.php'; $document = new Phly_Couch_Document($document); } elseif (!$document instanceof Phly_Couch_Document) { require_once 'Phly/Couch/Exception.php'; throw new Phly_Couch_Exception('Invalid document provided'); } if (null !== $document->getRevision()) { if (null === $id && null === ($id = $document->getId())) { require_once 'Phly/Couch/Exception.php'; throw new Phly_Couch_Exception('Document updates require a document id; none provided'); } $method = 'PUT'; } $this->getHttpClient()->setRawData($document->toJson()); $response = $this->_prepareAndSend($path, $method); $status = $response->getStatus(); switch ($status) { case 412: require_once 'Phly/Couch/Exception.php'; throw new Phly_Couch_Exception(sprintf('Document with the specified document id "%s" already exists', $id)); break; case 409: require_once 'Phly/Couch/Exception.php'; throw new Phly_Couch_Exception(sprintf('Document with document id "%s" does not contain the revision "%s"', $id, $data['_rev'])); break; case 201: default: require_once 'Phly/Couch/Result.php'; return new Phly_Couch_Result($response); break; } }