Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/http-foundation/JsonResponse.php @ 17:129ea1e6d783
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:21:36 +0000 |
parents | 1fec387a4317 |
children |
comparison
equal
deleted
inserted
replaced
16:c2387f117808 | 17:129ea1e6d783 |
---|---|
37 * @param mixed $data The response data | 37 * @param mixed $data The response data |
38 * @param int $status The response status code | 38 * @param int $status The response status code |
39 * @param array $headers An array of response headers | 39 * @param array $headers An array of response headers |
40 * @param bool $json If the data is already a JSON string | 40 * @param bool $json If the data is already a JSON string |
41 */ | 41 */ |
42 public function __construct($data = null, $status = 200, $headers = array(), $json = false) | 42 public function __construct($data = null, $status = 200, $headers = [], $json = false) |
43 { | 43 { |
44 parent::__construct('', $status, $headers); | 44 parent::__construct('', $status, $headers); |
45 | 45 |
46 if (null === $data) { | 46 if (null === $data) { |
47 $data = new \ArrayObject(); | 47 $data = new \ArrayObject(); |
62 * @param int $status The response status code | 62 * @param int $status The response status code |
63 * @param array $headers An array of response headers | 63 * @param array $headers An array of response headers |
64 * | 64 * |
65 * @return static | 65 * @return static |
66 */ | 66 */ |
67 public static function create($data = null, $status = 200, $headers = array()) | 67 public static function create($data = null, $status = 200, $headers = []) |
68 { | 68 { |
69 return new static($data, $status, $headers); | 69 return new static($data, $status, $headers); |
70 } | 70 } |
71 | 71 |
72 /** | 72 /** |
73 * Make easier the creation of JsonResponse from raw json. | 73 * Make easier the creation of JsonResponse from raw json. |
74 */ | 74 */ |
75 public static function fromJsonString($data = null, $status = 200, $headers = array()) | 75 public static function fromJsonString($data = null, $status = 200, $headers = []) |
76 { | 76 { |
77 return new static($data, $status, $headers, true); | 77 return new static($data, $status, $headers, true); |
78 } | 78 } |
79 | 79 |
80 /** | 80 /** |
92 // partially taken from http://www.geekality.net/2011/08/03/valid-javascript-identifier/ | 92 // partially taken from http://www.geekality.net/2011/08/03/valid-javascript-identifier/ |
93 // partially taken from https://github.com/willdurand/JsonpCallbackValidator | 93 // partially taken from https://github.com/willdurand/JsonpCallbackValidator |
94 // JsonpCallbackValidator is released under the MIT License. See https://github.com/willdurand/JsonpCallbackValidator/blob/v1.1.0/LICENSE for details. | 94 // JsonpCallbackValidator is released under the MIT License. See https://github.com/willdurand/JsonpCallbackValidator/blob/v1.1.0/LICENSE for details. |
95 // (c) William Durand <william.durand1@gmail.com> | 95 // (c) William Durand <william.durand1@gmail.com> |
96 $pattern = '/^[$_\p{L}][$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\x{200C}\x{200D}]*(?:\[(?:"(?:\\\.|[^"\\\])*"|\'(?:\\\.|[^\'\\\])*\'|\d+)\])*?$/u'; | 96 $pattern = '/^[$_\p{L}][$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\x{200C}\x{200D}]*(?:\[(?:"(?:\\\.|[^"\\\])*"|\'(?:\\\.|[^\'\\\])*\'|\d+)\])*?$/u'; |
97 $reserved = array( | 97 $reserved = [ |
98 'break', 'do', 'instanceof', 'typeof', 'case', 'else', 'new', 'var', 'catch', 'finally', 'return', 'void', 'continue', 'for', 'switch', 'while', | 98 'break', 'do', 'instanceof', 'typeof', 'case', 'else', 'new', 'var', 'catch', 'finally', 'return', 'void', 'continue', 'for', 'switch', 'while', |
99 'debugger', 'function', 'this', 'with', 'default', 'if', 'throw', 'delete', 'in', 'try', 'class', 'enum', 'extends', 'super', 'const', 'export', | 99 'debugger', 'function', 'this', 'with', 'default', 'if', 'throw', 'delete', 'in', 'try', 'class', 'enum', 'extends', 'super', 'const', 'export', |
100 'import', 'implements', 'let', 'private', 'public', 'yield', 'interface', 'package', 'protected', 'static', 'null', 'true', 'false', | 100 'import', 'implements', 'let', 'private', 'public', 'yield', 'interface', 'package', 'protected', 'static', 'null', 'true', 'false', |
101 ); | 101 ]; |
102 $parts = explode('.', $callback); | 102 $parts = explode('.', $callback); |
103 foreach ($parts as $part) { | 103 foreach ($parts as $part) { |
104 if (!preg_match($pattern, $part) || in_array($part, $reserved, true)) { | 104 if (!preg_match($pattern, $part) || \in_array($part, $reserved, true)) { |
105 throw new \InvalidArgumentException('The callback name is not valid.'); | 105 throw new \InvalidArgumentException('The callback name is not valid.'); |
106 } | 106 } |
107 } | 107 } |
108 } | 108 } |
109 | 109 |
135 * | 135 * |
136 * @return $this | 136 * @return $this |
137 * | 137 * |
138 * @throws \InvalidArgumentException | 138 * @throws \InvalidArgumentException |
139 */ | 139 */ |
140 public function setData($data = array()) | 140 public function setData($data = []) |
141 { | 141 { |
142 if (defined('HHVM_VERSION')) { | 142 if (\defined('HHVM_VERSION')) { |
143 // HHVM does not trigger any warnings and let exceptions | 143 // HHVM does not trigger any warnings and let exceptions |
144 // thrown from a JsonSerializable object pass through. | 144 // thrown from a JsonSerializable object pass through. |
145 // If only PHP did the same... | 145 // If only PHP did the same... |
146 $data = json_encode($data, $this->encodingOptions); | 146 $data = json_encode($data, $this->encodingOptions); |
147 } else { | 147 } else { |
154 } | 154 } |
155 } else { | 155 } else { |
156 try { | 156 try { |
157 $data = json_encode($data, $this->encodingOptions); | 157 $data = json_encode($data, $this->encodingOptions); |
158 } catch (\Exception $e) { | 158 } catch (\Exception $e) { |
159 if ('Exception' === get_class($e) && 0 === strpos($e->getMessage(), 'Failed calling ')) { | 159 if ('Exception' === \get_class($e) && 0 === strpos($e->getMessage(), 'Failed calling ')) { |
160 throw $e->getPrevious() ?: $e; | 160 throw $e->getPrevious() ?: $e; |
161 } | 161 } |
162 throw $e; | 162 throw $e; |
163 } | 163 } |
164 } | 164 } |