Skip to content

Commit 25b3a34

Browse files
committed
JsonResponse: allow scalar payload [Closes #168]
1 parent d84a480 commit 25b3a34

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/Application/Responses/JsonResponse.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,26 @@ class JsonResponse implements Nette\Application\IResponse
1717
{
1818
use Nette\SmartObject;
1919

20-
/** @var array|\stdClass */
20+
/** @var mixed */
2121
private $payload;
2222

2323
/** @var string */
2424
private $contentType;
2525

2626

2727
/**
28-
* @param array|\stdClass payload
29-
* @param string MIME content type
28+
* @param mixed payload
29+
* @param string MIME content type
3030
*/
3131
public function __construct($payload, $contentType = NULL)
3232
{
33-
if (!is_array($payload) && !is_object($payload)) {
34-
throw new Nette\InvalidArgumentException(sprintf('Payload must be array or object class, %s given.', gettype($payload)));
35-
}
3633
$this->payload = $payload;
3734
$this->contentType = $contentType ? $contentType : 'application/json';
3835
}
3936

4037

4138
/**
42-
* @return array|\stdClass
39+
* @return mixed
4340
*/
4441
public function getPayload()
4542
{

tests/Responses/JsonResponse.contentType.phpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,15 @@ test(function () {
2626
Assert::same($encoded, ob_get_clean());
2727
Assert::same('application/json; charset=utf-8', $response->getHeader('Content-Type'));
2828
});
29+
30+
test(function () {
31+
$data = TRUE;
32+
$encoded = json_encode($data, JSON_UNESCAPED_UNICODE);
33+
$jsonResponse = new JsonResponse($data, 'application/json');
34+
35+
ob_start();
36+
$jsonResponse->send(new Http\Request(new Http\UrlScript), $response = new Http\Response);
37+
38+
Assert::same($encoded, ob_get_clean());
39+
Assert::same('application/json; charset=utf-8', $response->getHeader('Content-Type'));
40+
});

0 commit comments

Comments
 (0)