1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette\Application\Responses;
13:
14: use Nette;
15:
16:
17:
18: 19: 20: 21: 22:
23: class JsonResponse extends Nette\Object implements Nette\Application\IResponse
24: {
25:
26: private $payload;
27:
28:
29: private $contentType;
30:
31:
32:
33: 34: 35: 36:
37: public function __construct($payload, $contentType = NULL)
38: {
39: if (!is_array($payload) && !is_object($payload)) {
40: throw new Nette\InvalidArgumentException("Payload must be array or object class, " . gettype($payload) . " given.");
41: }
42: $this->payload = $payload;
43: $this->contentType = $contentType ? $contentType : 'application/json';
44: }
45:
46:
47:
48: 49: 50:
51: final public function getPayload()
52: {
53: return $this->payload;
54: }
55:
56:
57:
58: 59: 60: 61:
62: final public function getContentType()
63: {
64: return $this->contentType;
65: }
66:
67:
68:
69: 70: 71: 72:
73: public function send(Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse)
74: {
75: $httpResponse->setContentType($this->contentType);
76: $httpResponse->setExpiration(FALSE);
77: echo Nette\Utils\Json::encode($this->payload);
78: }
79:
80: }
81: