|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Json |
|
17 * @subpackage Server |
|
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 * @version $Id: Response.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
21 */ |
|
22 |
|
23 /** |
|
24 * @category Zend |
|
25 * @package Zend_Json |
|
26 * @subpackage Server |
|
27 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
28 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
29 */ |
|
30 class Zend_Json_Server_Response |
|
31 { |
|
32 /** |
|
33 * Response error |
|
34 * @var null|Zend_Json_Server_Error |
|
35 */ |
|
36 protected $_error; |
|
37 |
|
38 /** |
|
39 * Request ID |
|
40 * @var mixed |
|
41 */ |
|
42 protected $_id; |
|
43 |
|
44 /** |
|
45 * Result |
|
46 * @var mixed |
|
47 */ |
|
48 protected $_result; |
|
49 |
|
50 /** |
|
51 * Service map |
|
52 * @var Zend_Json_Server_Smd |
|
53 */ |
|
54 protected $_serviceMap; |
|
55 |
|
56 /** |
|
57 * JSON-RPC version |
|
58 * @var string |
|
59 */ |
|
60 protected $_version; |
|
61 |
|
62 /** |
|
63 * Set result |
|
64 * |
|
65 * @param mixed $value |
|
66 * @return Zend_Json_Server_Response |
|
67 */ |
|
68 public function setResult($value) |
|
69 { |
|
70 $this->_result = $value; |
|
71 return $this; |
|
72 } |
|
73 |
|
74 /** |
|
75 * Get result |
|
76 * |
|
77 * @return mixed |
|
78 */ |
|
79 public function getResult() |
|
80 { |
|
81 return $this->_result; |
|
82 } |
|
83 |
|
84 // RPC error, if response results in fault |
|
85 /** |
|
86 * Set result error |
|
87 * |
|
88 * @param Zend_Json_Server_Error $error |
|
89 * @return Zend_Json_Server_Response |
|
90 */ |
|
91 public function setError(Zend_Json_Server_Error $error) |
|
92 { |
|
93 $this->_error = $error; |
|
94 return $this; |
|
95 } |
|
96 |
|
97 /** |
|
98 * Get response error |
|
99 * |
|
100 * @return null|Zend_Json_Server_Error |
|
101 */ |
|
102 public function getError() |
|
103 { |
|
104 return $this->_error; |
|
105 } |
|
106 |
|
107 /** |
|
108 * Is the response an error? |
|
109 * |
|
110 * @return bool |
|
111 */ |
|
112 public function isError() |
|
113 { |
|
114 return $this->getError() instanceof Zend_Json_Server_Error; |
|
115 } |
|
116 |
|
117 /** |
|
118 * Set request ID |
|
119 * |
|
120 * @param mixed $name |
|
121 * @return Zend_Json_Server_Response |
|
122 */ |
|
123 public function setId($name) |
|
124 { |
|
125 $this->_id = $name; |
|
126 return $this; |
|
127 } |
|
128 |
|
129 /** |
|
130 * Get request ID |
|
131 * |
|
132 * @return mixed |
|
133 */ |
|
134 public function getId() |
|
135 { |
|
136 return $this->_id; |
|
137 } |
|
138 |
|
139 /** |
|
140 * Set JSON-RPC version |
|
141 * |
|
142 * @param string $version |
|
143 * @return Zend_Json_Server_Response |
|
144 */ |
|
145 public function setVersion($version) |
|
146 { |
|
147 $version = (string) $version; |
|
148 if ('2.0' == $version) { |
|
149 $this->_version = '2.0'; |
|
150 } else { |
|
151 $this->_version = null; |
|
152 } |
|
153 |
|
154 return $this; |
|
155 } |
|
156 |
|
157 /** |
|
158 * Retrieve JSON-RPC version |
|
159 * |
|
160 * @return string |
|
161 */ |
|
162 public function getVersion() |
|
163 { |
|
164 return $this->_version; |
|
165 } |
|
166 |
|
167 /** |
|
168 * Cast to JSON |
|
169 * |
|
170 * @return string |
|
171 */ |
|
172 public function toJson() |
|
173 { |
|
174 if ($this->isError()) { |
|
175 $response = array( |
|
176 'result' => null, |
|
177 'error' => $this->getError()->toArray(), |
|
178 'id' => $this->getId(), |
|
179 ); |
|
180 } else { |
|
181 $response = array( |
|
182 'result' => $this->getResult(), |
|
183 'id' => $this->getId(), |
|
184 'error' => null, |
|
185 ); |
|
186 } |
|
187 |
|
188 if (null !== ($version = $this->getVersion())) { |
|
189 $response['jsonrpc'] = $version; |
|
190 } |
|
191 |
|
192 require_once 'Zend/Json.php'; |
|
193 return Zend_Json::encode($response); |
|
194 } |
|
195 |
|
196 /** |
|
197 * Retrieve args |
|
198 * |
|
199 * @return mixed |
|
200 */ |
|
201 public function getArgs() |
|
202 { |
|
203 return $this->_args; |
|
204 } |
|
205 |
|
206 /** |
|
207 * Set args |
|
208 * |
|
209 * @param mixed $args |
|
210 * @return self |
|
211 */ |
|
212 public function setArgs($args) |
|
213 { |
|
214 $this->_args = $args; |
|
215 return $this; |
|
216 } |
|
217 |
|
218 /** |
|
219 * Set service map object |
|
220 * |
|
221 * @param Zend_Json_Server_Smd $serviceMap |
|
222 * @return Zend_Json_Server_Response |
|
223 */ |
|
224 public function setServiceMap($serviceMap) |
|
225 { |
|
226 $this->_serviceMap = $serviceMap; |
|
227 return $this; |
|
228 } |
|
229 |
|
230 /** |
|
231 * Retrieve service map |
|
232 * |
|
233 * @return Zend_Json_Server_Smd|null |
|
234 */ |
|
235 public function getServiceMap() |
|
236 { |
|
237 return $this->_serviceMap; |
|
238 } |
|
239 |
|
240 /** |
|
241 * Cast to string (JSON) |
|
242 * |
|
243 * @return string |
|
244 */ |
|
245 public function __toString() |
|
246 { |
|
247 return $this->toJson(); |
|
248 } |
|
249 } |
|
250 |