예제 #1
0
 /**
  * @param Document $document
  * @param array $options
  * @return array
  */
 protected function buildRequestOptions($options, Document $document = null)
 {
     $requestOptions = ['headers' => []];
     if (isset($document)) {
         $requestOptions['json'] = $document->getData();
     } elseif (isset($options['getAuthToken'])) {
         $requestOptions['json'] = $options['getAuthToken'];
     } elseif (isset($options['json'])) {
         $requestOptions['json'] = $options['json'];
     }
     if (isset($options['headers'])) {
         $requestOptions['headers'] += $options['headers'];
     }
     if (isset($options['setAuthToken'])) {
         $requestOptions['headers'] += ['X-CouchDB-WWW-Authenticate' => 'Cookie'];
         $requestOptions['cookies'] = $this->buildAuthCookie($options['setAuthToken']);
     }
     if (isset($options['user'])) {
         $requestOptions['auth'] = [$options['user']['username'], $options['user']['password']];
     }
     if ($this->debug) {
         $requestOptions['debug'] = true;
     }
     return $requestOptions;
 }
예제 #2
0
 /**
  * @param $id
  * @return Document
  * @throws CouchDbException
  */
 public function getDocumentById($id)
 {
     try {
         $response = $this->client->get($this->getDocumentUrl($id), 200, $this->server->getOptions());
         $document = new Document();
         $document->populateFromArray($response);
         return $document;
     } catch (CouchDbException $exception) {
         // If exception was a 404 the document was not found. So we can create one using $id
         if ($exception->getCode() == 404) {
             $document = new Document($id);
             return $document;
         } else {
             throw $exception;
         }
     }
 }