示例#1
0
文件: Database.php 项目: h4cc/CouchDB
 /**
  * Return informations about the last changes from the database
  *
  * @return array
  *
  * @throws \RuntimeException If the request was not successfull
  */
 public function getChanges()
 {
     $response = $this->client->request('GET', "/{$this->name}/_changes");
     if (200 !== $response->getStatusCode()) {
         throw new Exception('Request wasn\'t successfull');
     }
     return JSONEncoder::decode((string) $response->getBody());
 }
示例#2
0
 /**
  * Execute the queue
  *
  * @return mixed
  */
 public function execute()
 {
     $response = $this->client->request('POST', "/{$this->database->getName()}/_bulk_docs", ['body' => JSONEncoder::encode($this->data), 'headers' => ['Content-Type' => 'application/json']]);
     return JSONEncoder::decode((string) $response->getBody());
 }
示例#3
0
 /**
  * Creates a new database.
  *
  * @param string $name The database name
  *
  * @return Database
  *
  * @throws Exception If the database could not be created.
  */
 public function createDatabase($name)
 {
     if (preg_match('@[^a-z0-9\\_\\$\\(\\)+\\-]@', $name)) {
         throw new InvalidDatabasenameException(sprintf('The database name %s is invalid. The database name must match the following pattern (a-z0-9_$()+-)', $name));
     }
     if ($this->eventManager->hasListeners(Events::PRE_CREATE_DATABASE)) {
         // @codeCoverageIgnoreStart
         $this->eventManager->dispatchEvent(Events::PRE_CREATE_DATABASE, new EventArgs($this, $name));
         // @codeCoverageIgnoreEnd
     }
     $response = $this->client->request('PUT', sprintf('/%s', $name));
     if (412 === $response->getStatusCode()) {
         throw new Exception(sprintf('The database "%s" already exist', $name));
     }
     $json = (string) $response->getBody();
     $value = JSONEncoder::decode($json);
     if (isset($value['error'])) {
         throw new Exception(sprintf('[%s] Failed to create database "%s". (%s)', $value['error'], $name, $value['reason']));
     }
     $database = $this->wrapDatabase($name);
     if ($this->eventManager->hasListeners(Events::POST_CREATE_DATABASE)) {
         // @codeCoverageIgnoreStart
         $this->eventManager->dispatchEvent(Events::POST_CREATE_DATABASE, new EventArgs($database));
         // @codeCoverageIgnoreEnd
     }
     return $database;
 }
示例#4
0
 public function testDecode()
 {
     $this->assertEquals(['foo' => 'bar'], JSONEncoder::decode('{"foo":"bar"}'));
 }