/framework/vendor/zend/Zend/Json/Server/Response.php

http://zoop.googlecode.com/ · PHP · 250 lines · 97 code · 24 blank · 129 comment · 6 complexity · 0ba2a61b5239fbabfb37d3d4887d0461 MD5 · raw file

  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. * @category Zend
  24. * @package Zend_Json
  25. * @subpackage Server
  26. * @copyright Copyright (c) 2005-2010 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. 'result' => null,
  161. 'error' => $this->getError()->toArray(),
  162. 'id' => $this->getId(),
  163. );
  164. } else {
  165. $response = array(
  166. 'result' => $this->getResult(),
  167. 'id' => $this->getId(),
  168. 'error' => null,
  169. );
  170. }
  171. if (null !== ($version = $this->getVersion())) {
  172. $response['jsonrpc'] = $version;
  173. }
  174. require_once 'Zend/Json.php';
  175. return Zend_Json::encode($response);
  176. }
  177. /**
  178. * Retrieve args
  179. *
  180. * @return mixed
  181. */
  182. public function getArgs()
  183. {
  184. return $this->_args;
  185. }
  186. /**
  187. * Set args
  188. *
  189. * @param mixed $args
  190. * @return self
  191. */
  192. public function setArgs($args)
  193. {
  194. $this->_args = $args;
  195. return $this;
  196. }
  197. /**
  198. * Set service map object
  199. *
  200. * @param Zend_Json_Server_Smd $serviceMap
  201. * @return Zend_Json_Server_Response
  202. */
  203. public function setServiceMap($serviceMap)
  204. {
  205. $this->_serviceMap = $serviceMap;
  206. return $this;
  207. }
  208. /**
  209. * Retrieve service map
  210. *
  211. * @return Zend_Json_Server_Smd|null
  212. */
  213. public function getServiceMap()
  214. {
  215. return $this->_serviceMap;
  216. }
  217. /**
  218. * Cast to string (JSON)
  219. *
  220. * @return string
  221. */
  222. public function __toString()
  223. {
  224. return $this->toJson();
  225. }
  226. }