Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Request.php
Go to the documentation of this file.
1 <?php
31 {
36  protected $_id;
37 
42  protected $_isMethodError = false;
43 
48  protected $_method;
49 
54  protected $_methodRegex = '/^[a-z][a-z0-9_.]*$/i';
55 
60  protected $_params = array();
61 
66  protected $_version = '1.0';
67 
74  public function setOptions(array $options)
75  {
76  $methods = get_class_methods($this);
77  foreach ($options as $key => $value) {
78  $method = 'set' . ucfirst($key);
79  if (in_array($method, $methods)) {
80  $this->$method($value);
81  } elseif ($key == 'jsonrpc') {
82  $this->setVersion($value);
83  }
84  }
85  return $this;
86  }
87 
95  public function addParam($value, $key = null)
96  {
97  if ((null === $key) || !is_string($key)) {
98  $index = count($this->_params);
99  $this->_params[$index] = $value;
100  } else {
101  $this->_params[$key] = $value;
102  }
103 
104  return $this;
105  }
106 
113  public function addParams(array $params)
114  {
115  foreach ($params as $key => $value) {
116  $this->addParam($value, $key);
117  }
118  return $this;
119  }
120 
127  public function setParams(array $params)
128  {
129  $this->_params = array();
130  return $this->addParams($params);
131  }
132 
139  public function getParam($index)
140  {
141  if (array_key_exists($index, $this->_params)) {
142  return $this->_params[$index];
143  }
144 
145  return null;
146  }
147 
153  public function getParams()
154  {
155  return $this->_params;
156  }
157 
164  public function setMethod($name)
165  {
166  if (!preg_match($this->_methodRegex, $name)) {
167  $this->_isMethodError = true;
168  } else {
169  $this->_method = $name;
170  }
171  return $this;
172  }
173 
179  public function getMethod()
180  {
181  return $this->_method;
182  }
183 
189  public function isMethodError()
190  {
191  return $this->_isMethodError;
192  }
193 
200  public function setId($name)
201  {
202  $this->_id = (string) $name;
203  return $this;
204  }
205 
211  public function getId()
212  {
213  return $this->_id;
214  }
215 
222  public function setVersion($version)
223  {
224  if ('2.0' == $version) {
225  $this->_version = '2.0';
226  } else {
227  $this->_version = '1.0';
228  }
229  return $this;
230  }
231 
237  public function getVersion()
238  {
239  return $this->_version;
240  }
241 
248  public function loadJson($json)
249  {
250  #require_once 'Zend/Json.php';
251  $options = Zend_Json::decode($json);
252  $this->setOptions($options);
253  }
254 
260  public function toJson()
261  {
262  $jsonArray = array(
263  'method' => $this->getMethod()
264  );
265  if (null !== ($id = $this->getId())) {
266  $jsonArray['id'] = $id;
267  }
268  $params = $this->getParams();
269  if (!empty($params)) {
270  $jsonArray['params'] = $params;
271  }
272  if ('2.0' == $this->getVersion()) {
273  $jsonArray['jsonrpc'] = '2.0';
274  }
275 
276  #require_once 'Zend/Json.php';
277  return Zend_Json::encode($jsonArray);
278  }
279 
285  public function __toString()
286  {
287  return $this->toJson();
288  }
289 }
static decode($encodedValue, $objectDecodeType=Zend_Json::TYPE_ARRAY)
Definition: Json.php:74
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$id
Definition: fieldset.phtml:14
setParams(array $params)
Definition: Request.php:127
$methods
Definition: billing.phtml:71
$value
Definition: gender.phtml:16
addParams(array $params)
Definition: Request.php:113
$method
Definition: info.phtml:13
addParam($value, $key=null)
Definition: Request.php:95
setOptions(array $options)
Definition: Request.php:74
static encode($valueToEncode, $cycleCheck=false, $options=array())
Definition: Json.php:130
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
$index
Definition: list.phtml:44
if(!isset($_GET['name'])) $name
Definition: log.php:14