Apache Zeta Components Manual :: File Source for tree_node_list_iterator.php

Source for file tree_node_list_iterator.php

Documentation is available at tree_node_list_iterator.php

  1. <?php
  2. /**
  3.  * File containing the ezcTreeNodeListIterator class.
  4.  *
  5.  * Licensed to the Apache Software Foundation (ASF) under one
  6.  * or more contributor license agreements.  See the NOTICE file
  7.  * distributed with this work for additional information
  8.  * regarding copyright ownership.  The ASF licenses this file
  9.  * to you under the Apache License, Version 2.0 (the
  10.  * "License"); you may not use this file except in compliance
  11.  * with the License.  You may obtain a copy of the License at
  12.  * 
  13.  *   http://www.apache.org/licenses/LICENSE-2.0
  14.  * 
  15.  * Unless required by applicable law or agreed to in writing,
  16.  * software distributed under the License is distributed on an
  17.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18.  * KIND, either express or implied.  See the License for the
  19.  * specific language governing permissions and limitations
  20.  * under the License.
  21.  *
  22.  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  23.  * @version //autogentag//
  24.  * @filesource
  25.  * @package Tree
  26.  */
  27.  
  28. /**
  29.  * ezcTreeNodeListIterator implements an iterator over an ezcTreeNodeList.
  30.  *
  31.  * The iterator is instantiated with both an implementation of an ezcTree and
  32.  * an ezcTreeNodeList object. It can be used to iterate over all the nodes
  33.  * in a list.
  34.  *
  35.  * Example:
  36.  * <code>
  37.  * <?php
  38.  *     // fetch all the nodes in a subtree as an ezcNodeList
  39.  *     $nodeList = $tree->fetchSubtree( 'pan' );
  40.  *     foreach ( new ezcTreeNodeListIterator( $tree, $nodeList ) as $nodeId => $data )
  41.  *     {
  42.  *         // do something with the node ID and data - data is fetched on
  43.  *         // demand
  44.  *     }
  45.  * ?>
  46.  * </code>
  47.  *
  48.  * Data for the nodes in the node lists is fetched on demand, unless
  49.  * the "prefetch" argument is set to true. In that case the iterator will
  50.  * fetch the data when the iterator is instantiated. This reduces the number
  51.  * of queries made for database and persistent object based data stores, but
  52.  * increases memory usage.
  53.  *
  54.  * Example:
  55.  * <code>
  56.  * <?php
  57.  *     // fetch all the nodes in a subtree as an ezcNodeList
  58.  *     $nodeList = $tree->fetchSubtree( 'Uranus' );
  59.  *     // instantiate an iterator with pre-fetching enabled
  60.  *     foreach ( new ezcTreeNodeListIterator( $tree, $nodeList, true ) as $nodeId => $data )
  61.  *     {
  62.  *         // do something with the node ID and data - data is fetched when
  63.  *         // the iterator is instatiated.
  64.  *     }
  65.  * ?>
  66.  * </code>
  67.  *
  68.  * @package Tree
  69.  * @version //autogentag//
  70.  * @mainclass
  71.  */
  72. class ezcTreeNodeListIterator implements Iterator
  73. {
  74.     /**
  75.      * Holds the nodes of this list.
  76.      *
  77.      * @var array(ezcTreeNode) 
  78.      */
  79.     private $nodeList;
  80.  
  81.     /**
  82.      * Holds a link to the tree that contains the nodes that are iterated over.
  83.      *
  84.      * This is used for accessing the data store so that data can be fetched
  85.      * on-demand.
  86.      *
  87.      * @var ezcTree 
  88.      */
  89.     private $tree;
  90.  
  91.     /**
  92.      * Constructs a new ezcTreeNodeListIterator object over $nodeList.
  93.      *
  94.      * The $tree argument is used so that data can be fetched on-demand.
  95.      *
  96.      * @param ezcTree         $tree 
  97.      * @param ezcTreeNodeList $nodeList 
  98.      * @param bool            $prefetch 
  99.      */
  100.     public function __constructezcTree $treeezcTreeNodeList $nodeList$prefetch false )
  101.     {
  102.         $this->tree $tree;
  103.         if $prefetch )
  104.         {
  105.             $this->tree->store->fetchDataForNodes$nodeList );
  106.         }
  107.         $this->nodeList $nodeList->nodes;
  108.     }
  109.  
  110.     /**
  111.      * Rewinds the internal pointer back to the start of the nodelist.
  112.      */
  113.     public function rewind()
  114.     {
  115.         reset$this->nodeList );
  116.         if count$this->nodeList ) )
  117.         {
  118.             $this->valid true;
  119.         }
  120.         else 
  121.         {
  122.             $this->valid false;
  123.         }
  124.     }
  125.  
  126.     /**
  127.      * Returns the node ID of the current node.
  128.      *
  129.      * @return string 
  130.      */
  131.     public function key()
  132.     {
  133.         return key$this->nodeList );
  134.     }
  135.  
  136.     /**
  137.      * Returns the data belonging to the current node, and fetches the data in
  138.      * case on-demand fetching is required.
  139.      *
  140.      * @return mixed 
  141.      */
  142.     public function current()
  143.     {
  144.         $node current$this->nodeList );
  145.         return $node->data;
  146.     }
  147.  
  148.     /**
  149.      * Advances the internal pointer to the next node in the nodelist.
  150.      */
  151.     public function next()
  152.     {
  153.         $nextElem next$this->nodeList );
  154.         if $nextElem === false )
  155.         {
  156.             $this->valid false;
  157.         }
  158.     }
  159.  
  160.     /**
  161.      * Returns whether the internal pointer is still valid.
  162.      *
  163.      * It returns false when the end of list has been reached.
  164.      *
  165.      * @return bool 
  166.      */
  167.     public function valid()
  168.     {
  169.         return $this->valid;
  170.     }
  171. }
  172. ?>
Documentation generated by phpDocumentor 1.4.3