统一生成器接口

由于内部隐式rewind,需要先调用Generator::current获取当前value,而直接调用Generator::send会跳到第二次yield。

  1. send方法参考
  2. 生成器参考
<?php

class Gen {
    public $isfirst = true;
    public $generator;

    public function __construct(\Generator $generator) {
        $this->generator = $generator;
    }

    public function valid() {
        return $this->generator->valid();
    }

    public function send($value = null) {
        if ($this->isfirst) {
            $this->isfirst = false;
            return $this->generator->current();
        } else {
            return $this->generator->send($value);
        }
    }
}
powered by Gitbook 该教程制作时间: 2017-04-12 13:42:37