/Zend/Json/Server/Request.php

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