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