- array_reverse: documentation ( source)
- func_get_args: documentation ( source)
- strtoupper: documentation ( source)
- strrev: documentation ( source)
- call_user_func_array: documentation ( source)
<?php
namespace GeneratorExample;
class Pipeline
{
protected $generators = [];
function __construct()
{
}
function add(callable $generator)
{
$this->generators[] = $generator;
return $this;
}
function resolve()
{
$inputs = func_get_args();
foreach (array_reverse($this->generators) as $g) {
$generator = call_user_func_array($g, $inputs);
$inputs = [$generator];
}
return $generator;
}
}
function uppercase(\Traversable $in)
{
foreach ($in as $word) {
yield strtoupper($word);
}
}
function reverse(\Traversable $in)
{
foreach ($in as $word) {
yield strrev($word);
}
}
function each_println(\Traversable $in)
{
foreach ($in as $line) {
echo "$line\n";
}
}
$words = new \ArrayIterator(["foo", "bar", "baz"]);
$p = new Pipeline;
$p->add("GeneratorExample\\each_println")->add("GeneratorExample\\uppercase")->add("GeneratorExample\\reverse");
$p->resolve($words);