Final Class Yiisoft\DataResponse\DataResponse
| Inheritance | Yiisoft\DataResponse\DataResponse |
|---|---|
| Implements | Psr\Http\Message\ResponseInterface |
| Deprecated since version | Use {@see \Yiisoft\DataResponse\DataStream\DataStream} instead. |
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
Yiisoft\DataResponse\Formatter\JsonDataResponseFormatter when Yiisoft\DataResponse\DataResponse::getBody() is called.
Public Methods
Method Details
| public __construct( mixed $data, integer $code, string $reasonPhrase, \Psr\Http\Message\ResponseFactoryInterface $responseFactory, \Psr\Http\Message\StreamFactoryInterface $streamFactory ): mixed | ||
| $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 getBody( ): \Psr\Http\Message\StreamInterface |
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 getData( ): mixed |
public function getData()
{
if (is_callable($this->data)) {
$this->data = ($this->data)();
}
return is_object($this->data) ? clone $this->data : $this->data;
}
| public getHeader( mixed $name ): array | ||
| $name | mixed | |
public function getHeader($name): array
{
$this->formatResponse();
return $this->response->getHeader($name);
}
| public getHeaderLine( mixed $name ): string | ||
| $name | mixed | |
public function getHeaderLine($name): string
{
$this->formatResponse();
return $this->response->getHeaderLine($name);
}
| public getHeaders( ): array |
public function getHeaders(): array
{
$this->formatResponse();
return $this->response->getHeaders();
}
| public getProtocolVersion( ): string |
public function getProtocolVersion(): string
{
$this->formatResponse();
return $this->response->getProtocolVersion();
}
| public getReasonPhrase( ): string |
public function getReasonPhrase(): string
{
$this->formatResponse();
return $this->response->getReasonPhrase();
}
Returns the original instance of the response.
| public getResponse( ): \Psr\Http\Message\ResponseInterface | ||
| return | \Psr\Http\Message\ResponseInterface |
The original instance of the response. |
|---|---|---|
public function getResponse(): ResponseInterface
{
return $this->response;
}
| public getStatusCode( ): integer |
public function getStatusCode(): int
{
$this->formatResponse();
return $this->response->getStatusCode();
}
Checks whether the response data has been set.
| public hasData( ): boolean | ||
| return | boolean |
Whether the response data has been set. |
|---|---|---|
public function hasData(): bool
{
return $this->getData() !== null;
}
| public hasHeader( mixed $name ): boolean | ||
| $name | mixed | |
public function hasHeader($name): bool
{
$this->formatResponse();
return $this->response->hasHeader($name);
}
Checks whether the response formatter has been set withResponseFormatter().
| public hasResponseFormatter( ): boolean | ||
| return | boolean |
Whether the formatter has been set. |
|---|---|---|
public function hasResponseFormatter(): bool
{
return $this->responseFormatter !== null;
}
| public withAddedHeader( mixed $name, mixed $value ): self | ||
| $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 withBody( \Psr\Http\Message\StreamInterface $body ): self | ||
| $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 withData( mixed $data ): self | ||
| $data | mixed |
The response data. |
| throws | RuntimeException |
If the body was previously forced to be set 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 withHeader( mixed $name, mixed $value ): self | ||
| $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 withProtocolVersion( mixed $version ): self | ||
| $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 withResponseFormatter( Yiisoft\DataResponse\DataResponseFormatterInterface $responseFormatter ): self | ||
| $responseFormatter | Yiisoft\DataResponse\DataResponseFormatterInterface | |
public function withResponseFormatter(DataResponseFormatterInterface $responseFormatter): self
{
$new = clone $this;
$new->responseFormatter = $responseFormatter;
$new->resetFormatted();
return $new;
}
| public withStatus( mixed $code, mixed $reasonPhrase = '' ): self | ||
| $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 withoutHeader( mixed $name ): self | ||
| $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.