Fr3nch13/CakePHP Utilities

BodyParserMiddleware
in package
implements MiddlewareInterface

Parse encoded request body data.

Enables JSON and XML request payloads to be parsed into the request's body. You can also add your own request body parsers using the addParser() method.

Table of Contents

Interfaces

MiddlewareInterface
Participant in processing a server request and response.

Properties

$methods  : array<string|int, string>
The HTTP methods to parse data on.
$parsers  : array<string|int, Closure>
Registered Parsers

Methods

__construct()  : mixed
Constructor
addParser()  : $this
Add a parser.
getMethods()  : array<string|int, string>
Get the HTTP methods to parse request bodies on.
getParsers()  : array<string|int, Closure>
Get the current parsers
process()  : ResponseInterface
Apply the middleware.
setMethods()  : $this
Set the HTTP methods to parse request bodies on.
decodeJson()  : array<string|int, mixed>|null
Decode JSON into an array.
decodeXml()  : array<string|int, mixed>
Decode XML into an array.

Properties

$methods

The HTTP methods to parse data on.

protected array<string|int, string> $methods = ['PUT', 'POST', 'PATCH', 'DELETE']

Methods

__construct()

Constructor

public __construct([array<string, mixed> $options = [] ]) : mixed

Options

  • json Set to false to disable JSON body parsing.
  • xml Set to true to enable XML parsing. Defaults to false, as XML handling requires more care than JSON does.
  • methods The HTTP methods to parse on. Defaults to PUT, POST, PATCH DELETE.
Parameters
$options : array<string, mixed> = []

The options to use. See above.

addParser()

Add a parser.

public addParser(array<string|int, string> $types, Closure $parser) : $this

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);
});
Parameters
$types : array<string|int, string>

An array of content-type header values to match. eg. application/json

$parser : Closure

The parser function. Must return an array of data to be inserted into the request.

Return values
$this

getMethods()

Get the HTTP methods to parse request bodies on.

public getMethods() : array<string|int, string>
Return values
array<string|int, string>

getParsers()

Get the current parsers

public getParsers() : array<string|int, Closure>
Return values
array<string|int, Closure>

setMethods()

Set the HTTP methods to parse request bodies on.

public setMethods(array<string|int, string> $methods) : $this
Parameters
$methods : array<string|int, string>

The methods to parse data on.

Return values
$this

decodeJson()

Decode JSON into an array.

protected decodeJson(string $body) : array<string|int, mixed>|null
Parameters
$body : string

The request body to decode

Return values
array<string|int, mixed>|null

decodeXml()

Decode XML into an array.

protected decodeXml(string $body) : array<string|int, mixed>
Parameters
$body : string

The request body to decode

Return values
array<string|int, mixed>

        
On this page

Search results