<?php
namespace nur\mapper\base;

use nur\A;

/**
 * Class PushProducer: un producer à qui on peut fournir les valeurs à produire
 * au fil de l'eau
 */
class PushProducer extends Producer {
  private $iterables;

  function push($value, $key=null): void {
    $values = $key !== null? [$key => $value]: [$value];
    A::append($this->iterables, $values);
  }

  function pushAll(iterable $values): void {
    A::append($this->iterables, $values);
  }

  function producer() {
    if ($this->iterables !== null) {
      try {
        foreach ($this->iterables as $iterable) {
          yield from $iterable;
        }
      } finally {
        $this->iterables = null;
      }
    }
  }
}
