Hoa central
Generic.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Tree;
38 
39 use Hoa\Visitor;
40 
49 abstract class Generic
50  implements Visitor\Element,
51  \Iterator,
52  \SeekableIterator,
53  \Countable
54 {
60  protected $_value = null;
61 
67  protected $_childs = [];
68 
69 
76  public function __construct($value = null)
77  {
78  $this->setValue($value);
79 
80  return;
81  }
82 
89  public function setValue($value)
90  {
91  if (!($value instanceof ITree\Node)) {
92  $value = new SimpleNode(md5($value), $value);
93  }
94 
95  $old = $this->_value;
96  $this->_value = $value;
97 
98  return $old;
99  }
100 
106  public function getValue()
107  {
108  return $this->_value;
109  }
110 
116  public function current()
117  {
118  return current($this->_childs);
119  }
120 
126  public function key()
127  {
128  return key($this->_childs);
129  }
130 
136  public function next()
137  {
138  return next($this->_childs);
139  }
140 
146  public function rewind()
147  {
148  return reset($this->_childs);
149  }
150 
157  public function valid()
158  {
159  if (empty($this->_collection)) {
160  return false;
161  }
162 
163  $key = key($this->_collection);
164  $return = (bool) next($this->_childs);
165  prev($this->_collection);
166 
167  if (false === $return) {
168  end($this->_childs);
169  if ($key === key($this->_childs)) {
170  $return = true;
171  }
172  }
173 
174  return $return;
175  }
176 
183  public function seek($position)
184  {
185  if (!array_key_exists($position, $this->_collection)) {
186  return;
187  }
188 
189  $this->rewind();
190 
191  while ($position != $this->key()) {
192  $this->next();
193  }
194 
195  return;
196  }
197 
203  public function count()
204  {
205  return count($this->_childs);
206  }
207 
215  public function getChild($nodeId)
216  {
217  if (false === $this->childExists($nodeId)) {
218  throw new Exception('Child %s does not exist.', 0, $nodeId);
219  }
220 
221  return $this->_childs[$nodeId];
222  }
223 
229  public function getChilds()
230  {
231  return $this->_childs;
232  }
233 
240  public function childExists($nodeId)
241  {
242  return array_key_exists($nodeId, $this->getChilds());
243  }
244 
253  abstract public function insert(Generic $child);
254 
262  abstract public function delete($i);
263 
269  abstract public function isLeaf();
270 
276  abstract public function isNode();
277 
286  public function accept(
287  Visitor\Visit $visitor,
288  &$handle = null,
289  $eldnah = null
290  ) {
291  return $visitor->visit($this, $handle, $eldnah);
292  }
293 }
__construct($value=null)
Definition: Generic.php:76
setValue($value)
Definition: Generic.php:89
getChild($nodeId)
Definition: Generic.php:215
insert(Generic $child)
childExists($nodeId)
Definition: Generic.php:240
accept(Visitor\Visit $visitor, &$handle=null, $eldnah=null)
Definition: Generic.php:286
seek($position)
Definition: Generic.php:183