PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/Nette/Application/Responses/JsonResponse.php

https://github.com/DocX/nette
PHP | 83 lines | 26 code | 20 blank | 37 comment | 2 complexity | ad47a5b78b7e9f1264191905fabd7e07 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Nette Framework
  4. *
  5. * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  6. *
  7. * This source file is subject to the "Nette license" that is bundled
  8. * with this package in the file license.txt.
  9. *
  10. * For more information please see http://nettephp.com
  11. *
  12. * @copyright Copyright (c) 2004, 2009 David Grudl
  13. * @license http://nettephp.com/license Nette license
  14. * @link http://nettephp.com
  15. * @category Nette
  16. * @package Nette\Application
  17. */
  18. /*namespace Nette\Application;*/
  19. require_once dirname(__FILE__) . '/../../Object.php';
  20. require_once dirname(__FILE__) . '/../../Application/IPresenterResponse.php';
  21. /**
  22. * JSON response used for AJAX requests.
  23. *
  24. * @author David Grudl
  25. * @copyright Copyright (c) 2004, 2009 David Grudl
  26. * @package Nette\Application
  27. */
  28. class JsonResponse extends /*Nette\*/Object implements IPresenterResponse
  29. {
  30. /** @var array|stdClass */
  31. private $payload;
  32. /** @var string */
  33. private $contentType;
  34. /**
  35. * @param array|stdClass payload
  36. * @param string MIME content type
  37. */
  38. public function __construct($payload, $contentType = NULL)
  39. {
  40. if (!is_array($payload) && !($payload instanceof /*\*/stdClass)) {
  41. throw new /*\*/InvalidArgumentException("Payload must be array or anonymous class, " . gettype($payload) . " given.");
  42. }
  43. $this->payload = $payload;
  44. $this->contentType = $contentType ? $contentType : 'application/json';
  45. }
  46. /**
  47. * @return array|stdClass
  48. */
  49. final public function getPayload()
  50. {
  51. return $this->payload;
  52. }
  53. /**
  54. * Sends response to output.
  55. * @return void
  56. */
  57. public function send()
  58. {
  59. /*Nette\*/Environment::getHttpResponse()->setContentType($this->contentType);
  60. /*Nette\*/Environment::getHttpResponse()->expire(FALSE);
  61. echo json_encode($this->payload);
  62. }
  63. }