<?php
/**
* СодеÑÐ¶Ð¸Ñ ÐºÐ»Ð°ÑÑ Tree
*
* @package energine
* @subpackage core
* @author dr.Pavka
* @copyright ColoCall 2007
* @version $Id: TreeNodeList.class.php,v 1.3 2007/12/17 14:16:03 pavka Exp $
*/
//require_once('TreeNode.class.php');
/**
* ÐÐ°Ð±Ð¾Ñ Ñзлов
*
* @package energine
* @subpackage core
*/
class TreeNodeList implements Iterator{
/**
* ÐнÑÑÑенний ÑказаÑелÑ
*
* @var mixed
* @access private
*/
private $currentKey;
/**
* ÐаÑÑив Ñзлов
*
* @var array
* @access private
*/
private $nodeList = array();
/**
* ÐонÑÑÑÑкÑÐ¾Ñ ÐºÐ»Ð°ÑÑа
*
* @return void
*/
public function __construct() {
}
/**
* ÐобавлÑÐµÑ Ñзел
*
* @param TreeNode
* @return TreeNode
* @access public
*/
public function add(TreeNode $node) {
$this->nodeList[$node->getID()] = $node;
return $node;
}
/**
* ÐÑÑавлÑÐµÑ Ñзел пеÑед ÑказаннÑм Ñзлом
*
* @param TreeNode
* @param TreeNode
* @return TreeNode
* @access public
*/
public function insertBefore(TreeNode $node, TreeNode $beforeNode) {
}
/**
* УдалÑÐµÑ ÐµÐ»ÐµÐ¼ÐµÐ½Ñ Ð¸Ð· ÑпиÑка
*
* @param TreeNode
* @return TreeNode
* @access public
*/
public function remove(TreeNode $node) {
$result = $node;
unset($this->nodeList[$node->getID()]);
return $result;
}
/**
* ÐозвÑаÑÐ°ÐµÑ ÐºÐ¾Ð»Ð¸ÑеÑÑво елменÑов в ÑпиÑке
*
* @return int
* @access public
*/
public function getLength() {
return sizeof($this->nodeList);
}
/**
* ÐозвÑаÑÐ°ÐµÑ Ñзел по его иденÑиÑикаÑоÑÑ
*
* @param int
* @return TreeNode
* @access public
*/
public function getNodeById($id) {
return $this->findNode($id, $this);
}
/**
* ÐнÑÑÑенний меÑод поиÑка Ñзла по его иденÑиÑикаÑоÑÑ
*
* @return
* @access private
*/
private function findNode($id, TreeNodeList $nodeList) {
foreach ($nodeList as $node) {
if ($node->getID() == $id) {
return $node;
}
elseif($node->hasChildren()) {
$result = $this->findNode($id, $node->getChildren());
if (!is_null($result)) {
return $result;
}
}
}
return null;
}
/**
* ÐозвÑаÑÐ°ÐµÑ Ð´ÐµÑево в виде маÑÑива
*
* @param bool ÑекÑÑÑиÑ
* @return array
* @access public
*/
public function asList($isRecursive = true) {
$result = array();
foreach ($this as $node) {
$result += $node->asList($isRecursive);
}
return $result;
}
/**
* ÐозвÑаÑÐ°ÐµÑ ÑекÑÑий елеменÑ
*
* @see Iterator
* @return unknown
* @access public
*/
public function current() {
return $this->nodeList[$this->currentKey];
}
/**
* ÐозвÑаÑÐ°ÐµÑ Ð·Ð½Ð°Ñение внÑÑÑеннего ÑказаÑелÑ
*
* @see Iterator
* @return int
* @access public
*/
public function key() {
return $this->currentKey;
}
/**
* УÑÑÐ°Ð½Ð°Ð²Ð»Ð¸Ð²Ð°ÐµÑ Ð²Ð½ÑÑÑенний ÑказаÑÐµÐ»Ñ Ð½Ð° поÑледний елеменÑ
*
* @see Iterator
* @return void
* @access public
*/
public function next() {
//полÑÑаем вÑе клÑÑи
$keys = array_keys($this->nodeList);
//менÑем меÑÑами клÑÑ Ñо знаÑением, полÑÑÐ°Ñ Ð¸Ð½Ð´ÐµÐºÑÑ
$indexes = array_flip($keys);
//полÑÑаем Ð¸Ð½Ð´ÐµÐºÑ ÑекÑÑего клÑÑа
$currentIndex = $indexes[$this->currentKey];
$currentIndex++;
if(isset($keys[$currentIndex])) {
$this->currentKey = $keys[$currentIndex];
}
else {
$this->currentKey = null;
}
}
/**
* УÑÑÐ°Ð½Ð°Ð²Ð»Ð¸Ð²Ð°ÐµÑ Ð²Ð½ÑÑÑенний ÑказаÑÐµÐ»Ñ Ð½Ð° пеÑвÑй елеменÑ
*
* @see Iterator
* @return void
* @access public
*/
public function rewind() {
if(empty($this->nodeList)) return;
//полÑÑаем вÑе клÑÑи
$keys = array_keys($this->nodeList);
//менÑем меÑÑами клÑÑ Ñо знаÑением, полÑÑÐ°Ñ Ð¸Ð½Ð´ÐµÐºÑÑ
$this->currentKey = $keys[0];
}
/**
* ÐозвÑаÑÐ°ÐµÑ bool в завиÑимоÑÑи Ð¾Ñ Ñого ÑвлÑеÑÑÑ Ð»Ð¸ ÑекÑÑий ÐµÐ»ÐµÐ¼ÐµÐ½Ñ Ð¿Ð¾Ñледним или Ð½ÐµÑ Ð² ÑпиÑке
*
* @see Iterator
* @return boolean
* @access public
*/
public function valid() {
if(!is_null($this->currentKey)){
$keys = array_keys($this->nodeList);
$indexes = array_flip($keys);
if(!isset($indexes[$this->currentKey])) {
$result = false;
}
else {
$result = $indexes[$this->currentKey] < sizeof($indexes);
}
}
else {
$result = false;
}
return $result;
}
/**
* ÐеÑеÑÐµÐºÐ°ÐµÑ ÑпиÑки Ñзлов
*
* @param TreeNodeList
* @return TreeNodeList
* @access public
*/
public function merge(TreeNodeList $newNodeList) {
$this->nodeList = array_merge($this->nodeList, $newNodeList->nodeList);
return $this;
}
}