composer package updates
[openemr.git] / vendor / zendframework / zend-diactoros / src / Response / JsonResponse.php
blobb8a032a3b2738a5dbe6ba0c923cf1342e0b49d6b
1 <?php
2 /**
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
6 */
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;
19 use function sprintf;
21 use const JSON_ERROR_NONE;
23 /**
24 * JSON response.
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;
34 /**
35 * Default flags for json_encode; value of:
37 * <code>
38 * JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_SLASHES
39 * </code>
41 * @const int
43 const DEFAULT_JSON_FLAGS = 79;
45 /**
46 * @var mixed
48 private $payload;
50 /**
51 * @var int
53 private $encodingOptions;
55 /**
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.
61 * - JSON_HEX_TAG
62 * - JSON_HEX_APOS
63 * - JSON_HEX_AMP
64 * - JSON_HEX_QUOT
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(
74 $data,
75 $status = 200,
76 array $headers = [],
77 $encodingOptions = self::DEFAULT_JSON_FLAGS
78 ) {
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);
90 /**
91 * @return mixed
93 public function getPayload()
95 return $this->payload;
98 /**
99 * @param $data
101 * @return JsonResponse
103 public function withPayload($data)
105 $new = clone $this;
106 $new->setPayload($data);
107 return $this->updateBodyFor($new);
111 * @return int
113 public function getEncodingOptions()
115 return $this->encodingOptions;
119 * @param int $encodingOptions
121 * @return JsonResponse
123 public function withEncodingOptions($encodingOptions)
125 $new = clone $this;
126 $new->encodingOptions = $encodingOptions;
127 return $this->updateBodyFor($new);
131 * @param string $json
133 * @return Stream
135 private function createBodyFromJson($json)
137 $body = new Stream('php://temp', 'wb+');
138 $body->write($json);
139 $body->rewind();
141 return $body;
145 * Encode the provided data to JSON.
147 * @param mixed $data
148 * @param int $encodingOptions
149 * @return string
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()
159 json_encode(null);
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',
166 __CLASS__,
167 json_last_error_msg()
171 return $json;
175 * @param $data
177 private function setPayload($data)
179 if (is_object($data)) {
180 $data = clone $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);