From b7b929bf99a3989323fc2617d0d075584c4a386b Mon Sep 17 00:00:00 2001 From: Shane Armstrong Date: Tue, 11 Oct 2016 20:40:18 +0100 Subject: [PATCH] #290 - JSON methods now support encode option/depth, added missing arguments to json/jsonp method documentation, _json/_jsonp methods now list arguments on individual lines to remain PSR-2 compliant --- README.md | 4 ++-- flight/Engine.php | 27 +++++++++++++++++++++++---- flight/Flight.php | 4 ++-- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 0e3784c..6f469da 100644 --- a/README.md +++ b/README.md @@ -887,8 +887,8 @@ Flight::error($exception) // Sends an HTTP 500 response. Flight::notFound() // Sends an HTTP 404 response. Flight::etag($id, [$type]) // Performs ETag HTTP caching. Flight::lastModified($time) // Performs last modified HTTP caching. -Flight::json($data, [$code], [$encode]) // Sends a JSON response. -Flight::jsonp($data, [$param], [$code], [$encode]) // Sends a JSONP response. +Flight::json($data, [$code], [$encode], [$charset], [$encodeOption], [$encodeDepth]) // Sends a JSON response. +Flight::jsonp($data, [$param], [$code], [$encode], [$charset], [$encodeOption], [$encodeDepth]) // Sends a JSONP response. ``` Any custom methods added with `map` and `register` can also be filtered. diff --git a/flight/Engine.php b/flight/Engine.php index f364e21..914e08e 100644 --- a/flight/Engine.php +++ b/flight/Engine.php @@ -452,9 +452,18 @@ class Engine { * @param int $code HTTP status code * @param bool $encode Whether to perform JSON encoding * @param string $charset Charset + * @param int $encodeOption Bitmask Json constant such as JSON_HEX_QUOT + * @param int $encodeDepth Maximum encoding depth, must be greater than 0 */ - public function _json($data, $code = 200, $encode = true, $charset = 'utf-8') { - $json = ($encode) ? json_encode($data) : $data; + public function _json( + $data, + $code = 200, + $encode = true, + $charset = 'utf-8', + $encodeOption = 0, + $encodeDepth = 512 + ) { + $json = ($encode) ? json_encode($data, $encodeOption, $encodeDepth) : $data; $this->response() ->status($code) @@ -471,9 +480,19 @@ class Engine { * @param int $code HTTP status code * @param bool $encode Whether to perform JSON encoding * @param string $charset Charset + * @param int $encodeOption Bitmask Json constant such as JSON_HEX_QUOT + * @param int $encodeDepth Maximum encoding depth, must be greater than 0 */ - public function _jsonp($data, $param = 'jsonp', $code = 200, $encode = true, $charset = 'utf-8') { - $json = ($encode) ? json_encode($data) : $data; + public function _jsonp( + $data, + $param = 'jsonp', + $code = 200, + $encode = true, + $charset = 'utf-8', + $encodeOption = 0, + $encodeDepth = 512 + ) { + $json = ($encode) ? json_encode($data, $encodeOption, $encodeDepth) : $data; $callback = $this->request()->query[$param]; diff --git a/flight/Flight.php b/flight/Flight.php index 2cbe4ae..f58dc2c 100644 --- a/flight/Flight.php +++ b/flight/Flight.php @@ -41,8 +41,8 @@ * @method static \flight\net\Request request() Returns Request instance. * @method static \flight\net\Response response() Returns Request instance. * @method static void redirect($url, $code = 303) Redirects to another URL. - * @method static void json($data, $code = 200, $encode = true) Sends a JSON response. - * @method static void jsonp($data, $param = 'jsonp', $code = 200, $encode = true) Sends a JSONP response. + * @method static void json($data, $code = 200, $encode = true, $charset = "utf8", $encodeOption = 0, $encodeDepth = 512) Sends a JSON response. + * @method static void jsonp($data, $param = 'jsonp', $code = 200, $encode = true, $charset = "utf8", $encodeOption = 0, $encodeDepth = 512) Sends a JSONP response. * @method static void error($exception) Sends an HTTP 500 response. * @method static void notFound() Sends an HTTP 404 response. *