/** * Get the document returned in the body (JSON decoded) * * @return mixed */ public function getDocument() { if (!$this->document) { require_once 'Sopha/Json.php'; $this->document = Sopha_Json::decode($this->body); } return $this->document; }
/** * Return the document at the specified offset according to the return type * * @param integer $offset * @return mixed */ protected function returnDoc($offset) { $ret = null; if (isset($this->rows[$offset])) { switch ($this->return_type) { case self::RETURN_ARRAY: $ret = $this->rows[$offset]['value']; break; case self::RETURN_JSON: $ret = Sopha_Json::encode($this->rows[$offset]['value']); break; case self::RETURN_OBJECT: $ret = new $this->doc_class($this->rows[$offset]['value']); break; } } else { throw new OutOfBoundsException('Pointer points to invalid index'); } return $ret; }
/** * Call an ad-hoc view function * * @param array $view View function code to call * @param array $params Parameters to pass to the view * @param mixed $return_doc Type of document to return * @return Sopha_View_Result */ public function adHocView($view, array $params = array(), $return_doc = null) { require_once 'Sopha/Json.php'; $url = $this->_db_uri . '_temp_view'; $data = Sopha_Json::encode($view); $request = new Sopha_Http_Request($url, Sopha_Http_Request::POST, $data); foreach ($params as $k => $v) { $request->addQueryParam($k, Sopha_Json::encode($v)); } $response = $request->send(); switch ($response->getStatus()) { case 200: require_once 'Sopha/View/Result.php'; if (!$return_doc) { $return_doc = Sopha_View_Result::RETURN_ARRAY; } return new Sopha_View_Result($response->getDocument(), $return_doc); break; default: require_once 'Sopha/Db/Exception.php'; throw new Sopha_Db_Exception("Unexpected response from server: " . "{$response->getStatus()} {$response->getMessage()}", $response->getStatus()); break; } }
/** * Convert the document to a string - will return a JSON encoded object * * @return string */ public function __toString() { require_once 'Sopha/Json.php'; return Sopha_Json::encode(array_merge($this->_metadata, $this->_data)); }
/** * Test encoding and decoding in a single step * @param array $values array of values to test against encode/decode */ protected function _testJson($values) { $encoded = Sopha_Json::encode($values); $this->assertEquals($values, Sopha_Json::decode($encoded)); }