Skip to content

Commit

Permalink
Update ClosureIterator.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Sep 17, 2019
1 parent 5e105da commit f691cfb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 38 deletions.
12 changes: 0 additions & 12 deletions spec/drupol/collection/Iterator/ClosureIteratorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,6 @@ public function it_can_get_a_key(): void
->shouldReturn(0);
}

public function it_can_send(): void
{
$callback = static function () {
yield from \range(1, 5);
};

$this
->beConstructedWith($callback);

$this->send('foo');
}

public function it_can_use_next(): void
{
$callback = static function () {
Expand Down
38 changes: 12 additions & 26 deletions src/Iterator/ClosureIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ public function __construct(callable $callable, ...$arguments)
*/
public function current()
{
if (null === $this->generator) {
$this->rewind();
}
$this->initGenerator();

return $this->generator->current();
}
Expand All @@ -55,9 +53,7 @@ public function current()
*/
public function key()
{
if (null === $this->generator) {
$this->rewind();
}
$this->initGenerator();

return $this->generator->key();
}
Expand All @@ -69,9 +65,7 @@ public function key()
*/
public function next()
{
if (null === $this->generator) {
$this->rewind();
}
$this->initGenerator();

$this->generator->next();

Expand All @@ -83,36 +77,28 @@ public function next()
*/
public function rewind(): void
{
$this->generator = ($this->source)();
$this->initGenerator();
}

/**
* Send a value to the inner Generator.
*
* @param null|mixed $value
* {@inheritdoc}
*
* @return mixed
* @return bool
*/
public function send($value = null)
public function valid()
{
if (null === $this->generator) {
$this->rewind();
}
$this->initGenerator();

return $this->generator->send($value);
return $this->generator->valid();
}

/**
* {@inheritdoc}
*
* @return bool
* Init the generator if not initialized yet.
*/
public function valid()
private function initGenerator(): void
{
if (null === $this->generator) {
$this->rewind();
$this->generator = ($this->source)();
}

return $this->generator->valid();
}
}

0 comments on commit f691cfb

Please sign in to comment.