File indexing completed on 2025-01-26 05:24:53

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_Controller
0017  * @subpackage Zend_Controller_Action_Helper
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * @see Zend_Controller_Action_Helper_Abstract
0025  */
0026 // require_once 'Zend/Controller/Action/Helper/Abstract.php';
0027 
0028 /**
0029  * Simplify AJAX context switching based on requested format
0030  *
0031  * @uses       Zend_Controller_Action_Helper_Abstract
0032  * @category   Zend
0033  * @package    Zend_Controller
0034  * @subpackage Zend_Controller_Action_Helper
0035  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0036  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0037  */
0038 class Zend_Controller_Action_Helper_Json extends Zend_Controller_Action_Helper_Abstract
0039 {
0040     /**
0041      * Suppress exit when sendJson() called
0042      * @var boolean
0043      */
0044     public $suppressExit = false;
0045 
0046     /**
0047      * Create JSON response
0048      *
0049      * Encodes and returns data to JSON. Content-Type header set to
0050      * 'application/json', and disables layouts and viewRenderer (if being
0051      * used).
0052      *
0053      * @param  mixed   $data
0054      * @param  boolean $keepLayouts
0055      * @param  boolean|array $keepLayouts
0056      * @param  boolean $encodeData Provided data is already JSON
0057      * NOTE:   if boolean, establish $keepLayouts to true|false
0058      *         if array, admit params for Zend_Json::encode as enableJsonExprFinder=>true|false
0059      *         if $keepLayouts and parmas for Zend_Json::encode are required
0060      *         then, the array can contains a 'keepLayout'=>true|false and/or 'encodeData'=>true|false
0061      *         that will not be passed to Zend_Json::encode method but will be passed
0062      *         to Zend_View_Helper_Json
0063      * @throws Zend_Controller_Action_Helper_Json
0064      * @return string
0065      */
0066     public function encodeJson($data, $keepLayouts = false, $encodeData = true)
0067     {
0068         /**
0069          * @see Zend_View_Helper_Json
0070          */
0071         // require_once 'Zend/View/Helper/Json.php';
0072         $jsonHelper = new Zend_View_Helper_Json();
0073         $data = $jsonHelper->json($data, $keepLayouts, $encodeData);
0074 
0075         if (!$keepLayouts) {
0076             /**
0077              * @see Zend_Controller_Action_HelperBroker
0078              */
0079             // require_once 'Zend/Controller/Action/HelperBroker.php';
0080             Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->setNoRender(true);
0081         }
0082 
0083         return $data;
0084     }
0085 
0086     /**
0087      * Encode JSON response and immediately send
0088      *
0089      * @param  mixed   $data
0090      * @param  boolean|array $keepLayouts
0091      * @param  $encodeData Encode $data as JSON?
0092      * NOTE:   if boolean, establish $keepLayouts to true|false
0093      *         if array, admit params for Zend_Json::encode as enableJsonExprFinder=>true|false
0094      *         if $keepLayouts and parmas for Zend_Json::encode are required
0095      *         then, the array can contains a 'keepLayout'=>true|false and/or 'encodeData'=>true|false
0096      *         that will not be passed to Zend_Json::encode method but will be passed
0097      *         to Zend_View_Helper_Json
0098      * @return string|void
0099      */
0100     public function sendJson($data, $keepLayouts = false, $encodeData = true)
0101     {
0102         $data = $this->encodeJson($data, $keepLayouts, $encodeData);
0103         $response = $this->getResponse();
0104         $response->setBody($data);
0105 
0106         if (!$this->suppressExit) {
0107             $response->sendResponse();
0108             exit;
0109         }
0110 
0111         return $data;
0112     }
0113 
0114     /**
0115      * Strategy pattern: call helper as helper broker method
0116      *
0117      * Allows encoding JSON. If $sendNow is true, immediately sends JSON
0118      * response.
0119      *
0120      * @param  mixed   $data
0121      * @param  boolean $sendNow
0122      * @param  boolean $keepLayouts
0123      * @param  boolean $encodeData Encode $data as JSON?
0124      * @return string|void
0125      */
0126     public function direct($data, $sendNow = true, $keepLayouts = false, $encodeData = true)
0127     {
0128         if ($sendNow) {
0129             return $this->sendJson($data, $keepLayouts, $encodeData);
0130         }
0131         return $this->encodeJson($data, $keepLayouts, $encodeData);
0132     }
0133 }