2
0

Response.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @category Zend
  24. * @package Zend_Json
  25. * @subpackage Server
  26. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class Zend_Json_Server_Response
  30. {
  31. /**
  32. * Response error
  33. * @var null|Zend_Json_Server_Error
  34. */
  35. protected $_error;
  36. /**
  37. * Request ID
  38. * @var mixed
  39. */
  40. protected $_id;
  41. /**
  42. * Result
  43. * @var mixed
  44. */
  45. protected $_result;
  46. /**
  47. * Service map
  48. * @var Zend_Json_Server_Smd
  49. */
  50. protected $_serviceMap;
  51. /**
  52. * JSON-RPC version
  53. * @var string
  54. */
  55. protected $_version;
  56. /**
  57. * Set result
  58. *
  59. * @param mixed $value
  60. * @return Zend_Json_Server_Response
  61. */
  62. public function setResult($value)
  63. {
  64. $this->_result = $value;
  65. return $this;
  66. }
  67. /**
  68. * Get result
  69. *
  70. * @return mixed
  71. */
  72. public function getResult()
  73. {
  74. return $this->_result;
  75. }
  76. // RPC error, if response results in fault
  77. /**
  78. * Set result error
  79. *
  80. * @param Zend_Json_Server_Error $error
  81. * @return Zend_Json_Server_Response
  82. */
  83. public function setError(Zend_Json_Server_Error $error)
  84. {
  85. $this->_error = $error;
  86. return $this;
  87. }
  88. /**
  89. * Get response error
  90. *
  91. * @return null|Zend_Json_Server_Error
  92. */
  93. public function getError()
  94. {
  95. return $this->_error;
  96. }
  97. /**
  98. * Is the response an error?
  99. *
  100. * @return bool
  101. */
  102. public function isError()
  103. {
  104. return $this->getError() instanceof Zend_Json_Server_Error;
  105. }
  106. /**
  107. * Set request ID
  108. *
  109. * @param mixed $name
  110. * @return Zend_Json_Server_Response
  111. */
  112. public function setId($name)
  113. {
  114. $this->_id = $name;
  115. return $this;
  116. }
  117. /**
  118. * Get request ID
  119. *
  120. * @return mixed
  121. */
  122. public function getId()
  123. {
  124. return $this->_id;
  125. }
  126. /**
  127. * Set JSON-RPC version
  128. *
  129. * @param string $version
  130. * @return Zend_Json_Server_Response
  131. */
  132. public function setVersion($version)
  133. {
  134. $version = (string) $version;
  135. if ('2.0' == $version) {
  136. $this->_version = '2.0';
  137. } else {
  138. $this->_version = null;
  139. }
  140. return $this;
  141. }
  142. /**
  143. * Retrieve JSON-RPC version
  144. *
  145. * @return string
  146. */
  147. public function getVersion()
  148. {
  149. return $this->_version;
  150. }
  151. /**
  152. * Cast to JSON
  153. *
  154. * @return string
  155. */
  156. public function toJson()
  157. {
  158. if ($this->isError()) {
  159. $response = array(
  160. 'error' => $this->getError()->toArray(),
  161. 'id' => $this->getId(),
  162. );
  163. } else {
  164. $response = array(
  165. 'result' => $this->getResult(),
  166. 'id' => $this->getId(),
  167. );
  168. }
  169. if (null !== ($version = $this->getVersion())) {
  170. $response['jsonrpc'] = $version;
  171. }
  172. require_once 'Zend/Json.php';
  173. return Zend_Json::encode($response);
  174. }
  175. /**
  176. * Retrieve args
  177. *
  178. * @return mixed
  179. */
  180. public function getArgs()
  181. {
  182. return $this->_args;
  183. }
  184. /**
  185. * Set args
  186. *
  187. * @param mixed $args
  188. * @return self
  189. */
  190. public function setArgs($args)
  191. {
  192. $this->_args = $args;
  193. return $this;
  194. }
  195. /**
  196. * Set service map object
  197. *
  198. * @param Zend_Json_Server_Smd $serviceMap
  199. * @return Zend_Json_Server_Response
  200. */
  201. public function setServiceMap($serviceMap)
  202. {
  203. $this->_serviceMap = $serviceMap;
  204. return $this;
  205. }
  206. /**
  207. * Retrieve service map
  208. *
  209. * @return Zend_Json_Server_Smd|null
  210. */
  211. public function getServiceMap()
  212. {
  213. return $this->_serviceMap;
  214. }
  215. /**
  216. * Cast to string (JSON)
  217. *
  218. * @return string
  219. */
  220. public function __toString()
  221. {
  222. return $this->toJson();
  223. }
  224. }