c8af7c4c2aba23b013456788352e64c6b2b78f2f
[yaffs-website] / vendor / zendframework / zend-diactoros / src / Response / JsonResponse.php
1 <?php
2 /**
3  * Zend Framework (http://framework.zend.com/)
4  *
5  * @see       http://github.com/zendframework/zend-diactoros for the canonical source repository
6  * @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (http://www.zend.com)
7  * @license   https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
8  */
9
10 namespace Zend\Diactoros\Response;
11
12 use InvalidArgumentException;
13 use Zend\Diactoros\Response;
14 use Zend\Diactoros\Stream;
15
16 /**
17  * JSON response.
18  *
19  * Allows creating a response by passing data to the constructor; by default,
20  * serializes the data to JSON, sets a status code of 200 and sets the
21  * Content-Type header to application/json.
22  */
23 class JsonResponse extends Response
24 {
25     use InjectContentTypeTrait;
26
27     /**
28      * Default flags for json_encode; value of:
29      *
30      * <code>
31      * JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_SLASHES
32      * </code>
33      *
34      * @const int
35      */
36     const DEFAULT_JSON_FLAGS = 79;
37
38     /**
39      * Create a JSON response with the given data.
40      *
41      * Default JSON encoding is performed with the following options, which
42      * produces RFC4627-compliant JSON, capable of embedding into HTML.
43      *
44      * - JSON_HEX_TAG
45      * - JSON_HEX_APOS
46      * - JSON_HEX_AMP
47      * - JSON_HEX_QUOT
48      * - JSON_UNESCAPED_SLASHES
49      *
50      * @param mixed $data Data to convert to JSON.
51      * @param int $status Integer status code for the response; 200 by default.
52      * @param array $headers Array of headers to use at initialization.
53      * @param int $encodingOptions JSON encoding options to use.
54      * @throws InvalidArgumentException if unable to encode the $data to JSON.
55      */
56     public function __construct(
57         $data,
58         $status = 200,
59         array $headers = [],
60         $encodingOptions = self::DEFAULT_JSON_FLAGS
61     ) {
62         $body = new Stream('php://temp', 'wb+');
63         $body->write($this->jsonEncode($data, $encodingOptions));
64         $body->rewind();
65
66         $headers = $this->injectContentType('application/json', $headers);
67
68         parent::__construct($body, $status, $headers);
69     }
70
71     /**
72      * Encode the provided data to JSON.
73      *
74      * @param mixed $data
75      * @param int $encodingOptions
76      * @return string
77      * @throws InvalidArgumentException if unable to encode the $data to JSON.
78      */
79     private function jsonEncode($data, $encodingOptions)
80     {
81         if (is_resource($data)) {
82             throw new InvalidArgumentException('Cannot JSON encode resources');
83         }
84
85         // Clear json_last_error()
86         json_encode(null);
87
88         $json = json_encode($data, $encodingOptions);
89
90         if (JSON_ERROR_NONE !== json_last_error()) {
91             throw new InvalidArgumentException(sprintf(
92                 'Unable to encode data to JSON in %s: %s',
93                 __CLASS__,
94                 json_last_error_msg()
95             ));
96         }
97
98         return $json;
99     }
100 }