简体   繁体   中英

Nested RecursiveIteraotrs

I want to iterate through nested objects using RecursiveIterator interface. HeadHunterEntity object can have many CandidateEntities. I want to iterate all CandidateEntities in main loop, but something is wrong.

I have main loop like this:

$iterator = new RecursiveIteratorIterator(new RecursiveHunterCandidatesIterator(new RecursiveArrayIterator($this->getHeadHunters())));

foreach ($iterator as $object) {
  echo('<br>MAIN LOOP: ' . (is_object($object) ? get_class($object) : $object));
}

RecursiveHunterCandidatesIterator

class RecursiveHunterCandidatesIterator extends RecursiveFilterIterator {

  public function accept() {
    echo "<br>RecursiveHunterCandidatesIterator (accept) hasChildren: "  . $this->hasChildren();

    return $this->hasChildren();
  }

  public function hasChildren() {
    $current = $this->current();    

    echo "<br>RecursiveHunterCandidatesIterator (hasChildren) current Class: "  .  get_class($current);

    return is_object($this->current()) ? (boolean)count($this->current()->getHuntedCandidates()) : FALSE;
  }

  public function getChildren() {
     echo "<br>RecursiveHunterCandidatesIterator (getChildren) count: "  .  count($this->current()->getHuntedCandidates());

     $childIterator =  new RecursiveArrayIterator($this->current()->getHuntedCandidates());
     $childIterator2 =  new RecursiveArrayIterator(array(1,2,3));

     return $childIterator;
  }
}

The result is unexpected, I have nothin in main loop :(

RecursiveHunterCandidatesIterator (hasChildren) current Class: HeadHunterEntity
RecursiveHunterCandidatesIterator (accept) hasChildren: 1
RecursiveHunterCandidatesIterator (hasChildren) current Class: HeadHunterEntity
RecursiveHunterCandidatesIterator (hasChildren) current Class: HeadHunterEntity
RecursiveHunterCandidatesIterator (getChildren) count: 2
RecursiveHunterCandidatesIterator (hasChildren) current Class: HeadHunterEntity
RecursiveHunterCandidatesIterator (accept) hasChildren: 
RecursiveHunterCandidatesIterator (hasChildren) current Class: HeadHunterEntity
RecursiveHunterCandidatesIterator (hasChildren) current Class: HeadHunterEntity
RecursiveHunterCandidatesIterator (accept) hasChildren: 
RecursiveHunterCandidatesIterator (hasChildren) current Class: HeadHunterEntity

But if I return $childIterator2 instead, there are results in main loop as expected:

    RecursiveHunterCandidatesIterator (hasChildren) current Class: HeadHunterEntity
    RecursiveHunterCandidatesIterator (accept) hasChildren: 1
    RecursiveHunterCandidatesIterator (hasChildren) current Class: HeadHunterEntity
    RecursiveHunterCandidatesIterator (hasChildren) current Class: HeadHunterEntity
    RecursiveHunterCandidatesIterator (getChildren) count: 2
    MAIN LOOP: 1
    MAIN LOOP: 2
    MAIN LOOP: 3
    RecursiveHunterCandidatesIterator (hasChildren) current Class: HeadHunterEntity
    RecursiveHunterCandidatesIterator (accept) hasChildren: 
    RecursiveHunterCandidatesIterator (hasChildren) current Class: HeadHunterEntity
    RecursiveHunterCandidatesIterator (hasChildren) current Class: HeadHunterEntity
    RecursiveHunterCandidatesIterator (accept) hasChildren: 
    RecursiveHunterCandidatesIterator (hasChildren) current Class: HeadHunterEntity

$this->current()->getHuntedCandidates() returns array, so it should be in main loop like fake 1,2,3 aray in $childIterator2...

What do I do wrong ?

I have found a solution. As getChildren() method of RecursiveIterator, has to return instance of RecursiveIterator, I have extended RecursiveArrayIterator (You can also extend RecursiveFilterIterator if you need to filter objects on this level).

class CandidateIterator extends RecursiveArrayIterator {

   public function hasChildren() { 
     return false; 
   }
}

and of course change returned iterator in RecursiveHunterCandidatesIterator class:

class RecursiveHunterCandidatesIterator extends RecursiveFilterIterator {

    (..)

    public function getChildren() {
         $childIterator =  new CandidateIterator(new RecursiveArrayIterator($this->current()->getHuntedCandidates()));

         return $childIterator;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM