From 25b3a34195bb3228913fb553add51d861b255dce Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 19 Dec 2016 12:19:09 +0100 Subject: [PATCH] JsonResponse: allow scalar payload [Closes #168] --- src/Application/Responses/JsonResponse.php | 11 ++++------- tests/Responses/JsonResponse.contentType.phpt | 12 ++++++++++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/Application/Responses/JsonResponse.php b/src/Application/Responses/JsonResponse.php index 050df7356..eeeeaeb49 100644 --- a/src/Application/Responses/JsonResponse.php +++ b/src/Application/Responses/JsonResponse.php @@ -17,7 +17,7 @@ class JsonResponse implements Nette\Application\IResponse { use Nette\SmartObject; - /** @var array|\stdClass */ + /** @var mixed */ private $payload; /** @var string */ @@ -25,21 +25,18 @@ class JsonResponse implements Nette\Application\IResponse /** - * @param array|\stdClass payload - * @param string MIME content type + * @param mixed payload + * @param string MIME content type */ public function __construct($payload, $contentType = NULL) { - if (!is_array($payload) && !is_object($payload)) { - throw new Nette\InvalidArgumentException(sprintf('Payload must be array or object class, %s given.', gettype($payload))); - } $this->payload = $payload; $this->contentType = $contentType ? $contentType : 'application/json'; } /** - * @return array|\stdClass + * @return mixed */ public function getPayload() { diff --git a/tests/Responses/JsonResponse.contentType.phpt b/tests/Responses/JsonResponse.contentType.phpt index 0d524cdf4..da896af4d 100644 --- a/tests/Responses/JsonResponse.contentType.phpt +++ b/tests/Responses/JsonResponse.contentType.phpt @@ -26,3 +26,15 @@ test(function () { Assert::same($encoded, ob_get_clean()); Assert::same('application/json; charset=utf-8', $response->getHeader('Content-Type')); }); + +test(function () { + $data = TRUE; + $encoded = json_encode($data, JSON_UNESCAPED_UNICODE); + $jsonResponse = new JsonResponse($data, 'application/json'); + + ob_start(); + $jsonResponse->send(new Http\Request(new Http\UrlScript), $response = new Http\Response); + + Assert::same($encoded, ob_get_clean()); + Assert::same('application/json; charset=utf-8', $response->getHeader('Content-Type')); +});