0

RecursiveIterator インターフェイスを使用して、ネストされたオブジェクトを反復処理したいと考えています。HeadHunterEntity オブジェクトは、多数の CandidateEntities を持つことができます。メイン ループですべての CandidateEntities を反復処理したいのですが、何か問題があります。

私はこのようなメインループを持っています:

$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;
  }
}

結果は予想外です。メインループには何もありません:(

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

しかし、代わりに $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() は配列を返すので、$childIterator2 の偽の 1,2,3 配列のようにメイン ループにある必要があります...

私は何を間違っていますか?

4

1 に答える 1