X-Git-Url: http://aleph1.co.uk/gitweb/?a=blobdiff_plain;f=vendor%2Fsymfony%2Fhttp-foundation%2FJsonResponse.php;h=d741ce099d730fb3d665e118653bd1c6d31bba20;hb=0bf8d09d2542548982e81a441b1f16e75873a04f;hp=a83d0077a84293b58fc6dd58a9b02a0733f5ccda;hpb=eba34333e3c89f208d2f72fa91351ad019a71583;p=yaffs-website diff --git a/vendor/symfony/http-foundation/JsonResponse.php b/vendor/symfony/http-foundation/JsonResponse.php index a83d0077a..d741ce099 100644 --- a/vendor/symfony/http-foundation/JsonResponse.php +++ b/vendor/symfony/http-foundation/JsonResponse.php @@ -29,16 +29,17 @@ class JsonResponse extends Response // Encode <, >, ', &, and " characters in the JSON, making it also safe to be embedded into HTML. // 15 === JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT - protected $encodingOptions = 15; + const DEFAULT_ENCODING_OPTIONS = 15; + + protected $encodingOptions = self::DEFAULT_ENCODING_OPTIONS; /** - * Constructor. - * * @param mixed $data The response data * @param int $status The response status code * @param array $headers An array of response headers + * @param bool $json If the data is already a JSON string */ - public function __construct($data = null, $status = 200, $headers = array()) + public function __construct($data = null, $status = 200, $headers = array(), $json = false) { parent::__construct('', $status, $headers); @@ -46,7 +47,7 @@ class JsonResponse extends Response $data = new \ArrayObject(); } - $this->setData($data); + $json ? $this->setJson($data) : $this->setData($data); } /** @@ -68,6 +69,14 @@ class JsonResponse extends Response return new static($data, $status, $headers); } + /** + * Make easier the creation of JsonResponse from raw json. + */ + public static function fromJsonString($data = null, $status = 200, $headers = array()) + { + return new static($data, $status, $headers, true); + } + /** * Sets the JSONP callback. * @@ -80,8 +89,8 @@ class JsonResponse extends Response public function setCallback($callback = null) { if (null !== $callback) { - // partially token from http://www.geekality.net/2011/08/03/valid-javascript-identifier/ - // partially token from https://github.com/willdurand/JsonpCallbackValidator + // partially taken from http://www.geekality.net/2011/08/03/valid-javascript-identifier/ + // partially taken from https://github.com/willdurand/JsonpCallbackValidator // JsonpCallbackValidator is released under the MIT License. See https://github.com/willdurand/JsonpCallbackValidator/blob/v1.1.0/LICENSE for details. // (c) William Durand $pattern = '/^[$_\p{L}][$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\x{200C}\x{200D}]*(?:\[(?:"(?:\\\.|[^"\\\])*"|\'(?:\\\.|[^\'\\\])*\'|\d+)\])*?$/u'; @@ -92,7 +101,7 @@ class JsonResponse extends Response ); $parts = explode('.', $callback); foreach ($parts as $part) { - if (!preg_match($pattern, $part) || in_array($part, $reserved, true)) { + if (!preg_match($pattern, $part) || \in_array($part, $reserved, true)) { throw new \InvalidArgumentException('The callback name is not valid.'); } } @@ -103,6 +112,22 @@ class JsonResponse extends Response return $this->update(); } + /** + * Sets a raw string containing a JSON document to be sent. + * + * @param string $json + * + * @return $this + * + * @throws \InvalidArgumentException + */ + public function setJson($json) + { + $this->data = $json; + + return $this->update(); + } + /** * Sets the data to be sent as JSON. * @@ -114,49 +139,28 @@ class JsonResponse extends Response */ public function setData($data = array()) { - if (defined('HHVM_VERSION')) { + if (\defined('HHVM_VERSION')) { // HHVM does not trigger any warnings and let exceptions // thrown from a JsonSerializable object pass through. // If only PHP did the same... $data = json_encode($data, $this->encodingOptions); } else { - try { - if (\PHP_VERSION_ID < 50400) { - // PHP 5.3 triggers annoying warnings for some - // types that can't be serialized as JSON (INF, resources, etc.) - // but doesn't provide the JsonSerializable interface. - set_error_handler(function () { return false; }); + if (!interface_exists('JsonSerializable', false)) { + set_error_handler(function () { return false; }); + try { $data = @json_encode($data, $this->encodingOptions); - } else { - // PHP 5.4 and up wrap exceptions thrown by JsonSerializable - // objects in a new exception that needs to be removed. - // Fortunately, PHP 5.5 and up do not trigger any warning anymore. - if (\PHP_VERSION_ID < 50500) { - // Clear json_last_error() - 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) { + } finally { restore_error_handler(); } - if (\PHP_VERSION_ID >= 50400 && 'Exception' === get_class($e) && 0 === strpos($e->getMessage(), 'Failed calling ')) { - throw $e->getPrevious() ?: $e; + } else { + try { + $data = json_encode($data, $this->encodingOptions); + } catch (\Exception $e) { + if ('Exception' === \get_class($e) && 0 === strpos($e->getMessage(), 'Failed calling ')) { + throw $e->getPrevious() ?: $e; + } + throw $e; } - throw $e; } } @@ -164,9 +168,7 @@ class JsonResponse extends Response throw new \InvalidArgumentException(json_last_error_msg()); } - $this->data = $data; - - return $this->update(); + return $this->setJson($data); } /**