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_View_Helper_Abstract.php */
0024 // require_once 'Zend/View/Helper/Abstract.php';
0025 
0026 /**
0027  * Helper for declaring default values of template variables
0028  *
0029  * @package    Zend_View
0030  * @subpackage Helper
0031  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0032  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0033  */
0034 class Zend_View_Helper_DeclareVars extends Zend_View_Helper_Abstract
0035 {
0036     /**
0037      * The view object that created this helper object.
0038      * @var Zend_View
0039      */
0040     public $view;
0041 
0042     /**
0043      * Declare template vars to set default values and avoid notices when using strictVars
0044      *
0045      * Primarily for use when using {@link Zend_View_Abstract::strictVars() Zend_View strictVars()},
0046      * this helper can be used to declare template variables that may or may
0047      * not already be set in the view object, as well as to set default values.
0048      * Arrays passed as arguments to the method will be used to set default
0049      * values; otherwise, if the variable does not exist, it is set to an empty
0050      * string.
0051      *
0052      * Usage:
0053      * <code>
0054      * $this->declareVars(
0055      *     'varName1',
0056      *     'varName2',
0057      *     array('varName3' => 'defaultValue',
0058      *           'varName4' => array()
0059      *     )
0060      * );
0061      * </code>
0062      *
0063      * @param string|array variable number of arguments, all string names of variables to test
0064      * @return void
0065      */
0066     public function declareVars()
0067     {
0068         $args = func_get_args();
0069         foreach($args as $key) {
0070             if (is_array($key)) {
0071                 foreach ($key as $name => $value) {
0072                     $this->_declareVar($name, $value);
0073                 }
0074             } else if (!isset($view->$key)) {
0075                 $this->_declareVar($key);
0076             }
0077         }
0078     }
0079 
0080     /**
0081      * Set a view variable
0082      *
0083      * Checks to see if a $key is set in the view object; if not, sets it to $value.
0084      *
0085      * @param  string $key
0086      * @param  string $value Defaults to an empty string
0087      * @return void
0088      */
0089     protected function _declareVar($key, $value = '')
0090     {
0091         if (!isset($this->view->$key)) {
0092             $this->view->$key = $value;
0093         }
0094     }
0095 }