Ver Mensaje Individual
  #6 (permalink)  
Antiguo 27/08/2013, 10:14
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

Diba\GenericTree\TableTreeOptions
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.  * Opciones utilizdas por una instancia de \Diba\GenericTree\TableTree.
  17.  */
  18. class TableTreeOptions
  19. {
  20.     /**
  21.      * El comparador utilizado para ordenar los nodos, o "flags" utlizados por
  22.      * la función usort().
  23.      *
  24.      * @see \Diba\GenericTree\NodeList::getComparator()
  25.      * @see \Diba\GenericTree\NodeList::sort()
  26.      *
  27.      * @var callable|int
  28.      */
  29.     private $comparator;
  30.    
  31.     /**
  32.      * Nombre de la clave del id de entrada.
  33.      *
  34.      * @var string
  35.      */
  36.     private $idKey;
  37.    
  38.     /**
  39.      * Nombre de la clave del id padre de entrada.
  40.      *
  41.      * @var string
  42.      */
  43.     private $parentIdKey;
  44.    
  45.     /**
  46.      * Constructor
  47.      *
  48.      * @param string $idKey Clave de entrada del id.
  49.      * @param string $parentIdKey Clave de entrada del id padre.
  50.      * @param callable|int $comparator El comparador de nodos.
  51.      * @return \Diba\GenericTree\TableTreeOptions
  52.      * @throws \InvalidArgumentException
  53.      * Cuando los argumentos $idKey y/o $parentIdKey no son de tipo string.
  54.      * @throws \InvalidArgumentException
  55.      * Cuando el argumento $comparator no es de tipo callable o integer.
  56.      */
  57.     public function __construct(
  58.         $idKey = 'id',
  59.         $parentIdKey = 'parentId',
  60.         $comparator = \SORT_REGULAR
  61.     ) {
  62.         $this->setComparator($comparator);
  63.         $this->setIdKey($idKey);
  64.         $this->setParentIdKey($parentIdKey);
  65.     }
  66.    
  67.     /**
  68.      * Devuelve el comparador establecido.
  69.      *
  70.      * @return callable|int
  71.      */
  72.     public function getComparator()
  73.     {
  74.         return $this->comparator;
  75.     }
  76.    
  77.     /**
  78.      * Devuelve el nombre de la clave id establecida.
  79.      *
  80.      * @return string
  81.      */
  82.     public function getIdKey()
  83.     {
  84.         return $this->idKey;
  85.     }
  86.    
  87.     /**
  88.      * Devuelve el nombre de la clave id padre establecida.
  89.      *
  90.      * @return string
  91.      */
  92.     public function getParentIdKey()
  93.     {
  94.         return $this->parentIdKey;
  95.     }
  96.    
  97.     /**
  98.      * Establece el comparador, y devuelve el anterior.
  99.      *
  100.      * @param callable|int $comparator El nuevo comparador.
  101.      * @return callable|int
  102.      * @throws \InvalidArgumentException
  103.      * Cuando el argumento $comparator no es de tipo callable o integer.
  104.      */
  105.     public function setComparator($comparator)
  106.     {
  107.         if (is_callable($comparator) || is_int($comparator)) {
  108.             $previousComparator = $this->comparator;
  109.             $this->comparator = $comparator;
  110.            
  111.             return $previousComparator;
  112.         } else {
  113.             throw new InvalidArgumentException(sprintf(
  114.                 'Invalid $comparator type: callable/int expected, %s given',
  115.                 gettype($comparator)
  116.             ));
  117.         }
  118.     }
  119.    
  120.     /**
  121.      * Establece el nombre de la clave del id, y devuelve el anterior.
  122.      *
  123.      * @param string $name La nueva clave del id.
  124.      * @return string
  125.      * @throws \InvalidArgumentException
  126.      * Cuando el argumento $name no es de tipo string.
  127.      */
  128.     public function setIdKey($name)
  129.     {
  130.         if (!is_string($name)) {
  131.             throw new InvalidArgumentException(sprintf(
  132.                 'Invalid $name type: string expected, %s given',
  133.                 gettype($name)
  134.             ));
  135.         }
  136.        
  137.         $previousIdKey = $this->idKey;
  138.         $this->idKey = $name;
  139.        
  140.         return $previousIdKey;
  141.     }
  142.    
  143.     /**
  144.      * Establece el nombre de la clave del id padre, y devuelve el anterior.
  145.      *
  146.      * @param string $name La nueva clave del id padre.
  147.      * @return string
  148.      * @throws \InvalidArgumentException
  149.      * Cuando el argumento $name no es de tipo string.
  150.      */
  151.     public function setParentIdKey($name)
  152.     {
  153.         if (!is_string($name)) {
  154.             throw new InvalidArgumentException(sprintf(
  155.                 'Invalid $name type: string expected, %s given',
  156.                 gettype($name)
  157.             ));
  158.         }
  159.        
  160.         $previousParentIdKey = $this->parentIdKey;
  161.         $this->parentIdKey = $name;
  162.        
  163.         return $previousParentIdKey;
  164.     }
  165. }

(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! :-)