class JsonResponse in Zircon Profile 8.0
Same name in this branch
- 8.0 vendor/symfony/http-foundation/JsonResponse.php \Symfony\Component\HttpFoundation\JsonResponse
- 8.0 vendor/zendframework/zend-diactoros/src/Response/JsonResponse.php \Zend\Diactoros\Response\JsonResponse
Same name and namespace in other branches
- 8 vendor/zendframework/zend-diactoros/src/Response/JsonResponse.php \Zend\Diactoros\Response\JsonResponse
JSON response.
Allows creating a response by passing data to the constructor; by default, serializes the data to JSON, sets a status code of 200 and sets the Content-Type header to application/json.
Hierarchy
- class \Zend\Diactoros\Response implements ResponseInterface uses MessageTrait
- class \Zend\Diactoros\Response\JsonResponse uses InjectContentTypeTrait
Expanded class hierarchy of JsonResponse
File
- vendor/
zendframework/ zend-diactoros/ src/ Response/ JsonResponse.php, line 23
Namespace
Zend\Diactoros\ResponseView source
class JsonResponse extends Response {
use InjectContentTypeTrait;
/**
* Create a JSON response with the given data.
*
* Default JSON encoding is performed with the following options, which
* produces RFC4627-compliant JSON, capable of embedding into HTML.
*
* - JSON_HEX_TAG
* - JSON_HEX_APOS
* - JSON_HEX_AMP
* - JSON_HEX_QUOT
*
* @param mixed $data Data to convert to JSON.
* @param int $status Integer status code for the response; 200 by default.
* @param array $headers Array of headers to use at initialization.
* @param int $encodingOptions JSON encoding options to use.
* @throws InvalidArgumentException if unable to encode the $data to JSON.
*/
public function __construct($data, $status = 200, array $headers = [], $encodingOptions = 15) {
$body = new Stream('php://temp', 'wb+');
$body
->write($this
->jsonEncode($data, $encodingOptions));
$headers = $this
->injectContentType('application/json', $headers);
parent::__construct($body, $status, $headers);
}
/**
* Encode the provided data to JSON.
*
* @param mixed $data
* @param int $encodingOptions
* @return string
* @throws InvalidArgumentException if unable to encode the $data to JSON.
*/
private function jsonEncode($data, $encodingOptions) {
if (is_resource($data)) {
throw new InvalidArgumentException('Cannot JSON encode resources');
}
// Clear json_last_error()
json_encode(null);
$json = json_encode($data, $encodingOptions);
if (JSON_ERROR_NONE !== json_last_error()) {
throw new InvalidArgumentException(sprintf('Unable to encode data to JSON in %s: %s', __CLASS__, json_last_error_msg()));
}
return $json;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
InjectContentTypeTrait:: |
private | function | Inject the provided Content-Type, if none is already present. | |
JsonResponse:: |
private | function | Encode the provided data to JSON. | |
JsonResponse:: |
public | function |
Create a JSON response with the given data. Overrides Response:: |
|
MessageTrait:: |
protected | property | Map of normalized header name to original name used to register header. | |
MessageTrait:: |
protected | property | List of all registered headers, as key => array of values. | |
MessageTrait:: |
private | property | ||
MessageTrait:: |
private | property | ||
MessageTrait:: |
private | function | Test that an array contains only strings | |
MessageTrait:: |
private static | function | Assert that the provided header values are valid. | |
MessageTrait:: |
private | function | Filter a set of headers to ensure they are in the correct internal format. | |
MessageTrait:: |
private static | function | Test if a value is a string | |
MessageTrait:: |
public | function | Gets the body of the message. | |
MessageTrait:: |
public | function | Retrieves a message header value by the given case-insensitive name. | 1 |
MessageTrait:: |
public | function | Retrieves a comma-separated string of the values for a single header. | |
MessageTrait:: |
public | function | Retrieves all message headers. | 1 |
MessageTrait:: |
public | function | Retrieves the HTTP protocol version as a string. | |
MessageTrait:: |
public | function | Checks if a header exists by the given case-insensitive name. | |
MessageTrait:: |
public | function | Return an instance with the specified header appended with the given value. | |
MessageTrait:: |
public | function | Return an instance with the specified message body. | |
MessageTrait:: |
public | function | Return an instance with the provided header, replacing any existing values of any headers with the same case-insensitive name. | |
MessageTrait:: |
public | function | Return an instance without the specified header. | |
MessageTrait:: |
public | function | Return an instance with the specified HTTP protocol version. | |
Response:: |
private | property | Map of standard HTTP status code/reason phrases | |
Response:: |
private | property | ||
Response:: |
private | property | ||
Response:: |
private | function | Ensure header names and values are valid. | |
Response:: |
public | function |
Gets the response reason phrase associated with the status code. Overrides ResponseInterface:: |
|
Response:: |
public | function |
Gets the response status code. Overrides ResponseInterface:: |
|
Response:: |
private | function | Validate a status code. | |
Response:: |
public | function |
Return an instance with the specified status code and, optionally, reason phrase. Overrides ResponseInterface:: |