File indexing completed on 2024-06-16 05:30:34

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 /**
0024  * Helper for alternating between set of values
0025  *
0026  * @package    Zend_View
0027  * @subpackage Helper
0028  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0029  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0030  */
0031 class Zend_View_Helper_Cycle implements Iterator
0032 {
0033 
0034     /**
0035      * Default name
0036      * @var string
0037      */
0038     const DEFAULT_NAME = 'default';
0039 
0040     /**
0041      * Pointers
0042      *
0043      * @var array
0044      */
0045     protected $_pointers = array(self::DEFAULT_NAME =>-1) ;
0046 
0047     /**
0048      * Array of values
0049      *
0050      * @var array
0051      */
0052     protected $_data = array(self::DEFAULT_NAME=>array());
0053 
0054     /**
0055      * Actual name of cycle
0056      *
0057      * @var string
0058      */
0059     protected $_name = self::DEFAULT_NAME;
0060 
0061     /**
0062      * Add elements to alternate
0063      *
0064      * @param array $data
0065      * @param string $name
0066      * @return Zend_View_Helper_Cycle
0067      */
0068     public function cycle(array $data = array(), $name = self::DEFAULT_NAME)
0069     {
0070         if(!empty($data))
0071            $this->_data[$name] = $data;
0072 
0073         $this->setName($name);
0074         return $this;
0075     }
0076 
0077     /**
0078      * Add elements to alternate
0079      *
0080      * @param array $data
0081      * @param string $name
0082      * @return Zend_View_Helper_Cycle
0083      */
0084     public function assign(Array $data , $name = self::DEFAULT_NAME)
0085     {
0086         $this->setName($name);
0087         $this->_data[$name] = $data;
0088         $this->rewind();
0089         return $this;
0090     }
0091 
0092     /**
0093      * Sets actual name of cycle
0094      *
0095      * @param string $name
0096      * @return Zend_View_Helper_Cycle
0097      */
0098     public function setName($name = self::DEFAULT_NAME)
0099     {
0100        $this->_name = $name;
0101 
0102        if(!isset($this->_data[$this->_name]))
0103          $this->_data[$this->_name] = array();
0104 
0105        if(!isset($this->_pointers[$this->_name]))
0106          $this->rewind();
0107 
0108        return $this;
0109     }
0110 
0111     /**
0112      * Gets actual name of cycle
0113      *
0114      * @return string
0115      */
0116     public function getName()
0117     {
0118         return $this->_name;
0119     }
0120 
0121 
0122     /**
0123      * Return all elements
0124      *
0125      * @return array
0126      */
0127     public function getAll()
0128     {
0129         return $this->_data[$this->_name];
0130     }
0131 
0132     /**
0133      * Turn helper into string
0134      *
0135      * @return string
0136      */
0137     public function toString()
0138     {
0139         return (string) $this->_data[$this->_name][$this->key()];
0140     }
0141 
0142     /**
0143      * Cast to string
0144      *
0145      * @return string
0146      */
0147     public function __toString()
0148     {
0149         return $this->toString();
0150     }
0151 
0152     /**
0153      * Move to next value
0154      *
0155      * @return Zend_View_Helper_Cycle
0156      */
0157     public function next()
0158     {
0159         $count = count($this->_data[$this->_name]);
0160         if ($this->_pointers[$this->_name] == ($count - 1))
0161             $this->_pointers[$this->_name] = 0;
0162         else
0163             $this->_pointers[$this->_name] = ++$this->_pointers[$this->_name];
0164         return $this;
0165     }
0166 
0167     /**
0168      * Move to previous value
0169      *
0170      * @return Zend_View_Helper_Cycle
0171      */
0172     public function prev()
0173     {
0174         $count = count($this->_data[$this->_name]);
0175         if ($this->_pointers[$this->_name] <= 0)
0176             $this->_pointers[$this->_name] = $count - 1;
0177         else
0178             $this->_pointers[$this->_name] = --$this->_pointers[$this->_name];
0179         return $this;
0180     }
0181 
0182     /**
0183      * Return iteration number
0184      *
0185      * @return int
0186      */
0187     public function key()
0188     {
0189         if ($this->_pointers[$this->_name] < 0)
0190             return 0;
0191         else
0192             return $this->_pointers[$this->_name];
0193     }
0194 
0195     /**
0196      * Rewind pointer
0197      *
0198      * @return Zend_View_Helper_Cycle
0199      */
0200     public function rewind()
0201     {
0202         $this->_pointers[$this->_name] = -1;
0203         return $this;
0204     }
0205 
0206     /**
0207      * Check if element is valid
0208      *
0209      * @return bool
0210      */
0211     public function valid()
0212     {
0213         return isset($this->_data[$this->_name][$this->key()]);
0214     }
0215 
0216     /**
0217      * Return  current element
0218      *
0219      * @return mixed
0220      */
0221     public function current()
0222     {
0223         return $this->_data[$this->_name][$this->key()];
0224     }
0225 }