Final Class Yiisoft\DataResponse\DataResponse
| Inheritance | Yiisoft\DataResponse\DataResponse |
|---|---|
| Implements | Psr\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.
Public Methods
Method Details
| 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);
}
| 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,
));
}
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;
}
| public array getHeader ( mixed $name ) | ||
| $name | mixed | |
public function getHeader($name): array
{
$this->formatResponse();
return $this->response->getHeader($name);
}
| public string getHeaderLine ( mixed $name ) | ||
| $name | mixed | |
public function getHeaderLine($name): string
{
$this->formatResponse();
return $this->response->getHeaderLine($name);
}
| public array getHeaders ( ) |
public function getHeaders(): array
{
$this->formatResponse();
return $this->response->getHeaders();
}
| public string getProtocolVersion ( ) |
public function getProtocolVersion(): string
{
$this->formatResponse();
return $this->response->getProtocolVersion();
}
| public string getReasonPhrase ( ) |
public function getReasonPhrase(): string
{
$this->formatResponse();
return $this->response->getReasonPhrase();
}
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;
}
| public integer getStatusCode ( ) |
public function getStatusCode(): int
{
$this->formatResponse();
return $this->response->getStatusCode();
}
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;
}
| public boolean hasHeader ( mixed $name ) | ||
| $name | mixed | |
public function hasHeader($name): bool
{
$this->formatResponse();
return $this->response->hasHeader($name);
}
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;
}
| 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;
}
| 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;
}
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;
}
| 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;
}
| 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;
}
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;
}
| 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;
}
| 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;
}
Signup or Login in order to comment.