Mercurial > hg > isophonics-drupal-site
diff 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 |
line wrap: on
line diff
--- a/vendor/zendframework/zend-diactoros/src/Response/JsonResponse.php Fri Feb 23 15:51:18 2018 +0000 +++ b/vendor/zendframework/zend-diactoros/src/Response/JsonResponse.php Fri Feb 23 15:52:07 2018 +0000 @@ -1,9 +1,7 @@ <?php /** - * Zend Framework (http://framework.zend.com/) - * - * @see http://github.com/zendframework/zend-diactoros for the canonical source repository - * @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (http://www.zend.com) + * @see https://github.com/zendframework/zend-diactoros for the canonical source repository + * @copyright Copyright (c) 2015-2017 Zend Technologies USA Inc. (http://www.zend.com) * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License */ @@ -36,6 +34,16 @@ const DEFAULT_JSON_FLAGS = 79; /** + * @var mixed + */ + private $payload; + + /** + * @var int + */ + private $encodingOptions; + + /** * Create a JSON response with the given data. * * Default JSON encoding is performed with the following options, which @@ -59,9 +67,11 @@ array $headers = [], $encodingOptions = self::DEFAULT_JSON_FLAGS ) { - $body = new Stream('php://temp', 'wb+'); - $body->write($this->jsonEncode($data, $encodingOptions)); - $body->rewind(); + $this->setPayload($data); + $this->encodingOptions = $encodingOptions; + + $json = $this->jsonEncode($data, $this->encodingOptions); + $body = $this->createBodyFromJson($json); $headers = $this->injectContentType('application/json', $headers); @@ -69,6 +79,60 @@ } /** + * @return mixed + */ + public function getPayload() + { + return $this->payload; + } + + /** + * @param $data + * + * @return JsonResponse + */ + public function withPayload($data) + { + $new = clone $this; + $new->setPayload($data); + return $this->updateBodyFor($new); + } + + /** + * @return int + */ + public function getEncodingOptions() + { + return $this->encodingOptions; + } + + /** + * @param int $encodingOptions + * + * @return JsonResponse + */ + public function withEncodingOptions($encodingOptions) + { + $new = clone $this; + $new->encodingOptions = $encodingOptions; + return $this->updateBodyFor($new); + } + + /** + * @param string $json + * + * @return Stream + */ + private function createBodyFromJson($json) + { + $body = new Stream('php://temp', 'wb+'); + $body->write($json); + $body->rewind(); + + return $body; + } + + /** * Encode the provided data to JSON. * * @param mixed $data @@ -97,4 +161,29 @@ return $json; } + + /** + * @param $data + */ + private function setPayload($data) + { + if (is_object($data)) { + $data = clone $data; + } + + $this->payload = $data; + } + + /** + * Update the response body for the given instance. + * + * @param self $toUpdate Instance to update. + * @return JsonResponse Returns a new instance with an updated body. + */ + private function updateBodyFor(self $toUpdate) + { + $json = $this->jsonEncode($toUpdate->payload, $toUpdate->encodingOptions); + $body = $this->createBodyFromJson($json); + return $toUpdate->withBody($body); + } }