/Zend/Json/Server/Request/Request.php

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