* * 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 Composite implements Component { protected $children = array(); protected $parent; /** * * @param Component $ comp */ public function add(Component $comp) { $comp->setParent($this); $this->children[] = $comp; return $comp; } /** * * @param Component $ comp */ public function remove(Component $comp) { $index = array_search($comp, $this->children, true); if ($index === false) { return false; } array_splice($this->children, $index, 1); return true; } /** * * @param Component $ comp */ public function setParent(Component $parent) { $this->parent = $parent; } /** * * @return Component Parent Component */ public function getParent() { if (isset ($this->parent)) { return $this->parent; } else throw new Exception("This child has no parents."); } /** * Returns true if this is the root node * * @return Bool */ public function isRoot() { return !isset($this->parent->parent); } /** * Returns true if this Component has no children * * @return Bool */ public function isLeaf() { return empty ($this->children); } /** * Returns the root node. * * @return Component Root node */ public function getRoot() { if ($this->isParent()) { $this->getRootNode(); } else { return $this; } } public function numberOfChildren() { return count($this->children); } public function allChildrenOfType($type) { foreach($this->children as $child) { if(!($child instanceof $type)) return false; } return true; } public function __clone() { $kids = array (); foreach ($this->children as $child) { $kids[] = clone ($child); } $this->children = $kids; } /** * * @return int Number of children */ public function number() { $total = 1; foreach ($this->children as $child) { $total += $child->number(); } return $total; } } ?>