Open Journal Systems  3.3.0
vendor/symfony/http-foundation/JsonResponse.php
1 <?php
2 
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
13 
25 class JsonResponse extends Response
26 {
27  protected $data;
28  protected $callback;
29 
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  const DEFAULT_ENCODING_OPTIONS = 15;
33 
35 
42  public function __construct($data = null, int $status = 200, array $headers = [], bool $json = false)
43  {
44  parent::__construct('', $status, $headers);
45 
46  if (null === $data) {
47  $data = new \ArrayObject();
48  }
49 
50  $json ? $this->setJson($data) : $this->setData($data);
51  }
52 
67  public static function create($data = null, $status = 200, $headers = [])
68  {
69  return new static($data, $status, $headers);
70  }
71 
86  public static function fromJsonString($data = null, $status = 200, $headers = [])
87  {
88  return new static($data, $status, $headers, true);
89  }
90 
100  public function setCallback($callback = null)
101  {
102  if (null !== $callback) {
103  // partially taken from https://geekality.net/2011/08/03/valid-javascript-identifier/
104  // partially taken from https://github.com/willdurand/JsonpCallbackValidator
105  // JsonpCallbackValidator is released under the MIT License. See https://github.com/willdurand/JsonpCallbackValidator/blob/v1.1.0/LICENSE for details.
106  // (c) William Durand <william.durand1@gmail.com>
107  $pattern = '/^[$_\p{L}][$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\x{200C}\x{200D}]*(?:\[(?:"(?:\\\.|[^"\\\])*"|\'(?:\\\.|[^\'\\\])*\'|\d+)\])*?$/u';
108  $reserved = [
109  'break', 'do', 'instanceof', 'typeof', 'case', 'else', 'new', 'var', 'catch', 'finally', 'return', 'void', 'continue', 'for', 'switch', 'while',
110  'debugger', 'function', 'this', 'with', 'default', 'if', 'throw', 'delete', 'in', 'try', 'class', 'enum', 'extends', 'super', 'const', 'export',
111  'import', 'implements', 'let', 'private', 'public', 'yield', 'interface', 'package', 'protected', 'static', 'null', 'true', 'false',
112  ];
113  $parts = explode('.', $callback);
114  foreach ($parts as $part) {
115  if (!preg_match($pattern, $part) || \in_array($part, $reserved, true)) {
116  throw new \InvalidArgumentException('The callback name is not valid.');
117  }
118  }
119  }
120 
121  $this->callback = $callback;
122 
123  return $this->update();
124  }
125 
135  public function setJson($json)
136  {
137  $this->data = $json;
138 
139  return $this->update();
140  }
141 
151  public function setData($data = [])
152  {
153  try {
154  $data = json_encode($data, $this->encodingOptions);
155  } catch (\Exception $e) {
156  if ('Exception' === \get_class($e) && 0 === strpos($e->getMessage(), 'Failed calling ')) {
157  throw $e->getPrevious() ?: $e;
158  }
159  throw $e;
160  }
161 
162  if (\PHP_VERSION_ID >= 70300 && (JSON_THROW_ON_ERROR & $this->encodingOptions)) {
163  return $this->setJson($data);
164  }
165 
166  if (JSON_ERROR_NONE !== json_last_error()) {
167  throw new \InvalidArgumentException(json_last_error_msg());
168  }
169 
170  return $this->setJson($data);
171  }
172 
178  public function getEncodingOptions()
179  {
180  return $this->encodingOptions;
181  }
182 
191  {
192  $this->encodingOptions = (int) $encodingOptions;
193 
194  return $this->setData(json_decode($this->data));
195  }
196 
202  protected function update()
203  {
204  if (null !== $this->callback) {
205  // Not using application/javascript for compatibility reasons with older browsers.
206  $this->headers->set('Content-Type', 'text/javascript');
207 
208  return $this->setContent(sprintf('/**/%s(%s);', $this->callback, $this->data));
209  }
210 
211  // Only set the header when there is none or when it equals 'text/javascript' (from a previous update with callback)
212  // in order to not overwrite a custom definition.
213  if (!$this->headers->has('Content-Type') || 'text/javascript' === $this->headers->get('Content-Type')) {
214  $this->headers->set('Content-Type', 'application/json');
215  }
216 
217  return $this->setContent($this->data);
218  }
219 }
Symfony\Component\HttpFoundation\Response\setContent
setContent($content)
Definition: lib/vendor/symfony/http-foundation/Response.php:424
Symfony\Component\HttpFoundation\JsonResponse\$encodingOptions
$encodingOptions
Definition: lib/vendor/symfony/http-foundation/JsonResponse.php:34
Symfony\Component\HttpFoundation\JsonResponse\setData
setData($data=[])
Definition: vendor/symfony/http-foundation/JsonResponse.php:151
Symfony\Component\HttpFoundation\JsonResponse\$callback
$callback
Definition: lib/vendor/symfony/http-foundation/JsonResponse.php:28
Symfony\Component\HttpFoundation\JsonResponse\getEncodingOptions
getEncodingOptions()
Definition: vendor/symfony/http-foundation/JsonResponse.php:178
Symfony\Component\HttpFoundation\JsonResponse\$data
$data
Definition: lib/vendor/symfony/http-foundation/JsonResponse.php:27
Symfony\Component\HttpFoundation\JsonResponse\create
static create($data=null, $status=200, $headers=[])
Definition: vendor/symfony/http-foundation/JsonResponse.php:67
Symfony\Component\HttpFoundation\JsonResponse\fromJsonString
static fromJsonString($data=null, $status=200, $headers=[])
Definition: vendor/symfony/http-foundation/JsonResponse.php:86
Symfony\Component\HttpFoundation\JsonResponse\setEncodingOptions
setEncodingOptions($encodingOptions)
Definition: vendor/symfony/http-foundation/JsonResponse.php:190
Symfony\Component\HttpFoundation\JsonResponse\__construct
__construct($data=null, int $status=200, array $headers=[], bool $json=false)
Definition: vendor/symfony/http-foundation/JsonResponse.php:42
Symfony\Component\HttpFoundation\JsonResponse\setCallback
setCallback($callback=null)
Definition: vendor/symfony/http-foundation/JsonResponse.php:100
Symfony\Component\HttpFoundation\Response\$headers
$headers
Definition: lib/vendor/symfony/http-foundation/Response.php:90
Symfony\Component\HttpFoundation\JsonResponse\setData
setData($data=array())
Definition: lib/vendor/symfony/http-foundation/JsonResponse.php:140
Symfony\Component\HttpFoundation\JsonResponse\update
update()
Definition: lib/vendor/symfony/http-foundation/JsonResponse.php:197
Symfony\Component\HttpFoundation\JsonResponse\setJson
setJson($json)
Definition: lib/vendor/symfony/http-foundation/JsonResponse.php:124
Symfony\Component\HttpFoundation
Definition: lib/vendor/symfony/http-foundation/AcceptHeader.php:12
Symfony\Component\HttpFoundation\JsonResponse\DEFAULT_ENCODING_OPTIONS
const DEFAULT_ENCODING_OPTIONS
Definition: lib/vendor/symfony/http-foundation/JsonResponse.php:32