Abstract Class Yiisoft\DataResponse\ResponseFactory\AbstractFormattedResponseFactory
Abstract factory class that creates responses with pre-formatted {@see DataStream} body and appropriate response headers.
The formatter is applied immediately when creating the response.
Public Methods
Method Details
| public mixed __construct ( \ | ||
| $responseFactory | \ |
The PSR-17 response factory. |
| $formatter | Yiisoft\ |
The formatter to apply to the response. |
public function __construct(
private readonly ResponseFactoryInterface $responseFactory,
private readonly FormatterInterface $formatter,
) {}
| public \ | ||
| $data | mixed | |
| $code | integer | |
| $reasonPhrase | string | |
final public function createResponse(
mixed $data = null,
int $code = Status::OK,
string $reasonPhrase = '',
): ResponseInterface {
$body = new DataStream($data, $this->formatter);
$response = $this->responseFactory
->createResponse($code, $reasonPhrase)
->withBody($body);
return $this->formatter->formatResponse($response);
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.