Source for file JsonResponse.php

Documentation is available at JsonResponse.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  7. 7:  * @license    http://nettephp.com/license  Nette license
  8. 8:  * @link       http://nettephp.com
  9. 9:  * @category   Nette
  10. 10:  * @package    Nette\Application
  11. 11:  */
  12. 12:  
  13. 13:  
  14. 14:  
  15. 15: /**
  16. 16:  * JSON response used for AJAX requests.
  17. 17:  *
  18. 18:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  19. 19:  * @package    Nette\Application
  20. 20:  */
  21. 21: class JsonResponse extends Object implements IPresenterResponse
  22. 22: {
  23. 23:     /** @var array|stdClass*/
  24. 24:     private $payload;
  25. 25:  
  26. 26:     /** @var string */
  27. 27:     private $contentType;
  28. 28:  
  29. 29:  
  30. 30:  
  31. 31:     /**
  32. 32:      * @param  array|stdClass payload
  33. 33:      * @param  string    MIME content type
  34. 34:      */
  35. 35:     public function __construct($payload$contentType NULL)
  36. 36:     {
  37. 37:         if (!is_array($payload&& !($payload instanceof stdClass)) {
  38. 38:             throw new InvalidArgumentException("Payload must be array or anonymous class, " gettype($payload" given.");
  39. 39:         }
  40. 40:         $this->payload $payload;
  41. 41:         $this->contentType $contentType $contentType 'application/json';
  42. 42:     }
  43. 43:  
  44. 44:  
  45. 45:  
  46. 46:     /**
  47. 47:      * @return array|stdClass
  48. 48:      */
  49. 49:     final public function getPayload()
  50. 50:     {
  51. 51:         return $this->payload;
  52. 52:     }
  53. 53:  
  54. 54:  
  55. 55:  
  56. 56:     /**
  57. 57:      * Sends response to output.
  58. 58:      * @return void 
  59. 59:      */
  60. 60:     public function send()
  61. 61:     {
  62. 62:         Environment::getHttpResponse()->setContentType($this->contentType);
  63. 63:         Environment::getHttpResponse()->setExpiration(FALSE);
  64. 64:         echo json_encode($this->payload);
  65. 65:     }
  66. 66:  
  67. 67: }