public function save($collection, $document, array $options = [])
    {
        $collection     = $this->makeCollection($collection);
        $_documentClass = $this->_documentClass;

        $params = $this->includeOptionsInParams(
            $options, [
                'waitForSync'      => null,
                'silent'           => false,
                'createCollection' => $this->getConnection()->getOption(ConnectionOptions::OPTION_CREATE),
                'overwrite'        => (bool) @$options[self::OPTION_OVERWRITE],
                'returnOld'        => (bool) @$options[self::OPTION_RETURN_OLD],
                'returnNew'        => (bool) @$options[self::OPTION_RETURN_NEW],
            ]
        );

        $this->createCollectionIfOptions($collection, $params);

        $url = UrlHelper::appendParamsUrl(Urls::URL_DOCUMENT . '/' . $collection, $params);

        if (is_array($document)) {
            $data = $document;
        } else {
            $data = $document->getAllForInsertUpdate();
        }

        $response = $this->getConnection()->post($url, $this->json_encode_wrapper($data));
        $json     = $response->getJson();

        // This makes sure that if we're in batch mode, it will not go further and choke on the checks below.
        // Caution: Instead of a document ID, we are returning the batchpart object
        // The Id of the BatchPart can be retrieved by calling getId() on it.
        // We're basically returning an object here, in order not to accidentally use the batch part id as the document id
        if ($batchPart = $response->getBatchPart()) {
            return $batchPart;
        }
                
        if (@$options[self::OPTION_RETURN_OLD] || @$options[self::OPTION_RETURN_NEW]) {
            return $json;
        }

        if (is_array($document)) {
            return $json[$_documentClass::ENTRY_KEY];
        }

        $location = $response->getLocationHeader();
        if (!$location) {
            throw new ClientException('Did not find location header in server response');
        }

        $id = UrlHelper::getDocumentIdFromLocation($location);

        $document->setInternalId($json[$_documentClass::ENTRY_ID]);
        $document->setRevision($json[$_documentClass::ENTRY_REV]);

        if ($id !== $document->getId()) {
            throw new ClientException('Got an invalid response from the server');
        }

        $document->setIsNew(false);

        return $document->getId();
    }
public function save($collection, $document, array $options = [])
    {
        $collection     = $this->makeCollection($collection);
        $_documentClass = $this->_documentClass;

        $params = $this->includeOptionsInParams(
            $options, [
                'waitForSync'      => null,
                'silent'           => false,
                'createCollection' => $this->getConnection()->getOption(ConnectionOptions::OPTION_CREATE),
                'overwrite'        => (bool) @$options[self::OPTION_OVERWRITE],
                'returnOld'        => (bool) @$options[self::OPTION_RETURN_OLD],
                'returnNew'        => (bool) @$options[self::OPTION_RETURN_NEW],
            ]
        );

        $this->createCollectionIfOptions($collection, $params);

        $url = UrlHelper::appendParamsUrl(Urls::URL_DOCUMENT . '/' . $collection, $params);

        if (is_array($document)) {
            $data = $document;
        } else {
            $data = $document->getAllForInsertUpdate();
        }

        $response = $this->getConnection()->post($url, $this->json_encode_wrapper($data));
        $json     = $response->getJson();

        // This makes sure that if we're in batch mode, it will not go further and choke on the checks below.
        // Caution: Instead of a document ID, we are returning the batchpart object
        // The Id of the BatchPart can be retrieved by calling getId() on it.
        // We're basically returning an object here, in order not to accidentally use the batch part id as the document id
        if ($batchPart = $response->getBatchPart()) {
            return $batchPart;
        }
                
        if (@$options[self::OPTION_RETURN_OLD] || @$options[self::OPTION_RETURN_NEW]) {
            return $json;
        }

        if (is_array($document)) {
            return $json[$_documentClass::ENTRY_KEY];
        }

        $location = $response->getLocationHeader();
        if (!$location) {
            throw new ClientException('Did not find location header in server response');
        }

        $id = UrlHelper::getDocumentIdFromLocation($location);

        $document->setInternalId($json[$_documentClass::ENTRY_ID]);
        $document->setRevision($json[$_documentClass::ENTRY_REV]);

        if ($id !== $document->getId()) {
            throw new ClientException('Got an invalid response from the server');
        }

        $document->setIsNew(false);

        return $document->getId();
    }
public function save ( $ collection , $ document , array $ options = [ ] ) { $ collection = $ this -> makeCollection ( $ collection ) ; $ _documentClass = $ this -> _documentClass ; $ params = $ this -> includeOptionsInParams ( $ options , [ 'waitForSync' => null , 'silent' => false , 'createCollection' => $ this -> getConnection ( ) -> getOption ( ConnectionOptions :: OPTION_CREATE ) , 'overwrite' => ( bool ) @ $ options [ self :: OPTION_OVERWRITE ] , 'returnOld' => ( bool ) @ $ options [ self :: OPTION_RETURN_OLD ] , 'returnNew' => ( bool ) @ $ options [ self :: OPTION_RETURN_NEW ] , ] ) ; $ this -> createCollectionIfOptions ( $ collection , $ params ) ; $ url = UrlHelper :: appendParamsUrl ( Urls :: URL_DOCUMENT . '/' . $ collection , $ params ) ; if ( is_array ( $ document ) ) { $ data = $ document ; } else { $ data = $ document -> getAllForInsertUpdate ( ) ; } $ response = $ this -> getConnection ( ) -> post ( $ url , $ this -> json_encode_wrapper ( $ data ) ) ; $ json = $ response -> getJson ( ) ; // This makes sure that if we're in batch mode, it will not go further and choke on the checks below. // Caution: Instead of a document ID, we are returning the batchpart object // The Id of the BatchPart can be retrieved by calling getId() on it. // We're basically returning an object here, in order not to accidentally use the batch part id as the document id if ( $ batchPart = $ response -> getBatchPart ( ) ) { return $ batchPart ; } if ( @ $ options [ self :: OPTION_RETURN_OLD ] || @ $ options [ self :: OPTION_RETURN_NEW ] ) { return $ json ; } if ( is_array ( $ document ) ) { return $ json [ $ _documentClass :: ENTRY_KEY ] ; } $ location = $ response -> getLocationHeader ( ) ; if ( ! $ location ) { throw new ClientException ( 'Did not find location header in server response' ) ; } $ id = UrlHelper :: getDocumentIdFromLocation ( $ location ) ; $ document -> setInternalId ( $ json [ $ _documentClass :: ENTRY_ID ] ) ; $ document -> setRevision ( $ json [ $ _documentClass :: ENTRY_REV ] ) ; if ( $ id !== $ document -> getId ( ) ) { throw new ClientException ( 'Got an invalid response from the server' ) ; } $ document -> setIsNew ( false ) ; return $ document -> getId ( ) ; }
save a document to a collection

This will add the document to the collection and return the document's id

This will throw if the document cannot be saved

@throws Exception

@param mixed          $collection - collection id as string or number
@param Document|array $document   - the document to be added, can be passed as a document or an array
@param array          $options    - optional, array of options
<p>Options are :<br>
<li>'createCollection' - create the collection if it does not yet exist.</li>
<li>'waitForSync' -  if set to true, then all removal operations will instantly be synchronised to disk / If this is not specified, then the collection's default sync behavior will be applied.</li>
<li>'overwrite' -  if set to true, will turn the insert into a replace operation if a document with the specified key already exists.</li>
<li>'returnNew' -  if set to true, then the newly created document will be returned.</li>
<li>'returnOld' -  if set to true, then the replaced document will be returned - useful only when using overwrite = true.</li>
</p>

@return mixed - id of document created
@since 1.0
save a document to a collection