web/lib/Zend/View/Helper/Cycle.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     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: Cycle.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    20  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    21  */
       
    22 
       
    23 /**
       
    24  * Helper for alternating between set of values
       
    25  *
       
    26  * @package    Zend_View
       
    27  * @subpackage Helper
       
    28  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    29  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    30  */
       
    31 class Zend_View_Helper_Cycle implements Iterator
       
    32 {
       
    33 
       
    34     /**
       
    35      * Default name
       
    36      * @var string
       
    37      */
       
    38     const DEFAULT_NAME = 'default';
       
    39 
       
    40     /**
       
    41      * Pointers
       
    42      *
       
    43      * @var array
       
    44      */
       
    45     protected $_pointers = array(self::DEFAULT_NAME =>-1) ;
       
    46 
       
    47     /**
       
    48      * Array of values
       
    49      *
       
    50      * @var array
       
    51      */
       
    52     protected $_data = array(self::DEFAULT_NAME=>array());
       
    53 
       
    54     /**
       
    55      * Actual name of cycle
       
    56      *
       
    57      * @var string
       
    58      */
       
    59     protected $_name = self::DEFAULT_NAME;
       
    60 
       
    61     /**
       
    62      * Add elements to alternate
       
    63      *
       
    64      * @param array $data
       
    65      * @param string $name
       
    66      * @return Zend_View_Helper_Cycle
       
    67      */
       
    68     public function cycle(array $data = array(), $name = self::DEFAULT_NAME)
       
    69     {
       
    70         if(!empty($data))
       
    71            $this->_data[$name] = $data;
       
    72 
       
    73         $this->setName($name);
       
    74         return $this;
       
    75     }
       
    76 
       
    77     /**
       
    78      * Add elements to alternate
       
    79      *
       
    80      * @param array $data
       
    81      * @param string $name
       
    82      * @return Zend_View_Helper_Cycle
       
    83      */
       
    84     public function assign(Array $data , $name = self::DEFAULT_NAME)
       
    85     {
       
    86         $this->setName($name);
       
    87         $this->_data[$name] = $data;
       
    88         $this->rewind();
       
    89         return $this;
       
    90     }
       
    91 
       
    92     /**
       
    93      * Sets actual name of cycle
       
    94      *
       
    95      * @param $name
       
    96      * @return Zend_View_Helper_Cycle
       
    97      */
       
    98     public function setName($name = self::DEFAULT_NAME)
       
    99     {
       
   100        $this->_name = $name;
       
   101 
       
   102        if(!isset($this->_data[$this->_name]))
       
   103          $this->_data[$this->_name] = array();
       
   104 
       
   105        if(!isset($this->_pointers[$this->_name]))
       
   106          $this->rewind();
       
   107 
       
   108        return $this;
       
   109     }
       
   110 
       
   111     /**
       
   112      * Gets actual name of cycle
       
   113      *
       
   114      * @param $name
       
   115      * @return string
       
   116      */
       
   117     public function getName()
       
   118     {
       
   119         return $this->_name;
       
   120     }
       
   121 
       
   122 
       
   123     /**
       
   124      * Return all elements
       
   125      *
       
   126      * @return array
       
   127      */
       
   128     public function getAll()
       
   129     {
       
   130         return $this->_data[$this->_name];
       
   131     }
       
   132 
       
   133     /**
       
   134      * Turn helper into string
       
   135      *
       
   136      * @return string
       
   137      */
       
   138     public function toString()
       
   139     {
       
   140         return (string) $this->_data[$this->_name][$this->key()];
       
   141     }
       
   142 
       
   143     /**
       
   144      * Cast to string
       
   145      *
       
   146      * @return string
       
   147      */
       
   148     public function __toString()
       
   149     {
       
   150         return $this->toString();
       
   151     }
       
   152 
       
   153     /**
       
   154      * Move to next value
       
   155      *
       
   156      * @return Zend_View_Helper_Cycle
       
   157      */
       
   158     public function next()
       
   159     {
       
   160         $count = count($this->_data[$this->_name]);
       
   161         if ($this->_pointers[$this->_name] == ($count - 1))
       
   162             $this->_pointers[$this->_name] = 0;
       
   163         else
       
   164             $this->_pointers[$this->_name] = ++$this->_pointers[$this->_name];
       
   165         return $this;
       
   166     }
       
   167 
       
   168     /**
       
   169      * Move to previous value
       
   170      *
       
   171      * @return Zend_View_Helper_Cycle
       
   172      */
       
   173     public function prev()
       
   174     {
       
   175         $count = count($this->_data[$this->_name]);
       
   176         if ($this->_pointers[$this->_name] <= 0)
       
   177             $this->_pointers[$this->_name] = $count - 1;
       
   178         else
       
   179             $this->_pointers[$this->_name] = --$this->_pointers[$this->_name];
       
   180         return $this;
       
   181     }
       
   182 
       
   183     /**
       
   184      * Return iteration number
       
   185      *
       
   186      * @return int
       
   187      */
       
   188     public function key()
       
   189     {
       
   190         if ($this->_pointers[$this->_name] < 0)
       
   191             return 0;
       
   192         else
       
   193             return $this->_pointers[$this->_name];
       
   194     }
       
   195 
       
   196     /**
       
   197      * Rewind pointer
       
   198      *
       
   199      * @return Zend_View_Helper_Cycle
       
   200      */
       
   201     public function rewind()
       
   202     {
       
   203         $this->_pointers[$this->_name] = -1;
       
   204         return $this;
       
   205     }
       
   206 
       
   207     /**
       
   208      * Check if element is valid
       
   209      *
       
   210      * @return bool
       
   211      */
       
   212     public function valid()
       
   213     {
       
   214         return isset($this->_data[$this->_name][$this->key()]);
       
   215     }
       
   216 
       
   217     /**
       
   218      * Return  current element
       
   219      *
       
   220      * @return mixed
       
   221      */
       
   222     public function current()
       
   223     {
       
   224         return $this->_data[$this->_name][$this->key()];
       
   225     }
       
   226 }