1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette\Application;
13:
14: use Nette;
15:
16:
17:
18: 19: 20: 21: 22:
23: class JsonResponse extends Nette\Object implements IPresenterResponse
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) && !($payload instanceof \stdClass)) {
40: throw new \InvalidArgumentException("Payload must be array or anonymous 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: public function send()
63: {
64: Nette\Environment::getHttpResponse()->setContentType($this->contentType);
65: Nette\Environment::getHttpResponse()->setExpiration(FALSE);
66: echo json_encode($this->payload);
67: }
68:
69: }
70: