Ver Mensaje Individual
  #7 (permalink)  
Antiguo 24/04/2011, 15:07
Avatar de masterpuppet
masterpuppet
Software Craftsman
 
Fecha de Ingreso: enero-2008
Ubicación: Montevideo, Uruguay
Mensajes: 3.550
Antigüedad: 16 años, 3 meses
Puntos: 845
Respuesta: Problema con definicion de una clase

La documentación dice que tiene que devolver un RecursiveIterator, en tu caso los nodos implementan dicha interface, así que lo que único que tienes que hacer es devolver el elemento actual.

Código PHP:
Ver original
  1. public function getChildren()
  2. {
  3.     return $this->current();
  4. }

Aunque yo no utilizaría un contador, podría ser así(deje solo esencial, para que quede mas claro el ejemplo):


Código PHP:
Ver original
  1. class Node implements RecursiveIterator, Countable
  2. {
  3.     /**
  4.      * @var string
  5.      */
  6.     private $name;
  7.  
  8.     /**
  9.      * @var array
  10.      */
  11.     private $childrens = array();
  12.  
  13.     /**
  14.      * @var Node
  15.      */
  16.     private $parent;
  17.  
  18.     /**
  19.      * @param string $name
  20.      * @param Node|null $parent
  21.      */
  22.     public function __construct($name, $parent = null)
  23.     {
  24.         $this->name   = $name;
  25.         $this->parent = $parent;
  26.     }
  27.  
  28.     /**
  29.      * @param Node $n
  30.      * @return Node provide fluent interface
  31.      */
  32.     public function addChild(Node $n)
  33.     {
  34.         $n->setParent($this);
  35.         $this->childrens[] = $n;
  36.         return $this;
  37.     }
  38.  
  39.     /**
  40.      * @return string $name
  41.      */
  42.     public function getName()
  43.     {
  44.         return $this->name;
  45.     }
  46.  
  47.     /**
  48.      * @param Node $n
  49.      * @return Node provide fluent interface
  50.      */
  51.     public function setParent(Node $n)
  52.     {
  53.         $this->parent = $n;
  54.         return $this;
  55.     }
  56.  
  57.     /**
  58.      * Countable Interface
  59.      */
  60.     public function count()
  61.     {
  62.         return count($this->childrens);
  63.     }
  64.  
  65.     /**
  66.      * RecrusiveIterator Interface
  67.      */
  68.     public function getChildren()
  69.     {
  70.         return $this->current();
  71.     }
  72.  
  73.     public function hasChildren()
  74.     {
  75.         return count($this->childrens) > 0 ? true : false;
  76.     }
  77.  
  78.  
  79.     public function rewind()
  80.     {
  81.         reset($this->childrens);
  82.     }
  83.  
  84.     public function current()
  85.     {
  86.         return current($this->childrens);
  87.     }
  88.  
  89.     public function key(){
  90.         return key($this->childrens);
  91.     }
  92.  
  93.     public function next()
  94.     {
  95.         next($this->childrens);
  96.     }
  97.  
  98.     public function valid()
  99.     {
  100.         return false !== $this->current();
  101.     }
  102.  
  103. }

Debes recordar que para iterar sobre todos los nodos hay que envolver el RecursiveIterator en un RecursiveIteratorIterator, un ejemplo:

Código PHP:
Ver original
  1. $node = new Node('Root');
  2.  
  3. $child1  = new Node('Node 1');
  4. $child1->addChild(new Node('Node 1.1'));
  5. $child1->addChild(new Node('Node 1.2'));
  6. $child1->addChild(new Node('Node 1.3'));
  7. $node->addChild($child1);
  8. $node->addChild(new Node('Node 2'));
  9. $node->addChild(new Node('Node 3'));
  10.  
  11. foreach(new RecursiveIteratorIterator($node,
  12.                 RecursiveIteratorIterator::SELF_FIRST) as $child) {
  13.     echo $child->getName() . ' :: ' . count($child) . PHP_EOL;
  14. }

En cuanto a que trabajas con XML, hay un iterator para ese caso tambien SimpleXMLIterator, el cual implementa RecursiveIterator y Countable.

Saludos.
__________________
http://es.phptherightway.com/
thats us riders :)

Última edición por masterpuppet; 24/04/2011 a las 16:24 Razón: typo