简体   繁体   English

嵌套递归迭代器

[英]Nested RecursiveIteraotrs

I want to iterate through nested objects using RecursiveIterator interface. 我想使用RecursiveIterator接口遍历嵌套对象。 HeadHunterEntity object can have many CandidateEntities. HeadHunterEntity对象可以具有许多CandidateEntities。 I want to iterate all CandidateEntities in main loop, but something is wrong. 我想在主循环中迭代所有CandidateEntities,但是出了点问题。

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 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: 但是,如果我改为返回$ childIterator2,则主循环中会出现预期的结果:

    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... $ this-> current()-> getHuntedCandidates()返回数组,因此它应该像$ childIterator2中的假1,2,3一样在主循环中...

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). 由于RecursiveIterator的getChildren()方法必须返回RecursiveIterator的实例,因此我扩展了RecursiveArrayIterator(如果需要在此级别上过滤对象,也可以扩展RecursiveFilterIterator)。

class CandidateIterator extends RecursiveArrayIterator {

   public function hasChildren() { 
     return false; 
   }
}

and of course change returned iterator in RecursiveHunterCandidatesIterator class: 并且当然会在RecursiveHunterCandidatesIterator类中更改返回的迭代器:

class RecursiveHunterCandidatesIterator extends RecursiveFilterIterator {

    (..)

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

         return $childIterator;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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