annotate vendor/zendframework/zend-diactoros/src/Response/JsonResponse.php @ 12:7a779792577d

Update Drupal core to v8.4.5 (via Composer)
author Chris Cannam
date Fri, 23 Feb 2018 15:52:07 +0000
parents 4c8ae668cc8c
children c2387f117808
rev   line source
Chris@0 1 <?php
Chris@0 2 /**
Chris@12 3 * @see https://github.com/zendframework/zend-diactoros for the canonical source repository
Chris@12 4 * @copyright Copyright (c) 2015-2017 Zend Technologies USA Inc. (http://www.zend.com)
Chris@0 5 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
Chris@0 6 */
Chris@0 7
Chris@0 8 namespace Zend\Diactoros\Response;
Chris@0 9
Chris@0 10 use InvalidArgumentException;
Chris@0 11 use Zend\Diactoros\Response;
Chris@0 12 use Zend\Diactoros\Stream;
Chris@0 13
Chris@0 14 /**
Chris@0 15 * JSON response.
Chris@0 16 *
Chris@0 17 * Allows creating a response by passing data to the constructor; by default,
Chris@0 18 * serializes the data to JSON, sets a status code of 200 and sets the
Chris@0 19 * Content-Type header to application/json.
Chris@0 20 */
Chris@0 21 class JsonResponse extends Response
Chris@0 22 {
Chris@0 23 use InjectContentTypeTrait;
Chris@0 24
Chris@0 25 /**
Chris@0 26 * Default flags for json_encode; value of:
Chris@0 27 *
Chris@0 28 * <code>
Chris@0 29 * JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_SLASHES
Chris@0 30 * </code>
Chris@0 31 *
Chris@0 32 * @const int
Chris@0 33 */
Chris@0 34 const DEFAULT_JSON_FLAGS = 79;
Chris@0 35
Chris@0 36 /**
Chris@12 37 * @var mixed
Chris@12 38 */
Chris@12 39 private $payload;
Chris@12 40
Chris@12 41 /**
Chris@12 42 * @var int
Chris@12 43 */
Chris@12 44 private $encodingOptions;
Chris@12 45
Chris@12 46 /**
Chris@0 47 * Create a JSON response with the given data.
Chris@0 48 *
Chris@0 49 * Default JSON encoding is performed with the following options, which
Chris@0 50 * produces RFC4627-compliant JSON, capable of embedding into HTML.
Chris@0 51 *
Chris@0 52 * - JSON_HEX_TAG
Chris@0 53 * - JSON_HEX_APOS
Chris@0 54 * - JSON_HEX_AMP
Chris@0 55 * - JSON_HEX_QUOT
Chris@0 56 * - JSON_UNESCAPED_SLASHES
Chris@0 57 *
Chris@0 58 * @param mixed $data Data to convert to JSON.
Chris@0 59 * @param int $status Integer status code for the response; 200 by default.
Chris@0 60 * @param array $headers Array of headers to use at initialization.
Chris@0 61 * @param int $encodingOptions JSON encoding options to use.
Chris@0 62 * @throws InvalidArgumentException if unable to encode the $data to JSON.
Chris@0 63 */
Chris@0 64 public function __construct(
Chris@0 65 $data,
Chris@0 66 $status = 200,
Chris@0 67 array $headers = [],
Chris@0 68 $encodingOptions = self::DEFAULT_JSON_FLAGS
Chris@0 69 ) {
Chris@12 70 $this->setPayload($data);
Chris@12 71 $this->encodingOptions = $encodingOptions;
Chris@12 72
Chris@12 73 $json = $this->jsonEncode($data, $this->encodingOptions);
Chris@12 74 $body = $this->createBodyFromJson($json);
Chris@0 75
Chris@0 76 $headers = $this->injectContentType('application/json', $headers);
Chris@0 77
Chris@0 78 parent::__construct($body, $status, $headers);
Chris@0 79 }
Chris@0 80
Chris@0 81 /**
Chris@12 82 * @return mixed
Chris@12 83 */
Chris@12 84 public function getPayload()
Chris@12 85 {
Chris@12 86 return $this->payload;
Chris@12 87 }
Chris@12 88
Chris@12 89 /**
Chris@12 90 * @param $data
Chris@12 91 *
Chris@12 92 * @return JsonResponse
Chris@12 93 */
Chris@12 94 public function withPayload($data)
Chris@12 95 {
Chris@12 96 $new = clone $this;
Chris@12 97 $new->setPayload($data);
Chris@12 98 return $this->updateBodyFor($new);
Chris@12 99 }
Chris@12 100
Chris@12 101 /**
Chris@12 102 * @return int
Chris@12 103 */
Chris@12 104 public function getEncodingOptions()
Chris@12 105 {
Chris@12 106 return $this->encodingOptions;
Chris@12 107 }
Chris@12 108
Chris@12 109 /**
Chris@12 110 * @param int $encodingOptions
Chris@12 111 *
Chris@12 112 * @return JsonResponse
Chris@12 113 */
Chris@12 114 public function withEncodingOptions($encodingOptions)
Chris@12 115 {
Chris@12 116 $new = clone $this;
Chris@12 117 $new->encodingOptions = $encodingOptions;
Chris@12 118 return $this->updateBodyFor($new);
Chris@12 119 }
Chris@12 120
Chris@12 121 /**
Chris@12 122 * @param string $json
Chris@12 123 *
Chris@12 124 * @return Stream
Chris@12 125 */
Chris@12 126 private function createBodyFromJson($json)
Chris@12 127 {
Chris@12 128 $body = new Stream('php://temp', 'wb+');
Chris@12 129 $body->write($json);
Chris@12 130 $body->rewind();
Chris@12 131
Chris@12 132 return $body;
Chris@12 133 }
Chris@12 134
Chris@12 135 /**
Chris@0 136 * Encode the provided data to JSON.
Chris@0 137 *
Chris@0 138 * @param mixed $data
Chris@0 139 * @param int $encodingOptions
Chris@0 140 * @return string
Chris@0 141 * @throws InvalidArgumentException if unable to encode the $data to JSON.
Chris@0 142 */
Chris@0 143 private function jsonEncode($data, $encodingOptions)
Chris@0 144 {
Chris@0 145 if (is_resource($data)) {
Chris@0 146 throw new InvalidArgumentException('Cannot JSON encode resources');
Chris@0 147 }
Chris@0 148
Chris@0 149 // Clear json_last_error()
Chris@0 150 json_encode(null);
Chris@0 151
Chris@0 152 $json = json_encode($data, $encodingOptions);
Chris@0 153
Chris@0 154 if (JSON_ERROR_NONE !== json_last_error()) {
Chris@0 155 throw new InvalidArgumentException(sprintf(
Chris@0 156 'Unable to encode data to JSON in %s: %s',
Chris@0 157 __CLASS__,
Chris@0 158 json_last_error_msg()
Chris@0 159 ));
Chris@0 160 }
Chris@0 161
Chris@0 162 return $json;
Chris@0 163 }
Chris@12 164
Chris@12 165 /**
Chris@12 166 * @param $data
Chris@12 167 */
Chris@12 168 private function setPayload($data)
Chris@12 169 {
Chris@12 170 if (is_object($data)) {
Chris@12 171 $data = clone $data;
Chris@12 172 }
Chris@12 173
Chris@12 174 $this->payload = $data;
Chris@12 175 }
Chris@12 176
Chris@12 177 /**
Chris@12 178 * Update the response body for the given instance.
Chris@12 179 *
Chris@12 180 * @param self $toUpdate Instance to update.
Chris@12 181 * @return JsonResponse Returns a new instance with an updated body.
Chris@12 182 */
Chris@12 183 private function updateBodyFor(self $toUpdate)
Chris@12 184 {
Chris@12 185 $json = $this->jsonEncode($toUpdate->payload, $toUpdate->encodingOptions);
Chris@12 186 $body = $this->createBodyFromJson($json);
Chris@12 187 return $toUpdate->withBody($body);
Chris@12 188 }
Chris@0 189 }