Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Json / Server / Response.php
blobe2000a8c3ecd326d24df1325d6496a11e61ea84b
1 <?php
2 /**
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
8 */
10 namespace Zend\Json\Server;
12 use Zend\Json\Json;
14 class Response
16 /**
17 * Response error
18 * @var null|Error
20 protected $error;
22 /**
23 * Request ID
24 * @var mixed
26 protected $id;
28 /**
29 * Result
30 * @var mixed
32 protected $result;
34 /**
35 * Service map
36 * @var Smd
38 protected $serviceMap;
40 /**
41 * JSON-RPC version
42 * @var string
44 protected $version;
46 /**
47 * @var $args
49 protected $args;
51 /**
52 * Set response state
54 * @param array $options
55 * @return Response
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);
74 return $this;
77 /**
78 * Set response state based on JSON
80 * @param string $json
81 * @return void
83 public function loadJson($json)
85 $options = Json::decode($json, Json::TYPE_ARRAY);
86 $this->setOptions($options);
89 /**
90 * Set result
92 * @param mixed $value
93 * @return Response
95 public function setResult($value)
97 $this->result = $value;
98 return $this;
102 * Get result
104 * @return mixed
106 public function getResult()
108 return $this->result;
111 // RPC error, if response results in fault
113 * Set result error
115 * @param mixed $error
116 * @return Response
118 public function setError(Error $error = null)
120 $this->error = $error;
121 return $this;
125 * Get response error
127 * @return null|Error
129 public function getError()
131 return $this->error;
135 * Is the response an error?
137 * @return bool
139 public function isError()
141 return $this->getError() instanceof Error;
145 * Set request ID
147 * @param mixed $name
148 * @return Response
150 public function setId($name)
152 $this->id = $name;
153 return $this;
157 * Get request ID
159 * @return mixed
161 public function getId()
163 return $this->id;
167 * Set JSON-RPC version
169 * @param string $version
170 * @return Response
172 public function setVersion($version)
174 $version = (string) $version;
175 if ('2.0' == $version) {
176 $this->version = '2.0';
177 } else {
178 $this->version = null;
181 return $this;
185 * Retrieve JSON-RPC version
187 * @return string
189 public function getVersion()
191 return $this->version;
195 * Cast to JSON
197 * @return string
199 public function toJson()
201 if ($this->isError()) {
202 $response = array(
203 'error' => $this->getError()->toArray(),
204 'id' => $this->getId(),
206 } else {
207 $response = array(
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);
221 * Retrieve args
223 * @return mixed
225 public function getArgs()
227 return $this->args;
231 * Set args
233 * @param mixed $args
234 * @return self
236 public function setArgs($args)
238 $this->args = $args;
239 return $this;
243 * Set service map object
245 * @param Smd $serviceMap
246 * @return Response
248 public function setServiceMap($serviceMap)
250 $this->serviceMap = $serviceMap;
251 return $this;
255 * Retrieve service map
257 * @return Smd|null
259 public function getServiceMap()
261 return $this->serviceMap;
265 * Cast to string (JSON)
267 * @return string
269 public function __toString()
271 return $this->toJson();