0 follower

Final Class Yiisoft\DataResponse\DataResponse

InheritanceYiisoft\DataResponse\DataResponse
ImplementsPsr\Http\Message\ResponseInterface

A wrapper around PSR-7 response that is assigned raw data to be formatted with a formatter later.

For example, ['name' => 'Dmitriy'] to be formatted as JSON using {@see \Yiisoft\DataResponse\Formatter\JsonDataResponseFormatter} when {@see \Yiisoft\DataResponse\DataResponse::getBody()} is called.

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( mixed $data, integer $code, string $reasonPhrase, \Psr\Http\Message\ResponseFactoryInterface $responseFactory, \Psr\Http\Message\StreamFactoryInterface $streamFactory )
$data mixed

The response data.

$code integer

The response status code.

$reasonPhrase string

The response reason phrase associated with the status code.

$responseFactory \Psr\Http\Message\ResponseFactoryInterface

The response factory instance.

$streamFactory \Psr\Http\Message\StreamFactoryInterface

The stream factory instance.

                public function __construct(
    private mixed $data,
    int $code,
    string $reasonPhrase,
    ResponseFactoryInterface $responseFactory,
    StreamFactoryInterface $streamFactory
) {
    $this->createResponse($code, $reasonPhrase, $responseFactory, $streamFactory);
}

            
getBody() public method

public \Psr\Http\Message\StreamInterface getBody ( )

                public function getBody(): StreamInterface
{
    if ($this->dataStream !== null) {
        return $this->dataStream;
    }
    if ($this->hasResponseFormatter()) {
        $this->formatResponse();
        return $this->dataStream = $this->response->getBody();
    }
    if ($this->data === null) {
        $this->clearResponseBody();
        return $this->dataStream = $this->response->getBody();
    }
    $data = $this->getData();
    if (is_string($data)) {
        $this->clearResponseBody();
        $this->response
            ->getBody()
            ->write($data);
        return $this->dataStream = $this->response->getBody();
    }
    throw new RuntimeException(sprintf(
        'The data is "%s" not a string. To get non-string data, use the "%s::getData()" method.',
        get_debug_type($data),
        self::class,
    ));
}

            
getData() public method

Returns the response data.

If the response data is a PHP callable, the result of the PHP callable execute will be returned. If the response data or the result of the execution of the PHP callable is an object, a cloned copy of this object will be returned.

public mixed getData ( )

                public function getData()
{
    if (is_callable($this->data)) {
        $this->data = ($this->data)();
    }
    return is_object($this->data) ? clone $this->data : $this->data;
}

            
getHeader() public method

public array getHeader ( mixed $name )
$name mixed

                public function getHeader($name): array
{
    $this->formatResponse();
    return $this->response->getHeader($name);
}

            
getHeaderLine() public method

public string getHeaderLine ( mixed $name )
$name mixed

                public function getHeaderLine($name): string
{
    $this->formatResponse();
    return $this->response->getHeaderLine($name);
}

            
getHeaders() public method

public array getHeaders ( )

                public function getHeaders(): array
{
    $this->formatResponse();
    return $this->response->getHeaders();
}

            
getProtocolVersion() public method

public string getProtocolVersion ( )

                public function getProtocolVersion(): string
{
    $this->formatResponse();
    return $this->response->getProtocolVersion();
}

            
getReasonPhrase() public method

public string getReasonPhrase ( )

                public function getReasonPhrase(): string
{
    $this->formatResponse();
    return $this->response->getReasonPhrase();
}

            
getResponse() public method

Returns the original instance of the response.

public \Psr\Http\Message\ResponseInterface getResponse ( )
return \Psr\Http\Message\ResponseInterface

The original instance of the response.

                public function getResponse(): ResponseInterface
{
    return $this->response;
}

            
getStatusCode() public method

public integer getStatusCode ( )

                public function getStatusCode(): int
{
    $this->formatResponse();
    return $this->response->getStatusCode();
}

            
hasData() public method

Checks whether the response data has been set.

public boolean hasData ( )
return boolean

Whether the response data has been set.

                public function hasData(): bool
{
    return $this->getData() !== null;
}

            
hasHeader() public method

public boolean hasHeader ( mixed $name )
$name mixed

                public function hasHeader($name): bool
{
    $this->formatResponse();
    return $this->response->hasHeader($name);
}

            
hasResponseFormatter() public method

Checks whether the response formatter has been set {@see withResponseFormatter()}.

public boolean hasResponseFormatter ( )
return boolean

Whether the formatter has been set.

                public function hasResponseFormatter(): bool
{
    return $this->responseFormatter !== null;
}

            
withAddedHeader() public method

public self withAddedHeader ( mixed $name, mixed $value )
$name mixed
$value mixed

                public function withAddedHeader($name, $value): self
{
    $new = clone $this;
    $new->response = $this->response->withAddedHeader($name, $value);
    $new->resetFormatted();
    return $new;
}

            
withBody() public method

public self withBody ( \Psr\Http\Message\StreamInterface $body )
$body \Psr\Http\Message\StreamInterface

                public function withBody(StreamInterface $body): self
{
    $new = clone $this;
    $new->response = $this->response->withBody($body);
    $new->resetFormatted();
    $new->dataStream = $body;
    $new->forcedBody = true;
    $new->data = null;
    return $new;
}

            
withData() public method

Returns a new instance with the specified response data.

public self withData ( mixed $data )
$data mixed

The response data.

throws RuntimeException

If the body was previously forced to be set {@see \Yiisoft\DataResponse\withBody()}.

                public function withData(mixed $data): self
{
    if ($this->forcedBody) {
        throw new RuntimeException(sprintf(
            'The data cannot be set because the body was previously'
            . ' forced to be set using the "%s::withBody()" method.',
            self::class,
        ));
    }
    $new = clone $this;
    $new->data = $data;
    $new->resetFormatted();
    return $new;
}

            
withHeader() public method

public self withHeader ( mixed $name, mixed $value )
$name mixed
$value mixed

                public function withHeader($name, $value): self
{
    $new = clone $this;
    $new->response = $this->response->withHeader($name, $value);
    $new->resetFormatted();
    return $new;
}

            
withProtocolVersion() public method

public self withProtocolVersion ( mixed $version )
$version mixed

                public function withProtocolVersion($version): self
{
    $new = clone $this;
    $new->response = $this->response->withProtocolVersion($version);
    $new->resetFormatted();
    return $new;
}

            
withResponseFormatter() public method

Returns a new instance with the specified response formatter.

public self withResponseFormatter ( Yiisoft\DataResponse\DataResponseFormatterInterface $responseFormatter )
$responseFormatter Yiisoft\DataResponse\DataResponseFormatterInterface

                public function withResponseFormatter(DataResponseFormatterInterface $responseFormatter): self
{
    $new = clone $this;
    $new->responseFormatter = $responseFormatter;
    $new->resetFormatted();
    return $new;
}

            
withStatus() public method

public self withStatus ( mixed $code, mixed $reasonPhrase '' )
$code mixed
$reasonPhrase mixed

                public function withStatus($code, $reasonPhrase = ''): self
{
    $new = clone $this;
    $new->response = $this->response->withStatus($code, $reasonPhrase);
    $new->resetFormatted();
    return $new;
}

            
withoutHeader() public method

public self withoutHeader ( mixed $name )
$name mixed

                public function withoutHeader($name): self
{
    $new = clone $this;
    $new->formatResponse();
    $new->response = $new->response->withoutHeader($name);
    return $new;
}