PHP RecursiveIterator 遍历

发布于 2024-10-02 01:06:17 字数 4095 浏览 4 评论 0原文

我有一个代表表单的结构,我想使用 RecursiveIterator 对其进行迭代。 问题是这只返回顶级问题。我做错了什么?

完整形式:

class Form implements RecursiveIterator{
    private $id;
    private $caption;
    private $other_text;
    private $questions = array();
    private $current;

    private function __construct(DibiRow $row){
        $this->id = $row->id;
        $this->caption = $row->caption;
        $this->other_text = $row->other_text;
        $this->loadQuestions();
    }

    private function loadQuestions(){
        $questions = dibi::query('SELECT * FROM cyp_questions WHERE form_id = %i AND parent_id IS NULL', $this->id);
        while($question = $questions->fetch()) $this->questions[] = new Question($question->question_id, $question->type, $question->caption, $question->other_text, $question->triggers_unique == 1);
    }

    /**
     * @throws InvalidArgumentException
     * @param $id
     * @return Form
     */
    public static function loadById($id){
        $form = dibi::query('SELECT * FROM cyp_forms WHERE id = %i', $id)->fetch();
        if($form === false) throw new InvalidArgumentException('Form with id '.$id.' was not found.');

        return new Form($form);
    }

    /**
     * @throws FormFieldException
     * @return bool
     */
    public function validate($postfields){

    }

    public function getQuestions(){
        return $this->questions;
    }

    public function getChildren(){
        return $this->questions[$this->current];
    }

    public function hasChildren(){
        return count($this->questions) > 0;
    }

    public function current(){
        return $this->questions[$this->current];
    }

    public function key(){
        return $this->current;
    }

    public function next(){
        $this->current++;
    }

    public function rewind(){
        $this->current = 0;
    }

    public function valid(){
        return isset($this->questions[$this->current]);
    }
}

问题:

class Question implements RecursiveIterator{
    private $id;
    private $type;
    private $answers = array();
    private $subquestions = array();
    private $other_text;
    private $triggers_unique;
    private $caption;
    private $current = 0;


    public function __construct($id, $type, $caption, $other_text = null, $triggers_unique = false){
        $this->id = $id;
        $this->type = $type;
        $this->caption = $caption;
        $this->other_text = $other_text;
        $this->triggers_unique = $triggers_unique;  
        $this->setSubQuestions();   

    }

    private function setSubQuestions(){
        $questions = dibi::query('SELECT * FROM cyp_questions WHERE parent_id = %i', $this->id);
        while($question = $questions->fetch()) $this->subquestions[] = new Question($question->question_id, $question->type, $question->caption, $question->other_text, $question->triggers_unique == 1);
    }

    public function getOtherText(){
        return $this->other_text;
    }

    public function getCaption(){
        return $this->caption;
    }

    public function addAnswer($answer){
        $this->answers[] = $answer;
    }

    public function getChildren(){
        return $this->subquestions[$this->current];
    }

    public function hasChildren(){
        return count($this->subquestions) > 0;
    }

    public function current(){
        return $this->subquestions[$this->current];
    }

    public function key(){
        return $this->id;
    }

    public function next(){
        ++$this->current;
    }

    public function rewind(){
        $this->current = 0; 
    }

    public function valid(){
        return isset($this->subquestions[$this->current]);
    }

    public function getAnswers(){
        return $this->answers;
    }


}

迭代:

$form = Form::loadById(1);

foreach($form as $question){
    echo $question->getCaption().'<br />';  
}

I have a structure representing a form and I want to iterate it using RecursiveIterator.
The problem is this only returns the top-level questions. What am I doing wrong?

Whole form:

class Form implements RecursiveIterator{
    private $id;
    private $caption;
    private $other_text;
    private $questions = array();
    private $current;

    private function __construct(DibiRow $row){
        $this->id = $row->id;
        $this->caption = $row->caption;
        $this->other_text = $row->other_text;
        $this->loadQuestions();
    }

    private function loadQuestions(){
        $questions = dibi::query('SELECT * FROM cyp_questions WHERE form_id = %i AND parent_id IS NULL', $this->id);
        while($question = $questions->fetch()) $this->questions[] = new Question($question->question_id, $question->type, $question->caption, $question->other_text, $question->triggers_unique == 1);
    }

    /**
     * @throws InvalidArgumentException
     * @param $id
     * @return Form
     */
    public static function loadById($id){
        $form = dibi::query('SELECT * FROM cyp_forms WHERE id = %i', $id)->fetch();
        if($form === false) throw new InvalidArgumentException('Form with id '.$id.' was not found.');

        return new Form($form);
    }

    /**
     * @throws FormFieldException
     * @return bool
     */
    public function validate($postfields){

    }

    public function getQuestions(){
        return $this->questions;
    }

    public function getChildren(){
        return $this->questions[$this->current];
    }

    public function hasChildren(){
        return count($this->questions) > 0;
    }

    public function current(){
        return $this->questions[$this->current];
    }

    public function key(){
        return $this->current;
    }

    public function next(){
        $this->current++;
    }

    public function rewind(){
        $this->current = 0;
    }

    public function valid(){
        return isset($this->questions[$this->current]);
    }
}

Question:

class Question implements RecursiveIterator{
    private $id;
    private $type;
    private $answers = array();
    private $subquestions = array();
    private $other_text;
    private $triggers_unique;
    private $caption;
    private $current = 0;


    public function __construct($id, $type, $caption, $other_text = null, $triggers_unique = false){
        $this->id = $id;
        $this->type = $type;
        $this->caption = $caption;
        $this->other_text = $other_text;
        $this->triggers_unique = $triggers_unique;  
        $this->setSubQuestions();   

    }

    private function setSubQuestions(){
        $questions = dibi::query('SELECT * FROM cyp_questions WHERE parent_id = %i', $this->id);
        while($question = $questions->fetch()) $this->subquestions[] = new Question($question->question_id, $question->type, $question->caption, $question->other_text, $question->triggers_unique == 1);
    }

    public function getOtherText(){
        return $this->other_text;
    }

    public function getCaption(){
        return $this->caption;
    }

    public function addAnswer($answer){
        $this->answers[] = $answer;
    }

    public function getChildren(){
        return $this->subquestions[$this->current];
    }

    public function hasChildren(){
        return count($this->subquestions) > 0;
    }

    public function current(){
        return $this->subquestions[$this->current];
    }

    public function key(){
        return $this->id;
    }

    public function next(){
        ++$this->current;
    }

    public function rewind(){
        $this->current = 0; 
    }

    public function valid(){
        return isset($this->subquestions[$this->current]);
    }

    public function getAnswers(){
        return $this->answers;
    }


}

Iteration:

$form = Form::loadById(1);

foreach($form as $question){
    echo $question->getCaption().'<br />';  
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瀞厅☆埖开 2024-10-09 01:06:17

要迭代 RecursiveIterator,您必须将其包装到 RecursiveIteratorIterator 中。

请参阅

默认迭代模式仅列出叶子。如果您还希望包含节点出现在迭代中,请将 RecursiveIteratorIterator::SELF_FIRST 作为第二个参数传递给 RecursiveIteratorIterator 的构造函数

To iterate over a RecursiveIterator, you have to wrap it into a RecursiveIteratorIterator.

See some examples at

The default iteration mode is only to list leaves. If you also want the containing nodes to appear in the iteration, pass RecursiveIteratorIterator::SELF_FIRST as the second argument to the constructor of the RecursiveIteratorIterator

玻璃人 2024-10-09 01:06:17

好吧,正如您在此处所看到的,

public RecursiveIterator RecursiveIterator::getChildren ( void )

Returns an iterator for the current iterator entry. 

该方法应该返回一个实现迭代器。您的方法返回一个简单的数组。

我的猜测是返回类似的内容:

public function getChildren(){
    return new Question($this->subquestions);
}

这是因为您使用的是递归迭代器,因此预计树的每个节点具有相同类型(迭代器)

Well, as you can see here

public RecursiveIterator RecursiveIterator::getChildren ( void )

Returns an iterator for the current iterator entry. 

the method should return an object implementing the iterator. Your method return a simple array.

My guess would be to return something like:

public function getChildren(){
    return new Question($this->subquestions);
}

This is because you're using a RECURSIVE iterator so it's expected to have each node of the tree of the same type (an iterator)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文