3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Json\Server
;
15 * @todo Revised method regex to allow NS; however, should SMD be revised to strip PHP NS instead when attaching functions?
29 protected $isMethodError = false;
35 protected $isParseError = false;
47 protected $methodRegex = '/^[a-z][a-z0-9\\\\_.]*$/i';
53 protected $params = array();
56 * JSON-RPC version of request
59 protected $version = '1.0';
64 * @param array $options
65 * @return \Zend\Json\Server\Request
67 public function setOptions(array $options)
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);
82 * Add a parameter to the request
86 * @return \Zend\Json\Server\Request
88 public function addParam($value, $key = null)
90 if ((null === $key) ||
!is_string($key)) {
91 $index = count($this->params
);
92 $this->params
[$index] = $value;
94 $this->params
[$key] = $value;
103 * @param array $params
104 * @return \Zend\Json\Server\Request
106 public function addParams(array $params)
108 foreach ($params as $key => $value) {
109 $this->addParam($value, $key);
117 * @param array $params
118 * @return \Zend\Json\Server\Request
120 public function setParams(array $params)
122 $this->params
= array();
123 return $this->addParams($params);
127 * Retrieve param by index or key
129 * @param int|string $index
130 * @return mixed|null Null when not found
132 public function getParam($index)
134 if (array_key_exists($index, $this->params
)) {
135 return $this->params
[$index];
142 * Retrieve parameters
146 public function getParams()
148 return $this->params
;
154 * @param string $name
155 * @return \Zend\Json\Server\Request
157 public function setMethod($name)
159 if (!preg_match($this->methodRegex
, $name)) {
160 $this->isMethodError
= true;
162 $this->method
= $name;
168 * Get request method name
172 public function getMethod()
174 return $this->method
;
178 * Was a bad method provided?
182 public function isMethodError()
184 return $this->isMethodError
;
188 * Was a malformed JSON provided?
192 public function isParseError()
194 return $this->isParseError
;
198 * Set request identifier
201 * @return \Zend\Json\Server\Request
203 public function setId($name)
205 $this->id
= (string) $name;
210 * Retrieve request identifier
214 public function getId()
220 * Set JSON-RPC version
222 * @param string $version
223 * @return \Zend\Json\Server\Request
225 public function setVersion($version)
227 if ('2.0' == $version) {
228 $this->version
= '2.0';
230 $this->version
= '1.0';
236 * Retrieve JSON-RPC version
240 public function getVersion()
242 return $this->version
;
246 * Set request state based on JSON
248 * @param string $json
251 public function loadJson($json)
254 $options = Json\Json
::decode($json, Json\Json
::TYPE_ARRAY
);
255 $this->setOptions($options);
256 } catch (\Exception
$e) {
257 $this->isParseError
= true;
262 * Cast request to JSON
266 public function toJson()
269 'method' => $this->getMethod()
271 if (null !== ($id = $this->getId())) {
272 $jsonArray['id'] = $id;
274 $params = $this->getParams();
275 if (!empty($params)) {
276 $jsonArray['params'] = $params;
278 if ('2.0' == $this->getVersion()) {
279 $jsonArray['jsonrpc'] = '2.0';
282 return Json\Json
::encode($jsonArray);
286 * Cast request to string (JSON)
290 public function __toString()
292 return $this->toJson();