2
0

Request.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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_Request
  29. {
  30. /**
  31. * Request ID
  32. * @var mixed
  33. */
  34. protected $_id;
  35. /**
  36. * Flag
  37. * @var bool
  38. */
  39. protected $_isMethodError = false;
  40. /**
  41. * Requested method
  42. * @var string
  43. */
  44. protected $_method;
  45. /**
  46. * Regex for method
  47. * @var string
  48. */
  49. protected $_methodRegex = '/^[a-z][a-z0-9_.]*$/i';
  50. /**
  51. * Request parameters
  52. * @var array
  53. */
  54. protected $_params = array();
  55. /**
  56. * JSON-RPC version of request
  57. * @var string
  58. */
  59. protected $_version = '1.0';
  60. /**
  61. * Set request state
  62. *
  63. * @param array $options
  64. * @return Zend_Json_Server_Request
  65. */
  66. public function setOptions(array $options)
  67. {
  68. $methods = get_class_methods($this);
  69. foreach ($options as $key => $value) {
  70. $method = 'set' . ucfirst($key);
  71. if (in_array($method, $methods)) {
  72. $this->$method($value);
  73. } elseif ($key == 'jsonrpc') {
  74. $this->setVersion($value);
  75. }
  76. }
  77. return $this;
  78. }
  79. /**
  80. * Add a parameter to the request
  81. *
  82. * @param mixed $value
  83. * @param string $key
  84. * @return Zend_Json_Server_Request
  85. */
  86. public function addParam($value, $key = null)
  87. {
  88. if ((null === $key) || !is_string($key)) {
  89. $index = count($this->_params);
  90. $this->_params[$index] = $value;
  91. } else {
  92. $this->_params[$key] = $value;
  93. }
  94. return $this;
  95. }
  96. /**
  97. * Add many params
  98. *
  99. * @param array $params
  100. * @return Zend_Json_Server_Request
  101. */
  102. public function addParams(array $params)
  103. {
  104. foreach ($params as $key => $value) {
  105. $this->addParam($value, $key);
  106. }
  107. return $this;
  108. }
  109. /**
  110. * Overwrite params
  111. *
  112. * @param array $params
  113. * @return Zend_Json_Server_Request
  114. */
  115. public function setParams(array $params)
  116. {
  117. $this->_params = array();
  118. return $this->addParams($params);
  119. }
  120. /**
  121. * Retrieve param by index or key
  122. *
  123. * @param int|string $index
  124. * @return mixed|null Null when not found
  125. */
  126. public function getParam($index)
  127. {
  128. if (array_key_exists($index, $this->_params)) {
  129. return $this->_params[$index];
  130. }
  131. return null;
  132. }
  133. /**
  134. * Retrieve parameters
  135. *
  136. * @return array
  137. */
  138. public function getParams()
  139. {
  140. return $this->_params;
  141. }
  142. /**
  143. * Set request method
  144. *
  145. * @param string $name
  146. * @return Zend_Json_Server_Request
  147. */
  148. public function setMethod($name)
  149. {
  150. if (!preg_match($this->_methodRegex, $name)) {
  151. $this->_isMethodError = true;
  152. } else {
  153. $this->_method = $name;
  154. }
  155. return $this;
  156. }
  157. /**
  158. * Get request method name
  159. *
  160. * @return string
  161. */
  162. public function getMethod()
  163. {
  164. return $this->_method;
  165. }
  166. /**
  167. * Was a bad method provided?
  168. *
  169. * @return bool
  170. */
  171. public function isMethodError()
  172. {
  173. return $this->_isMethodError;
  174. }
  175. /**
  176. * Set request identifier
  177. *
  178. * @param mixed $name
  179. * @return Zend_Json_Server_Request
  180. */
  181. public function setId($name)
  182. {
  183. $this->_id = (string) $name;
  184. return $this;
  185. }
  186. /**
  187. * Retrieve request identifier
  188. *
  189. * @return mixed
  190. */
  191. public function getId()
  192. {
  193. return $this->_id;
  194. }
  195. /**
  196. * Set JSON-RPC version
  197. *
  198. * @param string $version
  199. * @return Zend_Json_Server_Request
  200. */
  201. public function setVersion($version)
  202. {
  203. if ('2.0' == $version) {
  204. $this->_version = '2.0';
  205. } else {
  206. $this->_version = '1.0';
  207. }
  208. return $this;
  209. }
  210. /**
  211. * Retrieve JSON-RPC version
  212. *
  213. * @return string
  214. */
  215. public function getVersion()
  216. {
  217. return $this->_version;
  218. }
  219. /**
  220. * Set request state based on JSON
  221. *
  222. * @param string $json
  223. * @return void
  224. */
  225. public function loadJson($json)
  226. {
  227. require_once 'Zend/Json.php';
  228. $options = Zend_Json::decode($json);
  229. $this->setOptions($options);
  230. }
  231. /**
  232. * Cast request to JSON
  233. *
  234. * @return string
  235. */
  236. public function toJson()
  237. {
  238. $jsonArray = array(
  239. 'method' => $this->getMethod()
  240. );
  241. if (null !== ($id = $this->getId())) {
  242. $jsonArray['id'] = $id;
  243. }
  244. $params = $this->getParams();
  245. if (!empty($params)) {
  246. $jsonArray['params'] = $params;
  247. }
  248. if ('2.0' == $this->getVersion()) {
  249. $jsonArray['jsonrpc'] = '2.0';
  250. }
  251. require_once 'Zend/Json.php';
  252. return Zend_Json::encode($jsonArray);
  253. }
  254. /**
  255. * Cast request to string (JSON)
  256. *
  257. * @return string
  258. */
  259. public function __toString()
  260. {
  261. return $this->toJson();
  262. }
  263. }