Install zendframework via composer, update build.xml and run, updates to composer
[openemr.git] / vendor / zendframework / zendframework / library / Zend / Json / Server / Request.php
blobd27e10a93e9c1ba3ba3ee29eb007fe6efaa580cc
1 <?php
2 /**
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
8 */
10 namespace Zend\Json\Server;
12 use Zend\Json;
14 /**
15 * @todo Revised method regex to allow NS; however, should SMD be revised to strip PHP NS instead when attaching functions?
17 class Request
19 /**
20 * Request ID
21 * @var mixed
23 protected $id;
25 /**
26 * Flag
27 * @var bool
29 protected $isMethodError = false;
31 /**
32 * Flag
33 * @var bool
35 protected $isParseError = false;
37 /**
38 * Requested method
39 * @var string
41 protected $method;
43 /**
44 * Regex for method
45 * @var string
47 protected $methodRegex = '/^[a-z][a-z0-9\\\\_.]*$/i';
49 /**
50 * Request parameters
51 * @var array
53 protected $params = array();
55 /**
56 * JSON-RPC version of request
57 * @var string
59 protected $version = '1.0';
61 /**
62 * Set request state
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);
78 return $this;
81 /**
82 * Add a parameter to the request
84 * @param mixed $value
85 * @param string $key
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;
93 } else {
94 $this->params[$key] = $value;
97 return $this;
101 * Add many params
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);
111 return $this;
115 * Overwrite params
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];
138 return;
142 * Retrieve parameters
144 * @return array
146 public function getParams()
148 return $this->params;
152 * Set request method
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;
161 } else {
162 $this->method = $name;
164 return $this;
168 * Get request method name
170 * @return string
172 public function getMethod()
174 return $this->method;
178 * Was a bad method provided?
180 * @return bool
182 public function isMethodError()
184 return $this->isMethodError;
188 * Was a malformed JSON provided?
190 * @return bool
192 public function isParseError()
194 return $this->isParseError;
198 * Set request identifier
200 * @param mixed $name
201 * @return \Zend\Json\Server\Request
203 public function setId($name)
205 $this->id = (string) $name;
206 return $this;
210 * Retrieve request identifier
212 * @return mixed
214 public function getId()
216 return $this->id;
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';
229 } else {
230 $this->version = '1.0';
232 return $this;
236 * Retrieve JSON-RPC version
238 * @return string
240 public function getVersion()
242 return $this->version;
246 * Set request state based on JSON
248 * @param string $json
249 * @return void
251 public function loadJson($json)
253 try {
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
264 * @return string
266 public function toJson()
268 $jsonArray = array(
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)
288 * @return string
290 public function __toString()
292 return $this->toJson();