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
;
14 use function is_object
;
15 use function is_resource
;
16 use function json_encode
;
17 use function json_last_error
;
18 use function json_last_error_msg
;
21 use const JSON_ERROR_NONE
;
26 * Allows creating a response by passing data to the constructor; by default,
27 * serializes the data to JSON, sets a status code of 200 and sets the
28 * Content-Type header to application/json.
30 class JsonResponse
extends Response
32 use InjectContentTypeTrait
;
35 * Default flags for json_encode; value of:
38 * JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_SLASHES
43 const DEFAULT_JSON_FLAGS
= 79;
53 private $encodingOptions;
56 * Create a JSON response with the given data.
58 * Default JSON encoding is performed with the following options, which
59 * produces RFC4627-compliant JSON, capable of embedding into HTML.
65 * - JSON_UNESCAPED_SLASHES
67 * @param mixed $data Data to convert to JSON.
68 * @param int $status Integer status code for the response; 200 by default.
69 * @param array $headers Array of headers to use at initialization.
70 * @param int $encodingOptions JSON encoding options to use.
71 * @throws InvalidArgumentException if unable to encode the $data to JSON.
73 public function __construct(
77 $encodingOptions = self
::DEFAULT_JSON_FLAGS
79 $this->setPayload($data);
80 $this->encodingOptions
= $encodingOptions;
82 $json = $this->jsonEncode($data, $this->encodingOptions
);
83 $body = $this->createBodyFromJson($json);
85 $headers = $this->injectContentType('application/json', $headers);
87 parent
::__construct($body, $status, $headers);
93 public function getPayload()
95 return $this->payload
;
101 * @return JsonResponse
103 public function withPayload($data)
106 $new->setPayload($data);
107 return $this->updateBodyFor($new);
113 public function getEncodingOptions()
115 return $this->encodingOptions
;
119 * @param int $encodingOptions
121 * @return JsonResponse
123 public function withEncodingOptions($encodingOptions)
126 $new->encodingOptions
= $encodingOptions;
127 return $this->updateBodyFor($new);
131 * @param string $json
135 private function createBodyFromJson($json)
137 $body = new Stream('php://temp', 'wb+');
145 * Encode the provided data to JSON.
148 * @param int $encodingOptions
150 * @throws InvalidArgumentException if unable to encode the $data to JSON.
152 private function jsonEncode($data, $encodingOptions)
154 if (is_resource($data)) {
155 throw new InvalidArgumentException('Cannot JSON encode resources');
158 // Clear json_last_error()
161 $json = json_encode($data, $encodingOptions);
163 if (JSON_ERROR_NONE
!== json_last_error()) {
164 throw new InvalidArgumentException(sprintf(
165 'Unable to encode data to JSON in %s: %s',
167 json_last_error_msg()
177 private function setPayload($data)
179 if (is_object($data)) {
183 $this->payload
= $data;
187 * Update the response body for the given instance.
189 * @param self $toUpdate Instance to update.
190 * @return JsonResponse Returns a new instance with an updated body.
192 private function updateBodyFor(self
$toUpdate)
194 $json = $this->jsonEncode($toUpdate->payload
, $toUpdate->encodingOptions
);
195 $body = $this->createBodyFromJson($json);
196 return $toUpdate->withBody($body);