eZ Publish  [trunk]
eztemplatesectioniterator.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZTemplateSectionIterator class.
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package lib
00009  */
00010 
00011 /*!
00012   \class eZTemplateSectionIterator eztemplatesectioniterator.php
00013   \ingroup eZTemplateFunctions
00014   \brief The iterator item in a section loop which works as a proxy.
00015 
00016   The iterator provides transparent access to iterator items. It will
00017   redirect all attribute calls to the iterator item with the exception
00018   of a few internal values. The internal values are
00019   - item - The actual item, provides backwards compatibility
00020   - key - The current key
00021   - index - The current index value (starts at 0 and increases with 1 for each element)
00022   - number - The current index value + 1 (starts at 1 and increases with 1 for each element)
00023   - sequence - The current sequence value
00024   - last - The last iterated element item
00025 */
00026 
00027 class eZTemplateSectionIterator
00028 {
00029     /*!
00030      Initializes the iterator with empty values.
00031     */
00032     function eZTemplateSectionIterator()
00033     {
00034         $this->InternalAttributes = array( 'item' => false,
00035                                            'key' => false,
00036                                            'index' => false,
00037                                            'number' => false,
00038                                            'sequence' => false,
00039                                            'last' => false );
00040     }
00041 
00042     /*!
00043      \return the value of the current item for the template system to use.
00044     */
00045     function templateValue()
00046     {
00047         $value = $this->InternalAttributes['item'];
00048         return $value;
00049     }
00050 
00051     /*!
00052      \return a merged list of attributes from both the internal attributes and the items attributes.
00053     */
00054     function attributes()
00055     {
00056         $attributes = array();
00057         $item = $this->InternalAttributes['item'];
00058         if ( is_array( $item ) )
00059         {
00060             $attributes = array_keys( $item );
00061         }
00062         else if ( is_object( $item ) and
00063                   method_exists( $item, 'attributes' ) )
00064         {
00065             $attributes = $item->attributes();
00066         }
00067         $attributes = array_merge( $this->InternalAttributes, $attributes );
00068         $attributes = array_unique( $attributes );
00069         return $attributes;
00070     }
00071 
00072     /*!
00073      \return \c true if the attribute \a $name exists either in
00074              the internal attributes or in the item value.
00075     */
00076     function hasAttribute( $name )
00077     {
00078         switch ( $name )
00079         {
00080             case "item":
00081             case "key":
00082             case "index":
00083             case "number":
00084             case "sequence":
00085             case "last":
00086                 return true;
00087         }
00088         $item = $this->InternalAttributes['item'];
00089         if ( is_array( $item ) )
00090         {
00091             return array_key_exists( $name, $item );
00092         }
00093         if ( is_object( $item ) && method_exists( $item, 'hasAttribute' ) )
00094         {
00095             return $item->hasAttribute( $name );
00096         }
00097         return false;
00098     }
00099 
00100     /*!
00101      \return the attribute value of either the internal attributes or
00102              from the item value if the attribute exists for it.
00103     */
00104     function attribute( $name )
00105     {
00106         switch ( $name )
00107         {
00108             case "item":
00109             case "key":
00110             case "index":
00111             case "number":
00112             case "sequence":
00113             case "last":
00114                 return $this->InternalAttributes[$name];
00115         }
00116         $item = $this->InternalAttributes['item'];
00117         if ( is_array( $item ) )
00118         {
00119             return $item[$name];
00120         }
00121         if ( is_object( $item ) && method_exists( $item, 'attribute' ) )
00122         {
00123             return $item->attribute( $name );
00124         }
00125         eZDebug::writeError( "Attribute '$name' does not exist", __METHOD__ );
00126         return null;
00127     }
00128 
00129     /*!
00130      Updates the iterator with the current iteration values.
00131     */
00132     function setIteratorValues( $item, $key, $index, $number, $sequence, &$last )
00133     {
00134         $this->InternalAttributes['item'] = $item;
00135         $this->InternalAttributes['key'] = $key;
00136         $this->InternalAttributes['index'] = $index;
00137         $this->InternalAttributes['number'] = $number;
00138         $this->InternalAttributes['sequence'] = $sequence;
00139         $this->InternalAttributes['last'] = $last;
00140     }
00141 
00142     /*!
00143      Updates the current sequence value to \a $sequence.
00144     */
00145     function setSequence( $sequence )
00146     {
00147         $this->InternalAttributes['sequence'] = $sequence;
00148     }
00149 }
00150 
00151 ?>