3 * @see https://github.com/zendframework/zend-diactoros for the canonical source repository
4 * @copyright Copyright (c) 2015-2017 Zend Technologies USA Inc. (http://www.zend.com)
5 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
8 namespace Zend\Diactoros\Response
;
10 use InvalidArgumentException
;
11 use Zend\Diactoros\Response
;
12 use Zend\Diactoros\Stream
;
17 * Allows creating a response by passing data to the constructor; by default,
18 * serializes the data to JSON, sets a status code of 200 and sets the
19 * Content-Type header to application/json.
21 class JsonResponse
extends Response
23 use InjectContentTypeTrait
;
26 * Default flags for json_encode; value of:
29 * JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_SLASHES
34 const DEFAULT_JSON_FLAGS
= 79;
44 private $encodingOptions;
47 * Create a JSON response with the given data.
49 * Default JSON encoding is performed with the following options, which
50 * produces RFC4627-compliant JSON, capable of embedding into HTML.
56 * - JSON_UNESCAPED_SLASHES
58 * @param mixed $data Data to convert to JSON.
59 * @param int $status Integer status code for the response; 200 by default.
60 * @param array $headers Array of headers to use at initialization.
61 * @param int $encodingOptions JSON encoding options to use.
62 * @throws InvalidArgumentException if unable to encode the $data to JSON.
64 public function __construct(
68 $encodingOptions = self
::DEFAULT_JSON_FLAGS
70 $this->setPayload($data);
71 $this->encodingOptions
= $encodingOptions;
73 $json = $this->jsonEncode($data, $this->encodingOptions
);
74 $body = $this->createBodyFromJson($json);
76 $headers = $this->injectContentType('application/json', $headers);
78 parent
::__construct($body, $status, $headers);
84 public function getPayload()
86 return $this->payload
;
92 * @return JsonResponse
94 public function withPayload($data)
97 $new->setPayload($data);
98 return $this->updateBodyFor($new);
104 public function getEncodingOptions()
106 return $this->encodingOptions
;
110 * @param int $encodingOptions
112 * @return JsonResponse
114 public function withEncodingOptions($encodingOptions)
117 $new->encodingOptions
= $encodingOptions;
118 return $this->updateBodyFor($new);
122 * @param string $json
126 private function createBodyFromJson($json)
128 $body = new Stream('php://temp', 'wb+');
136 * Encode the provided data to JSON.
139 * @param int $encodingOptions
141 * @throws InvalidArgumentException if unable to encode the $data to JSON.
143 private function jsonEncode($data, $encodingOptions)
145 if (is_resource($data)) {
146 throw new InvalidArgumentException('Cannot JSON encode resources');
149 // Clear json_last_error()
152 $json = json_encode($data, $encodingOptions);
154 if (JSON_ERROR_NONE
!== json_last_error()) {
155 throw new InvalidArgumentException(sprintf(
156 'Unable to encode data to JSON in %s: %s',
158 json_last_error_msg()
168 private function setPayload($data)
170 if (is_object($data)) {
174 $this->payload
= $data;
178 * Update the response body for the given instance.
180 * @param self $toUpdate Instance to update.
181 * @return JsonResponse Returns a new instance with an updated body.
183 private function updateBodyFor(self
$toUpdate)
185 $json = $this->jsonEncode($toUpdate->payload
, $toUpdate->encodingOptions
);
186 $body = $this->createBodyFromJson($json);
187 return $toUpdate->withBody($body);