123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- <?php
- namespace Symfony\Component\HttpFoundation;
- class JsonResponse extends Response
- {
- protected $data;
- protected $callback;
-
-
- protected $encodingOptions = 15;
-
- public function __construct($data = null, $status = 200, $headers = array())
- {
- parent::__construct('', $status, $headers);
- if (null === $data) {
- $data = new \ArrayObject();
- }
- $this->setData($data);
- }
-
- public static function create($data = null, $status = 200, $headers = array())
- {
- return new static($data, $status, $headers);
- }
-
- public function setCallback($callback = null)
- {
- if (null !== $callback) {
-
- $pattern = '/^[$_\p{L}][$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\x{200C}\x{200D}]*+$/u';
- $parts = explode('.', $callback);
- foreach ($parts as $part) {
- if (!preg_match($pattern, $part)) {
- throw new \InvalidArgumentException('The callback name is not valid.');
- }
- }
- }
- $this->callback = $callback;
- return $this->update();
- }
-
- public function setData($data = array())
- {
- if (defined('HHVM_VERSION')) {
-
-
-
- $data = json_encode($data, $this->encodingOptions);
- } else {
- try {
- if (PHP_VERSION_ID < 50400) {
-
-
-
- set_error_handler(function () { return false; });
- $data = @json_encode($data, $this->encodingOptions);
- } else {
-
-
-
- if (PHP_VERSION_ID < 50500) {
-
- json_encode(null);
- $errorHandler = set_error_handler('var_dump');
- restore_error_handler();
- set_error_handler(function () use ($errorHandler) {
- if (JSON_ERROR_NONE === json_last_error()) {
- return $errorHandler && false !== call_user_func_array($errorHandler, func_get_args());
- }
- });
- }
- $data = json_encode($data, $this->encodingOptions);
- }
- if (PHP_VERSION_ID < 50500) {
- restore_error_handler();
- }
- } catch (\Exception $e) {
- if (PHP_VERSION_ID < 50500) {
- restore_error_handler();
- }
- if (PHP_VERSION_ID >= 50400 && 'Exception' === get_class($e) && 0 === strpos($e->getMessage(), 'Failed calling ')) {
- throw $e->getPrevious() ?: $e;
- }
- throw $e;
- }
- }
- if (JSON_ERROR_NONE !== json_last_error()) {
- throw new \InvalidArgumentException(json_last_error_msg());
- }
- $this->data = $data;
- return $this->update();
- }
-
- public function getEncodingOptions()
- {
- return $this->encodingOptions;
- }
-
- public function setEncodingOptions($encodingOptions)
- {
- $this->encodingOptions = (int) $encodingOptions;
- return $this->setData(json_decode($this->data));
- }
-
- protected function update()
- {
- if (null !== $this->callback) {
-
- $this->headers->set('Content-Type', 'text/javascript');
- return $this->setContent(sprintf('/**/%s(%s);', $this->callback, $this->data));
- }
-
-
- if (!$this->headers->has('Content-Type') || 'text/javascript' === $this->headers->get('Content-Type')) {
- $this->headers->set('Content-Type', 'application/json');
- }
- return $this->setContent($this->data);
- }
- }
|