1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19:
20: class NJsonResponse extends NObject implements IPresenterResponse
21: {
22:
23: private $payload;
24:
25:
26: private $contentType;
27:
28:
29:
30: 31: 32: 33:
34: public function __construct($payload, $contentType = NULL)
35: {
36: if (!is_array($payload) && !($payload instanceof stdClass)) {
37: throw new InvalidArgumentException("Payload must be array or anonymous class, " . gettype($payload) . " given.");
38: }
39: $this->payload = $payload;
40: $this->contentType = $contentType ? $contentType : 'application/json';
41: }
42:
43:
44:
45: 46: 47:
48: final public function getPayload()
49: {
50: return $this->payload;
51: }
52:
53:
54:
55: 56: 57: 58:
59: public function send()
60: {
61: NEnvironment::getHttpResponse()->setContentType($this->contentType);
62: NEnvironment::getHttpResponse()->setExpiration(FALSE);
63: echo NJson::encode($this->payload);
64: }
65:
66: }
67: