Open Journal Systems  3.3.0
lib/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
33 
35 
42  public function __construct($data = null, $status = 200, $headers = array(), $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 = array())
68  {
69  return new static($data, $status, $headers);
70  }
71 
75  public static function fromJsonString($data = null, $status = 200, $headers = array())
76  {
77  return new static($data, $status, $headers, true);
78  }
79 
89  public function setCallback($callback = null)
90  {
91  if (null !== $callback) {
92  // partially taken from http://www.geekality.net/2011/08/03/valid-javascript-identifier/
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.
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';
97  $reserved = array(
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',
100  'import', 'implements', 'let', 'private', 'public', 'yield', 'interface', 'package', 'protected', 'static', 'null', 'true', 'false',
101  );
102  $parts = explode('.', $callback);
103  foreach ($parts as $part) {
104  if (!preg_match($pattern, $part) || in_array($part, $reserved, true)) {
105  throw new \InvalidArgumentException('The callback name is not valid.');
106  }
107  }
108  }
109 
110  $this->callback = $callback;
111 
112  return $this->update();
113  }
114 
124  public function setJson($json)
125  {
126  $this->data = $json;
127 
128  return $this->update();
129  }
130 
140  public function setData($data = array())
141  {
142  if (defined('HHVM_VERSION')) {
143  // HHVM does not trigger any warnings and let exceptions
144  // thrown from a JsonSerializable object pass through.
145  // If only PHP did the same...
146  $data = json_encode($data, $this->encodingOptions);
147  } else {
148  try {
149  // PHP 5.4 and up wrap exceptions thrown by JsonSerializable
150  // objects in a new exception that needs to be removed.
151  // Fortunately, PHP 5.5 and up do not trigger any warning anymore.
152  $data = json_encode($data, $this->encodingOptions);
153  } catch (\Exception $e) {
154  if ('Exception' === get_class($e) && 0 === strpos($e->getMessage(), 'Failed calling ')) {
155  throw $e->getPrevious() ?: $e;
156  }
157  throw $e;
158  }
159  }
160 
161  if (JSON_ERROR_NONE !== json_last_error()) {
162  throw new \InvalidArgumentException(json_last_error_msg());
163  }
164 
165  return $this->setJson($data);
166  }
167 
173  public function getEncodingOptions()
174  {
175  return $this->encodingOptions;
176  }
177 
186  {
187  $this->encodingOptions = (int) $encodingOptions;
188 
189  return $this->setData(json_decode($this->data));
190  }
191 
197  protected function update()
198  {
199  if (null !== $this->callback) {
200  // Not using application/javascript for compatibility reasons with older browsers.
201  $this->headers->set('Content-Type', 'text/javascript');
202 
203  return $this->setContent(sprintf('/**/%s(%s);', $this->callback, $this->data));
204  }
205 
206  // Only set the header when there is none or when it equals 'text/javascript' (from a previous update with callback)
207  // in order to not overwrite a custom definition.
208  if (!$this->headers->has('Content-Type') || 'text/javascript' === $this->headers->get('Content-Type')) {
209  $this->headers->set('Content-Type', 'application/json');
210  }
211 
212  return $this->setContent($this->data);
213  }
214 }
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\Response
Definition: lib/vendor/symfony/http-foundation/Response.php:19
Symfony\Component\HttpFoundation\JsonResponse\$callback
$callback
Definition: lib/vendor/symfony/http-foundation/JsonResponse.php:28
Symfony\Component\HttpFoundation\JsonResponse\getEncodingOptions
getEncodingOptions()
Definition: lib/vendor/symfony/http-foundation/JsonResponse.php:173
Symfony\Component\HttpFoundation\JsonResponse\$data
$data
Definition: lib/vendor/symfony/http-foundation/JsonResponse.php:27
Symfony\Component\HttpFoundation\JsonResponse\setEncodingOptions
setEncodingOptions($encodingOptions)
Definition: lib/vendor/symfony/http-foundation/JsonResponse.php:185
Symfony\Component\HttpFoundation\JsonResponse\setCallback
setCallback($callback=null)
Definition: lib/vendor/symfony/http-foundation/JsonResponse.php:89
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\fromJsonString
static fromJsonString($data=null, $status=200, $headers=array())
Definition: lib/vendor/symfony/http-foundation/JsonResponse.php:75
Symfony\Component\HttpFoundation\JsonResponse\update
update()
Definition: lib/vendor/symfony/http-foundation/JsonResponse.php:197
Symfony\Component\HttpFoundation\JsonResponse\__construct
__construct($data=null, $status=200, $headers=array(), $json=false)
Definition: lib/vendor/symfony/http-foundation/JsonResponse.php:42
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\create
static create($data=null, $status=200, $headers=array())
Definition: lib/vendor/symfony/http-foundation/JsonResponse.php:67
Symfony\Component\HttpFoundation\JsonResponse\DEFAULT_ENCODING_OPTIONS
const DEFAULT_ENCODING_OPTIONS
Definition: lib/vendor/symfony/http-foundation/JsonResponse.php:32
Symfony\Component\HttpFoundation\JsonResponse
Definition: lib/vendor/symfony/http-foundation/JsonResponse.php:25