2
0

Response.php 4.8 KB

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