composer package updates
[openemr.git] / vendor / symfony / http-foundation / JsonResponse.php
blobd3695d1535c4c2cb6619e6b13e43199554c7e6f3
1 <?php
3 /*
4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation;
14 /**
15 * Response represents an HTTP response in JSON format.
17 * Note that this class does not force the returned JSON content to be an
18 * object. It is however recommended that you do return an object as it
19 * protects yourself against XSSI and JSON-JavaScript Hijacking.
21 * @see https://www.owasp.org/index.php/OWASP_AJAX_Security_Guidelines#Always_return_JSON_with_an_Object_on_the_outside
23 * @author Igor Wiedler <igor@wiedler.ch>
25 class JsonResponse extends Response
27 protected $data;
28 protected $callback;
30 // Encode <, >, ', &, and " characters in the JSON, making it also safe to be embedded into HTML.
31 // 15 === JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT
32 protected $encodingOptions = 15;
34 /**
35 * @param mixed $data The response data
36 * @param int $status The response status code
37 * @param array $headers An array of response headers
39 public function __construct($data = null, $status = 200, $headers = array())
41 parent::__construct('', $status, $headers);
43 if (null === $data) {
44 $data = new \ArrayObject();
47 $this->setData($data);
50 /**
51 * Factory method for chainability.
53 * Example:
55 * return JsonResponse::create($data, 200)
56 * ->setSharedMaxAge(300);
58 * @param mixed $data The json response data
59 * @param int $status The response status code
60 * @param array $headers An array of response headers
62 * @return static
64 public static function create($data = null, $status = 200, $headers = array())
66 return new static($data, $status, $headers);
69 /**
70 * Sets the JSONP callback.
72 * @param string|null $callback The JSONP callback or null to use none
74 * @return $this
76 * @throws \InvalidArgumentException When the callback name is not valid
78 public function setCallback($callback = null)
80 if (null !== $callback) {
81 // partially token from http://www.geekality.net/2011/08/03/valid-javascript-identifier/
82 // partially token from https://github.com/willdurand/JsonpCallbackValidator
83 // JsonpCallbackValidator is released under the MIT License. See https://github.com/willdurand/JsonpCallbackValidator/blob/v1.1.0/LICENSE for details.
84 // (c) William Durand <william.durand1@gmail.com>
85 $pattern = '/^[$_\p{L}][$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\x{200C}\x{200D}]*(?:\[(?:"(?:\\\.|[^"\\\])*"|\'(?:\\\.|[^\'\\\])*\'|\d+)\])*?$/u';
86 $reserved = array(
87 'break', 'do', 'instanceof', 'typeof', 'case', 'else', 'new', 'var', 'catch', 'finally', 'return', 'void', 'continue', 'for', 'switch', 'while',
88 'debugger', 'function', 'this', 'with', 'default', 'if', 'throw', 'delete', 'in', 'try', 'class', 'enum', 'extends', 'super', 'const', 'export',
89 'import', 'implements', 'let', 'private', 'public', 'yield', 'interface', 'package', 'protected', 'static', 'null', 'true', 'false',
91 $parts = explode('.', $callback);
92 foreach ($parts as $part) {
93 if (!preg_match($pattern, $part) || in_array($part, $reserved, true)) {
94 throw new \InvalidArgumentException('The callback name is not valid.');
99 $this->callback = $callback;
101 return $this->update();
105 * Sets the data to be sent as JSON.
107 * @param mixed $data
109 * @return $this
111 * @throws \InvalidArgumentException
113 public function setData($data = array())
115 if (defined('HHVM_VERSION')) {
116 // HHVM does not trigger any warnings and let exceptions
117 // thrown from a JsonSerializable object pass through.
118 // If only PHP did the same...
119 $data = json_encode($data, $this->encodingOptions);
120 } else {
121 try {
122 if (!interface_exists('JsonSerializable', false)) {
123 // PHP 5.3 triggers annoying warnings for some
124 // types that can't be serialized as JSON (INF, resources, etc.)
125 // but doesn't provide the JsonSerializable interface.
126 set_error_handler(function () { return false; });
127 $data = @json_encode($data, $this->encodingOptions);
128 restore_error_handler();
129 } elseif (\PHP_VERSION_ID < 50500) {
130 // PHP 5.4 and up wrap exceptions thrown by JsonSerializable
131 // objects in a new exception that needs to be removed.
132 // Fortunately, PHP 5.5 and up do not trigger any warning anymore.
133 // Clear json_last_error()
134 json_encode(null);
135 $errorHandler = set_error_handler('var_dump');
136 restore_error_handler();
137 set_error_handler(function () use ($errorHandler) {
138 if (JSON_ERROR_NONE === json_last_error()) {
139 return $errorHandler && false !== call_user_func_array($errorHandler, func_get_args());
142 $data = json_encode($data, $this->encodingOptions);
143 restore_error_handler();
144 } else {
145 $data = json_encode($data, $this->encodingOptions);
147 } catch (\Error $e) {
148 if (\PHP_VERSION_ID < 50500 || !interface_exists('JsonSerializable', false)) {
149 restore_error_handler();
151 throw $e;
152 } catch (\Exception $e) {
153 if (\PHP_VERSION_ID < 50500 || !interface_exists('JsonSerializable', false)) {
154 restore_error_handler();
156 if (interface_exists('JsonSerializable', false) && 'Exception' === get_class($e) && 0 === strpos($e->getMessage(), 'Failed calling ')) {
157 throw $e->getPrevious() ?: $e;
159 throw $e;
163 if (JSON_ERROR_NONE !== json_last_error()) {
164 throw new \InvalidArgumentException(json_last_error_msg());
167 $this->data = $data;
169 return $this->update();
173 * Returns options used while encoding data to JSON.
175 * @return int
177 public function getEncodingOptions()
179 return $this->encodingOptions;
183 * Sets options used while encoding data to JSON.
185 * @param int $encodingOptions
187 * @return $this
189 public function setEncodingOptions($encodingOptions)
191 $this->encodingOptions = (int) $encodingOptions;
193 return $this->setData(json_decode($this->data));
197 * Updates the content and headers according to the JSON data and callback.
199 * @return $this
201 protected function update()
203 if (null !== $this->callback) {
204 // Not using application/javascript for compatibility reasons with older browsers.
205 $this->headers->set('Content-Type', 'text/javascript');
207 return $this->setContent(sprintf('/**/%s(%s);', $this->callback, $this->data));
210 // Only set the header when there is none or when it equals 'text/javascript' (from a previous update with callback)
211 // in order to not overwrite a custom definition.
212 if (!$this->headers->has('Content-Type') || 'text/javascript' === $this->headers->get('Content-Type')) {
213 $this->headers->set('Content-Type', 'application/json');
216 return $this->setContent($this->data);