wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.NodeIterator.php
Go to the documentation of this file.
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2014 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of any of the following licenses
7  * at your choice:
8  *
9  * - GNU Lesser General Public License (LGPL)
10  * http://www.gnu.org/licenses/lgpl.html
11  * - Eclipse Public License (EPL)
12  * http://www.eclipse.org/org/documents/epl-v10.php
13  *
14  * See the license.txt file distributed with this work for
15  * additional information.
16  *
17  * $Id: class.NodeIterator.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 
20 /**
21  * @class NodeIterator
22  * @ingroup Model
23  * @brief NodeIterator is used to iterate over a tree/list build of objects
24  * using a Depth-First-Algorithm. Classes used with the NodeIterator must
25  * implement the getChildren() and getOID() methods.
26  * NodeIterator implements the 'Iterator Pattern'.
27  * The base class NodeIterator defines the interface for all
28  * specialized Iterator classes.
29  *
30  * @author ingo herwig <ingo@wemove.com>
31  */
33 {
34  var $_end; // indicates if the iteration is ended
35  var $_objList; // the list of seen objects
36  var $_objIdList; // the list of seen object ids
37  var $_currentObj; // the object the iterator points to
38  /**
39  * Constructor.
40  * @param obj The object to start from.
41  */
42  function NodeIterator(&$obj)
43  {
44  $this->_end = false;
45  $this->_objList = array();
46  $this->_objIdList = array();
47  $this->_currentObj = &$obj;
48  }
49  /**
50  * Proceed to next object.
51  * Subclasses may override this method to implement spezial traversion algorithms.
52  * @return A reference to the NodeIterator.
53  */
54  function &proceed()
55  {
56  $childrenArray = $this->_currentObj->getChildren();
57  $this->addToSeenList($childrenArray);
58 
59  if (sizeOf($this->_objList) != 0)
60  {
61  // array_pop destroys the reference to the object
62  $this->_currentObj = & $this->_objList[sizeOf($this->_objList)-1];
63  array_pop($this->_objList);
64  }
65  else
66  {
67  $this->_end = true;
68  }
69  return $this;
70  }
71  /**
72  * Get the current object.
73  * Subclasses may override this method to return only special objects.
74  * @return A reference to the current object.
75  */
76  function &getCurrentObject()
77  {
78  return $this->_currentObj;
79  }
80  /**
81  * Find out whether iteration is finished.
82  * @return 'True' if iteration is finished, 'False' alternatively.
83  */
84  function isEnd()
85  {
86  return $this->_end;
87  }
88  /**
89  * Reset the iterator to given object.
90  * @param obj The object to start from.
91  */
92  function reset(&$obj)
93  {
94  $this->_end = false;
95  $this->_objList = array();
96  $this->_objIdList = array();
97  $this->_currentObj = &$obj;
98  }
99  /**
100  * Add objects, only if they are not already in the internal processed object list.
101  * @attention Internal use only.
102  * @param objList An array of objects.
103  */
104  //
105  function addToSeenList($objList)
106  {
107  for ($i=sizeOf($objList)-1;$i>=0;$i--)
108  {
109  if (is_a($objList[$i], 'node') || is_a($objList[$i], 'Node'))
110  {
111  if (!in_array($objList[$i]->getOID(), $this->_objIdList))
112  {
113  $this->_objList[sizeOf($this->_objList)] = &$objList[$i];
114  array_push($this->_objIdList, $objList[$i]->getOID());
115  }
116  }
117  }
118  }
119 }
120 ?>
NodeIterator is used to iterate over a tree/list build of objects using a Depth-First-Algorithm. Classes used with the NodeIterator must implement the getChildren() and getOID() methods. NodeIterator implements the 'Iterator Pattern'. The base class NodeIterator defines the interface for all specialized Iterator classes.
addToSeenList($objList)