NodeValueIterator.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
11 namespace wcmf\lib\model;
12 
15 
16 /**
17  * NodeValueIterator is used to iterate over all persistent values of a Node
18  * (not including relations).
19  *
20  * The following example shows the usage:
21  *
22  * @code
23  * // load the node with depth 10
24  * $node = ObjectFactory::getInstance('persistenceFacade')->load(new ObjectId('Page', 300), 10);
25  *
26  * // iterate over all values
27  * $it = new NodeValueIterator($node);
28  * foreach($it as $name => $value) {
29  * echo("current attribute name: ".$name);
30  * echo("current attribute value: ".$value);
31  * }
32  *
33  * // iterate over all values with access to the current object
34  * for($it->rewind(); $it->valid(); $it->next()) {
35  * echo("current object: ".$it->currentNode());
36  * echo("current attribute name: ".$it->key());
37  * echo("current attribute value: ".$it->current());
38  * }
39  * @endcode
40  *
41  * @author ingo herwig <ingo@wemove.com>
42  */
43 class NodeValueIterator implements \Iterator {
44 
45  protected $end; // indicates if the iteration is ended
46  protected $recursive; // indicates if the iterator should also process child nodes
47  protected $nodeIterator; // the NodeIterator used internally
48  protected $currentAttributes; // the list of attributes of the current node
49  protected $currentAttribute; // the attribute the iterator points to
50 
51  /**
52  * Constructor.
53  * @param $node The node to start from.
54  * @param $recursive Boolean whether the iterator should also process child nodes
55  */
56  public function __construct($node, $recursive) {
57  $this->recursive = $recursive;
58  $this->nodeIterator = new NodeIterator($node);
59  $this->currentAttributes = $node->getValueNames(false);
60  $this->currentAttribute = current($this->currentAttributes);
61  $this->end = sizeof($this->currentAttributes) == 0;
62  }
63 
64  /**
65  * Return the current element
66  * @return Value of the current attribute
67  */
68  public function current() {
69  $node = $this->nodeIterator->current();
70  return $node->getValue($this->currentAttribute);
71  }
72 
73  /**
74  * Return the key of the current element
75  * @return String, the name of the current attribute
76  */
77  public function key() {
79  }
80 
81  /**
82  * Move forward to next element
83  */
84  public function next() {
85  $next = next($this->currentAttributes);
86  if ($next !== false) {
87  $this->currentAttribute = $next;
88  }
89  else {
90  if ($this->recursive) {
91  $this->nodeIterator->next();
92  if (!$this->nodeIterator->valid()) {
93  $this->end = true;
94  }
95  else {
96  $nextNode = $this->nodeIterator->current();
97  $this->currentAttributes = $nextNode->getValueNames(false);
98  $this->currentAttribute = current($this->currentAttributes);
99  }
100  }
101  else {
102  $this->end = true;
103  }
104  }
105  return $this;
106  }
107 
108  /**
109  * Rewind the Iterator to the first element
110  */
111  public function rewind() {
112  $this->nodeIterator->rewind();
113  $this->currentAttributes = $this->nodeIterator->current()->getValueNames(false);
114  $this->currentAttribute = current($this->currentAttributes);
115  $this->end = (sizeof($this->currentAttributes) == 0);
116  }
117 
118  /**
119  * Checks if current position is valid
120  */
121  public function valid() {
122  return !$this->end;
123  }
124 
125  /**
126  * Get the current node
127  * @return Node instance
128  */
129  public function currentNode() {
130  return $this->nodeIterator->current();
131  }
132 }
133 ?>
NodeValueIterator is used to iterate over all persistent values of a Node (not including relations).
valid()
Checks if current position is valid.
key()
Return the key of the current element.
NodeIterator is used to iterate over a tree/list built of Nodes using a Depth-First-Algorithm.
__construct($node, $recursive)
Constructor.
rewind()
Rewind the Iterator to the first element.
current()
Return the current element.
Node adds the concept of relations to PersistentObject.
Definition: Node.php:34
next()
Move forward to next element.
Node related interfaces and classes.
Definition: namespaces.php:26
currentNode()
Get the current node.