Final Class Yiisoft\Request\Body\RequestBodyParser
| Inheritance | Yiisoft\ |
|---|---|
| Implements | Psr\ |
The package is a PSR-15 middleware that allows parsing PSR-7 server request body selecting the parser according to the server request mime type.
See also:
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\ |
|
| ignoreBadRequestBody() | Makes the middleware to simple skip requests it cannot parse. | Yiisoft\ |
| process() | Yiisoft\ |
|
| withParser() | Registers a request parser for a mime type specified. | Yiisoft\ |
| withoutParsers() | Returns new instance with parsers un-registered for mime types specified. | Yiisoft\ |
Method Details
| public mixed __construct ( \ | ||
| $responseFactory | \ |
|
| $container | \ |
|
| $badRequestHandler | ?\ |
|
public function __construct(
ResponseFactoryInterface $responseFactory,
private readonly ContainerInterface $container,
?BadRequestHandlerInterface $badRequestHandler = null,
) {
$this->badRequestHandler = $badRequestHandler ?? new BadRequestHandler($responseFactory);
}
Makes the middleware to simple skip requests it cannot parse.
| public self ignoreBadRequestBody ( ) |
public function ignoreBadRequestBody(): self
{
$new = clone $this;
$new->ignoreBadRequestBody = true;
return $new;
}
| public \ | ||
| $request | \ |
|
| $handler | \ |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$parser = $this->getParser($this->getContentType($request));
if ($parser !== null) {
try {
/** @var mixed $parsed */
$parsed = $parser->parse((string) $request->getBody());
if ($parsed !== null && !is_object($parsed) && !is_array($parsed)) {
$parserClass = $parser::class;
throw new RuntimeException(
"$parserClass::parse() return value must be an array, an object, or null.",
);
}
$request = $request->withParsedBody($parsed);
} catch (ParserException $e) {
if (!$this->ignoreBadRequestBody) {
return $this->badRequestHandler
->withParserException($e)
->handle($request);
}
}
}
return $handler->handle($request);
}
Registers a request parser for a mime type specified.
| public self withParser ( string $mimeType, string $parserClass ) | ||
| $mimeType | string |
Mime type to register parser for. |
| $parserClass | string |
Parser fully qualified name. |
public function withParser(string $mimeType, string $parserClass): self
{
$this->validateMimeType($mimeType);
if ($parserClass === '') {
throw new InvalidArgumentException('The parser class cannot be an empty string.');
}
if ($this->container->has($parserClass) === false) {
throw new InvalidArgumentException("The parser \"$parserClass\" cannot be found.");
}
$new = clone $this;
$new->parsers[$this->normalizeMimeType($mimeType)] = $parserClass;
return $new;
}
Returns new instance with parsers un-registered for mime types specified.
| public self withoutParsers ( string $mimeTypes ) | ||
| $mimeTypes | string |
Mime types to unregister parsers for. |
public function withoutParsers(string ...$mimeTypes): self
{
$new = clone $this;
if (count($mimeTypes) === 0) {
$new->parsers = [];
return $new;
}
foreach ($mimeTypes as $mimeType) {
$this->validateMimeType($mimeType);
unset($new->parsers[$this->normalizeMimeType($mimeType)]);
}
return $new;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.