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
10 
13 
15 {
19  const HTTP_METHOD_GET = 'GET';
20  const HTTP_METHOD_DELETE = 'DELETE';
21  const HTTP_METHOD_PUT = 'PUT';
22  const HTTP_METHOD_POST = 'POST';
28  const REQUEST_CHARSET = 'utf-8';
29 
30  const DEFAULT_ACCEPT = '*/*';
31 
35  protected $_serviceName;
36 
40  protected $_serviceType;
41 
45  protected $_deserializer;
46 
50  protected $_bodyParams;
51 
56 
67  public function __construct(
68  \Magento\Framework\Stdlib\Cookie\CookieReaderInterface $cookieReader,
69  \Magento\Framework\Stdlib\StringUtils $converter,
70  \Magento\Framework\App\AreaList $areaList,
71  \Magento\Framework\Config\ScopeInterface $configScope,
72  \Magento\Framework\Webapi\Rest\Request\DeserializerFactory $deserializerFactory,
73  $uri = null
74  ) {
75  parent::__construct($cookieReader, $converter, $areaList, $configScope, $uri);
76  $this->_deserializerFactory = $deserializerFactory;
77  }
78 
84  protected function _getDeserializer()
85  {
86  if (null === $this->_deserializer) {
87  $this->_deserializer = $this->_deserializerFactory->get($this->getContentType());
88  }
89  return $this->_deserializer;
90  }
91 
97  public function getAcceptTypes()
98  {
99  $qualityToTypes = [];
100  $orderedTypes = [];
101 
102  foreach (preg_split('/,\s*/', $this->getHeader('Accept')) as $definition) {
103  $typeWithQ = explode(';', $definition);
104  $mimeType = trim(array_shift($typeWithQ));
105 
106  // check MIME type validity
107  if (!preg_match('~^([0-9a-z*+\-]+)(?:/([0-9a-z*+\-\.]+))?$~i', $mimeType)) {
108  continue;
109  }
110  $quality = '1.0';
111  // default value for quality
112 
113  if ($typeWithQ) {
114  $qAndValue = explode('=', $typeWithQ[0]);
115 
116  if (2 == count($qAndValue)) {
117  $quality = $qAndValue[1];
118  }
119  }
120  $qualityToTypes[$quality][$mimeType] = true;
121  }
122  krsort($qualityToTypes);
123 
124  foreach ($qualityToTypes as $typeList) {
125  $orderedTypes += $typeList;
126  }
127  return empty($orderedTypes) ? [self::DEFAULT_ACCEPT] : array_keys($orderedTypes);
128  }
129 
135  public function getBodyParams()
136  {
137  if (null == $this->_bodyParams) {
138  $this->_bodyParams = [];
139  //avoid JSON decoding with empty string
140  if ($this->getContent()) {
141  $this->_bodyParams = (array)$this->_getDeserializer()->deserialize((string)$this->getContent());
142  }
143  }
144  return $this->_bodyParams;
145  }
146 
153  public function getContentType()
154  {
155  $headerValue = $this->getHeader('Content-Type');
156 
157  if (!$headerValue) {
158  throw new \Magento\Framework\Exception\InputException(new Phrase('Content-Type header is empty.'));
159  }
160  if (!preg_match('~^([a-z\d/\-+.]+)(?:; *charset=(.+))?$~Ui', $headerValue, $matches)) {
161  throw new \Magento\Framework\Exception\InputException(new Phrase('Content-Type header is invalid.'));
162  }
163  // request encoding check if it is specified in header
164  if (isset($matches[2]) && self::REQUEST_CHARSET != strtolower($matches[2])) {
165  throw new \Magento\Framework\Exception\InputException(new Phrase('UTF-8 is the only supported charset.'));
166  }
167 
168  return $matches[1];
169  }
170 
177  public function getHttpMethod()
178  {
179  if (!$this->isGet() && !$this->isPost() && !$this->isPut() && !$this->isDelete()) {
180  throw new \Magento\Framework\Exception\InputException(new Phrase('Request method is invalid.'));
181  }
182  return $this->getMethod();
183  }
184 
190  public function getRequestData()
191  {
192  $requestBodyParams = [];
193  $params = $this->getParams();
194 
195  $httpMethod = $this->getHttpMethod();
196  if ($httpMethod == self::HTTP_METHOD_POST ||
197  $httpMethod == self::HTTP_METHOD_PUT
198  ) {
199  $requestBodyParams = $this->getBodyParams();
200  }
201 
202  return array_merge($requestBodyParams, $params);
203  }
204 
223  protected function overrideRequestBodyIdWithPathParam($urlPathParams)
224  {
225  $requestBodyParams = $this->getBodyParams();
226  $pathParamValue = end($urlPathParams);
227  // Self apis should not be overridden
228  if ($pathParamValue === 'me') {
229  return $requestBodyParams;
230  }
231  $pathParamKey = key($urlPathParams);
232  // Check if the request data is a top level object of body
233  if (count($requestBodyParams) == 1 && is_array(end($requestBodyParams))) {
234  $requestDataKey = key($requestBodyParams);
235  $this->substituteParameters($requestBodyParams[$requestDataKey], $pathParamKey, $pathParamValue);
236  } else { // Else parameters passed as scalar values in body will be overridden
237  $this->substituteParameters($requestBodyParams, $pathParamKey, $pathParamValue);
238  }
239 
240  return $requestBodyParams;
241  }
242 
253  protected function substituteParameters(&$requestData, $key, $value)
254  {
257 
258  if (isset($requestData[$camelCaseKey])) {
259  $requestData[$camelCaseKey] = $value;
260  } else {
261  $requestData[$snakeCaseKey] = $value;
262  }
263  }
264 }
overrideRequestBodyIdWithPathParam($urlPathParams)
Definition: Request.php:223
getHeader($header, $default=false)
Definition: Request.php:62
$value
Definition: gender.phtml:16
substituteParameters(&$requestData, $key, $value)
Definition: Request.php:253
__construct(\Magento\Framework\Stdlib\Cookie\CookieReaderInterface $cookieReader, \Magento\Framework\Stdlib\StringUtils $converter, \Magento\Framework\App\AreaList $areaList, \Magento\Framework\Config\ScopeInterface $configScope, \Magento\Framework\Webapi\Rest\Request\DeserializerFactory $deserializerFactory, $uri=null)
Definition: Request.php:67