1: <?php
2:
3: /**
4: * This file is part of the Nette Framework.
5: *
6: * Copyright (c) 2004, 2010 David Grudl (http://davidgrudl.com)
7: *
8: * This source file is subject to the "Nette license", and/or
9: * GPL license. For more information please see http://nette.org
10: * @package Nette\Application
11: */
12:
13:
14:
15: /**
16: * JSON response used for AJAX requests.
17: *
18: * @author David Grudl
19: */
20: class JsonResponse extends Object implements IPresenterResponse
21: {
22: /** @var array|stdClass */
23: private $payload;
24:
25: /** @var string */
26: private $contentType;
27:
28:
29:
30: /**
31: * @param array|stdClass payload
32: * @param string MIME content type
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: * @return array|stdClass
47: */
48: final public function getPayload()
49: {
50: return $this->payload;
51: }
52:
53:
54:
55: /**
56: * Sends response to output.
57: * @return void
58: */
59: public function send()
60: {
61: Environment::getHttpResponse()->setContentType($this->contentType);
62: Environment::getHttpResponse()->setExpiration(FALSE);
63: echo Json::encode($this->payload);
64: }
65:
66: }
67: