0 follower

Final Class Yiisoft\DataResponse\Formatter\JsonDataResponseFormatter

InheritanceYiisoft\DataResponse\Formatter\JsonDataResponseFormatter
ImplementsYiisoft\DataResponse\DataResponseFormatterInterface
Uses TraitsYiisoft\DataResponse\ResponseContentTrait
Deprecated since version Use {@see \Yiisoft\DataResponse\Formatter\DataStream} with {@see \Yiisoft\DataResponse\Formatter\JsonFormatter} instead.

JsonDataResponseFormatter formats the response data as JSON.

Public Methods

Hide inherited 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

Hide inherited methods

format() public method

Returns an instance of the response with formatted response data.

public format( Yiisoft\DataResponse\DataResponse $dataResponse ): \Psr\Http\Message\ResponseInterface
$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),
    );
}

            
withContentType() public method

Defined in: Yiisoft\DataResponse\ResponseContentTrait::withContentType()

Returns a new instance with the specified content type.

public withContentType( string $contentType ): self
$contentType string

The content type. For example, "text/html".

                public function withContentType(string $contentType): self
{
    $new = clone $this;
    $new->contentType = $contentType;
    return $new;
}

            
withEncoding() public method

Defined in: Yiisoft\DataResponse\ResponseContentTrait::withEncoding()

Returns a new instance with the specified encoding.

public withEncoding( string $encoding ): self
$encoding string

The encoding. For example, "UTF-8".

                public function withEncoding(string $encoding): self
{
    $new = clone $this;
    $new->encoding = $encoding;
    return $new;
}

            
withOptions() public method

Returns a new instance with the specified encoding options.

public withOptions( integer $options ): self
$options integer

The encoding options. For more details please refer to https://www.php.net/manual/en/function.json-encode.php.

                public function withOptions(int $options): self
{
    $new = clone $this;
    $new->options = $options;
    return $new;
}