Json.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_View
  17. * @subpackage Helper
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @version $Id$
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. /** Zend_Json */
  23. require_once 'Zend/Json.php';
  24. /** Zend_Controller_Front */
  25. require_once 'Zend/Controller/Front.php';
  26. /** Zend_View_Helper_Abstract.php */
  27. require_once 'Zend/View/Helper/Abstract.php';
  28. /**
  29. * Helper for simplifying JSON responses
  30. *
  31. * @package Zend_View
  32. * @subpackage Helper
  33. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_View_Helper_Json extends Zend_View_Helper_Abstract
  37. {
  38. /**
  39. * Encode data as JSON, disable layouts, and set response header
  40. *
  41. * If $keepLayouts is true, does not disable layouts.
  42. * If $encodeJson is false, does not JSON-encode $data
  43. *
  44. * @param mixed $data
  45. * @param bool $keepLayouts
  46. * NOTE: if boolean, establish $keepLayouts to true|false
  47. * if array, admit params for Zend_Json::encode as enableJsonExprFinder=>true|false
  48. * this array can contains a 'keepLayout'=>true|false and/or 'encodeData'=>true|false
  49. * that will not be passed to Zend_Json::encode method but will be used here
  50. * @param bool $encodeData
  51. * @return string|void
  52. */
  53. public function json($data, $keepLayouts = false, $encodeData = true)
  54. {
  55. $options = array();
  56. if (is_array($keepLayouts)) {
  57. $options = $keepLayouts;
  58. $keepLayouts = false;
  59. if (array_key_exists('keepLayouts', $options)) {
  60. $keepLayouts = $options['keepLayouts'];
  61. unset($options['keepLayouts']);
  62. }
  63. if (array_key_exists('encodeData', $options)) {
  64. $encodeData = $options['encodeData'];
  65. unset($options['encodeData']);
  66. }
  67. }
  68. if ($encodeData) {
  69. $data = Zend_Json::encode($data, null, $options);
  70. }
  71. if (!$keepLayouts) {
  72. require_once 'Zend/Layout.php';
  73. $layout = Zend_Layout::getMvcInstance();
  74. if ($layout instanceof Zend_Layout) {
  75. $layout->disableLayout();
  76. }
  77. }
  78. $response = Zend_Controller_Front::getInstance()->getResponse();
  79. $response->setHeader('Content-Type', 'application/json', true);
  80. return $data;
  81. }
  82. }