Ver Mensaje Individual
  #4 (permalink)  
Antiguo 27/08/2013, 10:11
Nisrokh
 
Fecha de Ingreso: septiembre-2009
Ubicación: Neuquén
Mensajes: 142
Antigüedad: 14 años, 7 meses
Puntos: 12
Respuesta: [APORTE] Estructura de árbol desde de base de datos

Ahora, las implementaciones para la construcción del árbol.

Diba\GenericTree\Node
Código PHP:
Ver original
  1. <?php
  2.  
  3. /**
  4.  * @author Diego P. M. Baltar <[email protected]>
  5.  * @copyright (c) 2013
  6.  * @package Diba
  7.  * @subpackage GenericTree
  8.  */
  9. namespace Diba\GenericTree;
  10.  
  11. use Diba\Tree\Node as INode;
  12. use Diba\Tree\NodeList as INodeList;
  13. use InvalidArgumentException;
  14.  
  15. /**
  16.  * Implementación de \Diba\Tree\Node.
  17.  */
  18. class Node implements INode
  19. {
  20.     /**
  21.      * Colección de nodos hijos.
  22.      *
  23.      * @var \Diba\Tree\NodeList
  24.      */
  25.     protected $children;
  26.    
  27.     /**
  28.      * El nodo padre.
  29.      *
  30.      * @var \Diba\Tree\Node
  31.      */
  32.     protected $parent;
  33.    
  34.     /**
  35.      * El valor del nodo.
  36.      *
  37.      * @var mixed
  38.      */
  39.     protected $value;
  40.    
  41.     // \Diba\Tree\Node
  42.    
  43.     /**
  44.      * Devuelve una colección de los nodos hijos.
  45.      *
  46.      * @return \Diba\Tree\NodeList
  47.      */
  48.     public function getChildren()
  49.     {
  50.         if (!$this->children instanceof INodeList) {
  51.             $this->children = new NodeList();
  52.         }
  53.        
  54.         return $this->children;
  55.     }
  56.    
  57.     /**
  58.      * Devuelve el nodo padre (si existe), o de lo contrario null.
  59.      *
  60.      * @return \Diba\Tree\Node|null
  61.      */
  62.     public function getParent()
  63.     {
  64.         return $this->parent;
  65.     }
  66.    
  67.     /**
  68.      * Devuelve el valor del nodo.
  69.      *
  70.      * @return mixed
  71.      */
  72.     public function getValue()
  73.     {
  74.         return $this->value;
  75.     }
  76.    
  77.     /**
  78.      * Indica si el nodo contiene hijos.
  79.      *
  80.      * @return bool
  81.      */
  82.     public function hasChildren()
  83.     {
  84.         $hasChildren = false;
  85.        
  86.         if (isset($this->children)) {
  87.             if (!$this->children->isEmpty()) {
  88.                 $hasChildren = true;
  89.             }
  90.         }
  91.        
  92.         return $hasChildren;
  93.     }
  94.    
  95.     /**
  96.      * Indica si el nodo contiene padre.
  97.      *
  98.      * @return bool
  99.      */
  100.     public function hasParent()
  101.     {
  102.         return isset($this->parent);
  103.     }
  104.    
  105.     // \Diba\GenericTree\Node
  106.    
  107.     /**
  108.      * Constructor
  109.      *
  110.      * @param mixed $value El valor para el nodo.
  111.      * @param \Diba\Tree\Node $parent El padre de este nodo.
  112.      * @param \Diba\Tree\NodeList $children Los hijos de este nodo.
  113.      * @return \Diba\GenericTree\Node
  114.      */
  115.     public function __construct(
  116.         $value = null,
  117.         INode $parent = null,
  118.         INodeList $children = null
  119.     ) {
  120.         $this->value = $value;
  121.         $this->parent = $parent;
  122.         $this->children = $children;
  123.     }
  124.    
  125.     /**
  126.      * Reestablece el nodo a su estado inicial.
  127.      *
  128.      * @return void
  129.      */
  130.     public function reset()
  131.     {
  132.         $this->children = null;
  133.         $this->parent = null;
  134.         $this->value = null;
  135.     }
  136.    
  137.     /**
  138.      * Establece los hijos de este nodo.
  139.      *
  140.      * @return void
  141.      */
  142.     public function setChildren(INodeList $children)
  143.     {
  144.         $this->children = $children;
  145.     }
  146.    
  147.     /**
  148.      * Establece el padre de este nodo.
  149.      *
  150.      * @return void
  151.      * @throws \InvalidArgumentException
  152.      * When trying to set a node as its own parent.
  153.      */
  154.     public function setParent(INode $node)
  155.     {
  156.         if ($node === $this) {
  157.             throw new InvalidArgumentException(
  158.                 'Cannot set node as its own parent'
  159.             );
  160.         }
  161.        
  162.         $this->parent = $node;
  163.     }
  164.    
  165.     /**
  166.      * Establece el valor de este nodo.
  167.      *
  168.      * @return void
  169.      */
  170.     public function setValue($value)
  171.     {
  172.         $this->value = $value;
  173.     }
  174. }

(Continúa en comentarios)
__________________
Amigos de Foros del Web: seamos más solidarios. ¡No dejemos que un tema se valla al final de las páginas con 0 (cero) respuestas! ¡Gracias por su ayuda! :-)