File indexing completed on 2024-12-22 05:37:13

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_View
0017  * @subpackage Helper
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @version    $Id$
0020  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0021  */
0022 
0023 /** Zend_Json */
0024 // require_once 'Zend/Json.php';
0025 
0026 /** Zend_Controller_Front */
0027 // require_once 'Zend/Controller/Front.php';
0028 
0029 /** Zend_View_Helper_Abstract.php */
0030 // require_once 'Zend/View/Helper/Abstract.php';
0031 
0032 /**
0033  * Helper for simplifying JSON responses
0034  *
0035  * @package    Zend_View
0036  * @subpackage Helper
0037  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0038  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0039  */
0040 class Zend_View_Helper_Json extends Zend_View_Helper_Abstract
0041 {
0042     /**
0043      * Encode data as JSON, disable layouts, and set response header
0044      *
0045      * If $keepLayouts is true, does not disable layouts.
0046      * If $encodeJson is false, does not JSON-encode $data
0047      *
0048      * @param  mixed $data
0049      * @param  bool $keepLayouts
0050      * NOTE:   if boolean, establish $keepLayouts to true|false
0051      *         if array, admit params for Zend_Json::encode as enableJsonExprFinder=>true|false
0052      *         this array can contains a 'keepLayout'=>true|false and/or 'encodeData'=>true|false
0053      *         that will not be passed to Zend_Json::encode method but will be used here
0054      * @param  bool $encodeData
0055      * @return string|void
0056      */
0057     public function json($data, $keepLayouts = false, $encodeData = true)
0058     {
0059         $options = array();
0060         if (is_array($keepLayouts)) {
0061             $options = $keepLayouts;
0062 
0063             $keepLayouts = false;
0064             if (array_key_exists('keepLayouts', $options)) {
0065                 $keepLayouts = $options['keepLayouts'];
0066                 unset($options['keepLayouts']);
0067             }
0068 
0069             if (array_key_exists('encodeData', $options)) {
0070                 $encodeData = $options['encodeData'];
0071                 unset($options['encodeData']);
0072             }
0073         }
0074 
0075         if ($encodeData) {
0076             $data = Zend_Json::encode($data, null, $options);
0077         }
0078         if (!$keepLayouts) {
0079             // require_once 'Zend/Layout.php';
0080             $layout = Zend_Layout::getMvcInstance();
0081             if ($layout instanceof Zend_Layout) {
0082                 $layout->disableLayout();
0083             }
0084         }
0085 
0086         $response = Zend_Controller_Front::getInstance()->getResponse();
0087         $response->setHeader('Content-Type', 'application/json', true);
0088         return $data;
0089     }
0090 }