energine
[ class tree: energine ] [ index: energine ] [ all elements ]

Source for file TreeNodeList.class.php

Documentation is available at TreeNodeList.class.php

  1. <?php
  2.  
  3. /**
  4.  * Содержит класс Tree
  5.  *
  6.  * @package energine
  7.  * @subpackage core
  8.  * @author dr.Pavka
  9.  * @copyright Energine 2007
  10.  * @version $Id$
  11.  */
  12.  
  13. //require_once('TreeNode.class.php');
  14.  
  15. /**
  16.  * Набор узлов
  17.  *
  18.  * @package energine
  19.  * @subpackage core
  20.  * @author dr.Pavka
  21.  */
  22. class TreeNodeList implements Iterator{
  23.     /**
  24.      * Внутренний указатель
  25.      *
  26.      * @var mixed 
  27.      * @access private
  28.      */
  29.     private $currentKey;
  30.  
  31.     /**
  32.      * Массив узлов
  33.      *
  34.      * @var array 
  35.      * @access private
  36.      */
  37.     private $nodeList = array();
  38.  
  39.     /**
  40.      * Конструктор класса
  41.      *
  42.      * @return void 
  43.      */
  44.     public function __construct({
  45.  
  46.     }
  47.  
  48.     /**
  49.      * Добавляет узел
  50.      *
  51.      * @param TreeNode 
  52.      * @return TreeNode 
  53.      * @access public
  54.      */
  55.  
  56.     public function add(TreeNode $node{
  57.         $this->nodeList[$node->getID()$node;
  58.         return $node;
  59.     }
  60.  
  61.     /**
  62.      * Вставляет узел перед указанным узлом
  63.      *
  64.      * @param TreeNode 
  65.      * @param TreeNode 
  66.      * @return TreeNode 
  67.      * @access public
  68.      */
  69.  
  70.     public function insertBefore(TreeNode $nodeTreeNode $beforeNode{
  71.  
  72.     }
  73.  
  74.     /**
  75.      * Удаляет елемент из списка
  76.      *
  77.      * @param TreeNode 
  78.      * @return TreeNode 
  79.      * @access public
  80.      */
  81.  
  82.     public function remove(TreeNode $node{
  83.         $result $node;
  84.         unset($this->nodeList[$node->getID()]);
  85.  
  86.         return $result;
  87.     }
  88.  
  89.     /**
  90.      * Возвращает количество елментов в списке
  91.      *
  92.      * @return int 
  93.      * @access public
  94.      */
  95.  
  96.     public function getLength({
  97.         return sizeof($this->nodeList);
  98.     }
  99.  
  100.     /**
  101.      * Возвращает узел по его идентификатору
  102.      *
  103.      * @param int 
  104.      * @return TreeNode 
  105.      * @access public
  106.      */
  107.  
  108.     public function getNodeById($id{
  109.         return $this->findNode($id$this);
  110.     }
  111.  
  112.     /**
  113.      * Внутренний метод поиска узла по его идентификатору
  114.      *
  115.      * @return mixed 
  116.      * @access private
  117.      */
  118.  
  119.     private function findNode($idTreeNodeList $nodeList{
  120.         foreach ($nodeList as $node{
  121.             if ($node->getID(== $id{
  122.                 return $node;
  123.             }
  124.             elseif($node->hasChildren()) {
  125.                 $result $this->findNode($id$node->getChildren());
  126.                 if (!is_null($result)) {
  127.                     return $result;
  128.                 }
  129.             }
  130.         }
  131.         return null;
  132.     }
  133.  
  134.     /**
  135.      * Возвращает дерево в виде массива
  136.      *
  137.      * @param bool рекурсия
  138.      * @return array 
  139.      * @access public
  140.      */
  141.  
  142.     public function asList($isRecursive true{
  143.         $result array();
  144.         foreach ($this as $node{
  145.             $result += $node->asList($isRecursive);
  146.         }
  147.  
  148.         return $result;
  149.     }
  150.  
  151.     /**
  152.      * Возвращает текущий елемент
  153.      *
  154.      * @see Iterator
  155.      * @return unknown 
  156.      * @access public
  157.      */
  158.     public function current({
  159.         return $this->nodeList[$this->currentKey];
  160.     }
  161.  
  162.     /**
  163.      * Возвращает значение внутреннего указателя
  164.      *
  165.      * @see Iterator
  166.      * @return int 
  167.      * @access public
  168.      */
  169.     public function key({
  170.         return $this->currentKey;
  171.     }
  172.  
  173.     /**
  174.      * Устанавливает внутренний указатель на последний елемент
  175.      *
  176.      * @see Iterator
  177.      * @return void 
  178.      * @access public
  179.      */
  180.     public function next({
  181.         //получаем все ключи
  182.         $keys array_keys($this->nodeList);
  183.         //меняем местами ключ со значением, получая индексы
  184.         $indexes array_flip($keys);
  185.         //получаем индекс текущего ключа
  186.         $currentIndex $indexes[$this->currentKey];
  187.         $currentIndex++;
  188.         if(isset($keys[$currentIndex])) {
  189.             $this->currentKey = $keys[$currentIndex];
  190.         }
  191.         else {
  192.             $this->currentKey = null;
  193.         }
  194.     }
  195.  
  196.     /**
  197.      * Устанавливает внутренний указатель на первый елемент
  198.      *
  199.      * @see Iterator
  200.      * @return void 
  201.      * @access public
  202.      */
  203.     public function rewind({
  204.         if(empty($this->nodeList)) return;
  205.         //получаем все ключи
  206.         $keys array_keys($this->nodeList);
  207.         //меняем местами ключ со значением, получая индексы
  208.         $this->currentKey = $keys[0];
  209.     }
  210.  
  211.     /**
  212.      * Возвращает bool в зависимости от того является ли текущий елемент последним или нет в списке
  213.      *
  214.      * @see Iterator
  215.      * @return boolean 
  216.      * @access public
  217.      */
  218.     public function valid({
  219.         if(!is_null($this->currentKey)){
  220.             $keys array_keys($this->nodeList);
  221.             $indexes array_flip($keys);
  222.             if(!isset($indexes[$this->currentKey])) {
  223.                 $result false;
  224.             }
  225.             else {
  226.                 $result $indexes[$this->currentKeysizeof($indexes);
  227.             }
  228.         }
  229.         else {
  230.             $result false;
  231.         }
  232.         return $result;
  233.     }
  234.  
  235.     /**
  236.      * Пересекает списки узлов
  237.      *
  238.      * @param TreeNodeList 
  239.      * @return TreeNodeList 
  240.      * @access public
  241.      */
  242.  
  243.     public function merge(TreeNodeList $newNodeList{
  244.         $this->nodeList = array_merge($this->nodeList$newNodeList->nodeList);
  245.         return $this;
  246.     }
  247. }
В создании документации нам помог: phpDocumentor