0 follower

Final Class Yiisoft\DataResponse\Formatter\JsonDataResponseFormatter

InheritanceYiisoft\DataResponse\Formatter\JsonDataResponseFormatter
ImplementsYiisoft\DataResponse\DataResponseFormatterInterface
Uses TraitsYiisoft\DataResponse\ResponseContentTrait

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 \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)
    );
}

            
withContentType() public method

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;
}

            
withEncoding() public method

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;
}

            
withOptions() public method

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;
}