3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Json\Server
;
38 protected $serviceMap;
54 * @param array $options
57 public function setOptions(array $options)
59 // re-produce error state
60 if (isset($options['error']) && is_array($options['error'])) {
61 $error = $options['error'];
62 $options['error'] = new Error($error['message'], $error['code'], $error['data']);
65 $methods = get_class_methods($this);
66 foreach ($options as $key => $value) {
67 $method = 'set' . ucfirst($key);
68 if (in_array($method, $methods)) {
69 $this->$method($value);
70 } elseif ($key == 'jsonrpc') {
71 $this->setVersion($value);
78 * Set response state based on JSON
83 public function loadJson($json)
85 $options = Json
::decode($json, Json
::TYPE_ARRAY
);
86 $this->setOptions($options);
95 public function setResult($value)
97 $this->result
= $value;
106 public function getResult()
108 return $this->result
;
111 // RPC error, if response results in fault
115 * @param mixed $error
118 public function setError(Error
$error = null)
120 $this->error
= $error;
129 public function getError()
135 * Is the response an error?
139 public function isError()
141 return $this->getError() instanceof Error
;
150 public function setId($name)
161 public function getId()
167 * Set JSON-RPC version
169 * @param string $version
172 public function setVersion($version)
174 $version = (string) $version;
175 if ('2.0' == $version) {
176 $this->version
= '2.0';
178 $this->version
= null;
185 * Retrieve JSON-RPC version
189 public function getVersion()
191 return $this->version
;
199 public function toJson()
201 if ($this->isError()) {
203 'error' => $this->getError()->toArray(),
204 'id' => $this->getId(),
208 'result' => $this->getResult(),
209 'id' => $this->getId(),
213 if (null !== ($version = $this->getVersion())) {
214 $response['jsonrpc'] = $version;
217 return \Zend\Json\Json
::encode($response);
225 public function getArgs()
236 public function setArgs($args)
243 * Set service map object
245 * @param Smd $serviceMap
248 public function setServiceMap($serviceMap)
250 $this->serviceMap
= $serviceMap;
255 * Retrieve service map
259 public function getServiceMap()
261 return $this->serviceMap
;
265 * Cast to string (JSON)
269 public function __toString()
271 return $this->toJson();