/application/libraries/Zend/View/Helper/Json.php

https://bitbucket.org/FnTm/codeigniter-zend-sample-application · PHP · 80 lines · 30 code · 6 blank · 44 comment · 3 complexity · 15b66bf5342e017cbcdddab614bfd7d8 MD5 · raw file

  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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @version $Id: Json.php 20096 2010-01-06 02:05:09Z bkarwin $
  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-2010 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. *
  43. * @param mixed $data
  44. * @param bool $keepLayouts
  45. * NOTE: if boolean, establish $keepLayouts to true|false
  46. * if array, admit params for Zend_Json::encode as enableJsonExprFinder=>true|false
  47. * this array can contains a 'keepLayout'=>true|false
  48. * that will not be passed to Zend_Json::encode method but will be used here
  49. * @return string|void
  50. */
  51. public function json($data, $keepLayouts = false)
  52. {
  53. $options = array();
  54. if (is_array($keepLayouts))
  55. {
  56. $options = $keepLayouts;
  57. $keepLayouts = (array_key_exists('keepLayouts', $keepLayouts))
  58. ? $keepLayouts['keepLayouts']
  59. : false;
  60. unset($options['keepLayouts']);
  61. }
  62. $data = Zend_Json::encode($data, null, $options);
  63. if (!$keepLayouts) {
  64. require_once 'Zend/Layout.php';
  65. $layout = Zend_Layout::getMvcInstance();
  66. if ($layout instanceof Zend_Layout) {
  67. $layout->disableLayout();
  68. }
  69. }
  70. $response = Zend_Controller_Front::getInstance()->getResponse();
  71. $response->setHeader('Content-Type', 'application/json');
  72. return $data;
  73. }
  74. }