<?php
/**
* СодеÑÐ¶Ð¸Ñ ÐºÐ»Ð°ÑÑ TreeNode
*
* @package energine
* @subpackage core
* @author dr.Pavka
* @copyright ColoCall 2007
* @version $Id: TreeNode.class.php,v 1.3 2007/12/17 14:16:03 pavka Exp $
*/
/**
* ÐлаÑÑ ÑеализÑÑÑий ÑабоÑÑ Ñ Ñзлом деÑева
*
* @package energine
* @subpackage core
* @final
*/
final class TreeNode implements IteratorAggregate{
/**
* ÐденÑиÑикаÑÐ¾Ñ Ñзла
*
* @var int
* @access private
*/
private $id;
/**
* ÐденÑиÑикаÑÐ¾Ñ ÑодиÑелÑÑкого Ñзла
*
* @var TreeNode
* @access private
*/
private $parent = null;
/**
* ÐаÑÑив доÑеÑниÑ
Ñзлов
*
* @var TreeNodeList
* @access private
*/
private $children;
/**
* ÐонÑÑÑÑкÑÐ¾Ñ ÐºÐ»Ð°ÑÑа
*
* @return void
*/
public function __construct($id) {
$this->children = new TreeNodeList();
$this->id = $id;
}
/**
* ÐозвÑаÑÐ°ÐµÑ Ð¸Ð´ÐµÐ½ÑиÑикаÑÐ¾Ñ Ñзла
*
* @return int
* @access public
*/
public function getID() {
return $this->id;
}
/**
* ÐозвÑаÑÐ°ÐµÑ ÑодиÑелÑÑкий Ñзел
*
* @return TreeNode
* @access public
*/
public function getParent() {
return $this->parent;
}
/**
* ÐозвÑаÑÐ°ÐµÑ Ñлаг ÑказÑваÑÑий на налиÑие деÑей
*
* @return bool
* @access public
*/
public function hasChildren() {
return (bool)$this->children->getLength();
}
/**
* ÐозвÑаÑÐ°ÐµÑ Ð¼Ð°ÑÑив поÑомков
*
* @return TreeNodeList
* @access public
*/
public function getChildren() {
return $this->children;
}
/**
* ÐозвÑаÑÐ°ÐµÑ Ð¸ÑеÑаÑÐ¾Ñ Ð¾Ð±ÑекÑа
*
* @see IteratorAggregate
* @return TreeNodeList
* @access public
*/
public function getIterator() {
return $this->getChildren();
}
/**
* Ðобавление Ñзла как доÑеÑнего
*
* @param TreeNode
* @return TreeNode
* @access public
*/
public function addChild(TreeNode $node) {
$node = $this->children->add($node);
$node->parent = $this;
return $node;
}
/**
* Удаление Ñзла из ÑпиÑка доÑеÑниÑ
Ñзлов
*
* @param TreeNode
* @return TreeNode
* @access public
*/
public function removeChild($node) {
$this->children->remove($node)->parent = null;
return $node;
}
/**
* ÐозвÑаÑÐ°ÐµÑ Ð²ÑеÑ
ÑодиÑелей Ñзла
*
* @return TreeNodeList
* @access public
*/
public function getParents() {
$result = new TreeNodeList();
$node = $this;
while (!is_null($node)) {
if (!is_null($node = $node->getParent())) {
$result->add($node);
}
}
return $result;
}
/**
* ÐозвÑаÑÐ°ÐµÑ Ð²ÑеÑ
поÑомков
*
* @return TreeNodeList
* @access public
*/
public function getDescendants() {
$result = $this->iterateDescendants($this->getChildren());
return $result;
}
/**
* ÐнÑÑÑенний меÑод возвÑаÑÐ°ÐµÐ½Ð¸Ñ Ð¿Ð¾Ñомков
*
* @return TreeNodeList
* @access private
*/
private function iterateDescendants(TreeNodeList $nodeList) {
$result = new TreeNodeList();
foreach ($nodeList as $node) {
$result->add($node);
$result->merge($node->iterateDescendants($node->getChildren()));
}
return $result;
}
/**
* ÐозвÑаÑÐ°ÐµÑ Ð¾Ð±ÑÐµÐºÑ Ð² виде маÑÑива
*
* @param bool ÑекÑÑÑиÑ
* @return array
* @access public
*/
public function asList($isRecursive = true) {
$result[$this->getID()] = (!is_null($this->getParent()))?$this->getParent()->getID():null;
if ($this->hasChildren() && $isRecursive) {
$result += $this->getChildren()->asList();
}
return $result;
}
}