Json.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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-2012 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-2012 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. {
  58. $options = $keepLayouts;
  59. $keepLayouts = (array_key_exists('keepLayouts', $keepLayouts))
  60. ? $keepLayouts['keepLayouts']
  61. : false;
  62. unset($options['keepLayouts']);
  63. $encodeData = (array_key_exists('encodeData', $keepLayouts))
  64. ? $keepLayouts['encodeData']
  65. : $encodeData;
  66. unset($options['encodeData']);
  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. }