* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. */ abstract class ListComposite implements ListComponent, Iterator { public $children = array (); private $parent; /** * Whether current element is valid or not */ private $valid; function add(ListComponent $comp) { #$comp->setParent($this); $this->children[] = $comp; return $comp; } function removeChild(ListComponent $comp) { $index = array_search($comp, $this->children, true); if ($index === false) { return false; } array_splice($this->children, $index, 1); return true; } function remove() { $this->parent->removeChild($this); } function setParent(ListComponent $parent) { $this->parent = $parent; } function getPos() { return $this->pos; } function getParent() { if (isset ($this->parent)) { return $this->parent; } else { return false; } } function isLeaf() { return empty ($this->children); } function getRootNode($node = null) { echo $node->name . "\n"; if ($this->getParent()) { $this->getRootNode($this->getParent()); } else { return $this; } } function __clone() { $kids = array (); foreach ($this->children as $child) { $kids[] = clone ($child); } $this->children = $kids; } function number() { $total = 1; foreach ($this->children as $child) { $total += $child->number(); } return $total; } /** * Iterator::current() - Returns the current line of the file. * * @return string Current line of file */ public function current() { return current($this->children); } /** * Iterator::key() - Returns the current "key". In this instance, key() + 1 would * give you the current line number. * * @return string Current elements key */ public function key() { return key($this->children); } /** * Iterator::next() - Advances the iterator to the next "element" */ public function next() { if(next($this->children) === false) { $this->valid = false; return false; } else { return true; } } /** * Iterator::rewind() - Resets the iterator to the "beginning" */ public function rewind() { reset($this->children); $this->valid = true; } /** * Iterator::valid() - Returns true/false as to whether a call to current() * is valid/possible or not. * * @return bool Whether current element is valid or not */ public function valid() { return $this->valid; } } ?>