/var/www/vhosts/ihelp.ro/_OLD/vendor/cakephp/cakephp/src/Http/Middleware
Edit: /var/www/vhosts/ihelp.ro/_OLD/vendor/cakephp/cakephp/src/Http/Middleware/BodyParserMiddleware.php (5940B)
true, 'xml' => false, 'methods' => null];
if ($options['json']) {
$this->addParser(
['application/json', 'text/json'],
Closure::fromCallable([$this, 'decodeJson'])
);
}
if ($options['xml']) {
$this->addParser(
['application/xml', 'text/xml'],
Closure::fromCallable([$this, 'decodeXml'])
);
}
if ($options['methods']) {
$this->setMethods($options['methods']);
}
}
/**
* Set the HTTP methods to parse request bodies on.
*
* @param string[] $methods The methods to parse data on.
* @return $this
*/
public function setMethods(array $methods)
{
$this->methods = $methods;
return $this;
}
/**
* Get the HTTP methods to parse request bodies on.
*
* @return string[]
*/
public function getMethods(): array
{
return $this->methods;
}
/**
* Add a parser.
*
* Map a set of content-type header values to be parsed by the $parser.
*
* ### Example
*
* An naive CSV request body parser could be built like so:
*
* ```
* $parser->addParser(['text/csv'], function ($body) {
* return str_getcsv($body);
* });
* ```
*
* @param string[] $types An array of content-type header values to match. eg. application/json
* @param \Closure $parser The parser function. Must return an array of data to be inserted
* into the request.
* @return $this
*/
public function addParser(array $types, Closure $parser)
{
foreach ($types as $type) {
$type = strtolower($type);
$this->parsers[$type] = $parser;
}
return $this;
}
/**
* Get the current parsers
*
* @return \Closure[]
*/
public function getParsers(): array
{
return $this->parsers;
}
/**
* Apply the middleware.
*
* Will modify the request adding a parsed body if the content-type is known.
*
* @param \Psr\Http\Message\ServerRequestInterface $request The request.
* @param \Psr\Http\Server\RequestHandlerInterface $handler The request handler.
* @return \Psr\Http\Message\ResponseInterface A response.
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if (!in_array($request->getMethod(), $this->methods, true)) {
return $handler->handle($request);
}
[$type] = explode(';', $request->getHeaderLine('Content-Type'));
$type = strtolower($type);
if (!isset($this->parsers[$type])) {
return $handler->handle($request);
}
$parser = $this->parsers[$type];
$result = $parser($request->getBody()->getContents());
if (!is_array($result)) {
throw new BadRequestException();
}
$request = $request->withParsedBody($result);
return $handler->handle($request);
}
/**
* Decode JSON into an array.
*
* @param string $body The request body to decode
* @return mixed
*/
protected function decodeJson(string $body)
{
return json_decode($body, true);
}
/**
* Decode XML into an array.
*
* @param string $body The request body to decode
* @return array
*/
protected function decodeXml(string $body): array
{
try {
$xml = Xml::build($body, ['return' => 'domdocument', 'readFile' => false]);
// We might not get child nodes if there are nested inline entities.
if ((int)$xml->childNodes->length > 0) {
return Xml::toArray($xml);
}
return [];
} catch (XmlException $e) {
return [];
}
}
}