#232 removed Iterator classes

This commit is contained in:
Dominik Liebler
2016-09-21 14:34:24 +02:00
parent 8887a564c2
commit d5b8a56425
8 changed files with 562 additions and 362 deletions

View File

@@ -2,18 +2,17 @@
namespace DesignPatterns\Behavioral\Iterator;
class BookList implements \Countable
class BookList implements \Countable, \Iterator
{
/**
* @var Book[]
*/
private $books;
public function getBook($bookNumberToGet)
{
if (isset($this->books[$bookNumberToGet])) {
return $this->books[$bookNumberToGet];
}
return;
}
/**
* @var int
*/
private $currentIndex = 0;
public function addBook(Book $book)
{
@@ -30,8 +29,33 @@ class BookList implements \Countable
}
}
public function count()
public function count(): int
{
return count($this->books);
}
public function current(): Book
{
return $this->books[array_keys($this->books)[$this->currentIndex]];
}
public function key(): int
{
return array_keys($this->books)[$this->currentIndex];
}
public function next()
{
$this->currentIndex++;
}
public function rewind()
{
$this->currentIndex = 0;
}
public function valid(): bool
{
return isset(array_keys($this->books)[$this->currentIndex]);
}
}