Final Class Yiisoft\DataResponse\Formatter\JsonDataResponseFormatter
| Inheritance | Yiisoft\DataResponse\Formatter\JsonDataResponseFormatter |
|---|---|
| Implements | Yiisoft\DataResponse\DataResponseFormatterInterface |
| Uses Traits | Yiisoft\DataResponse\ResponseContentTrait |
JsonDataResponseFormatter formats the response data as JSON.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| format() | Returns an instance of the response with formatted response data. | Yiisoft\DataResponse\Formatter\JsonDataResponseFormatter |
| withContentType() | Returns a new instance with the specified content type. | Yiisoft\DataResponse\ResponseContentTrait |
| withEncoding() | Returns a new instance with the specified encoding. | Yiisoft\DataResponse\ResponseContentTrait |
| withOptions() | Returns a new instance with the specified encoding options. | Yiisoft\DataResponse\Formatter\JsonDataResponseFormatter |
Method Details
Returns an instance of the response with formatted response data.
| public \Psr\Http\Message\ResponseInterface format ( Yiisoft\DataResponse\DataResponse $dataResponse ) | ||
| $dataResponse | Yiisoft\DataResponse\DataResponse |
The instance of the data response. |
| return | \Psr\Http\Message\ResponseInterface |
The response with formatted response data. |
|---|---|---|
| throws | JsonException | |
public function format(DataResponse $dataResponse): ResponseInterface
{
if (!$dataResponse->hasData()) {
return $this->addToResponse($dataResponse->getResponse());
}
return $this->addToResponse(
$dataResponse->getResponse(),
Json::encode($dataResponse->getData(), $this->options)
);
}
Defined in: Yiisoft\DataResponse\ResponseContentTrait::withContentType()
Returns a new instance with the specified content type.
| public self withContentType ( string $contentType ) | ||
| $contentType | string |
The content type. For example, "text/html". |
public function withContentType(string $contentType): self
{
$new = clone $this;
$new->contentType = $contentType;
return $new;
}
Defined in: Yiisoft\DataResponse\ResponseContentTrait::withEncoding()
Returns a new instance with the specified encoding.
| public self withEncoding ( string $encoding ) | ||
| $encoding | string |
The encoding. For example, "UTF-8". |
public function withEncoding(string $encoding): self
{
$new = clone $this;
$new->encoding = $encoding;
return $new;
}
Returns a new instance with the specified encoding options.
| public self withOptions ( integer $options ) | ||
| $options | integer |
The encoding options. For more details please refer to {@link https://www.php.net/manual/en/function.json-encode.php}. |
public function withOptions(int $options): self
{
$new = clone $this;
$new->options = $options;
return $new;
}
Signup or Login in order to comment.