diff --git a/Behavioral/Iterator/BookList.php b/Behavioral/Iterator/BookList.php index e69825f..f8d0c87 100644 --- a/Behavioral/Iterator/BookList.php +++ b/Behavioral/Iterator/BookList.php @@ -7,7 +7,7 @@ class BookList implements \Countable, \Iterator /** * @var Book[] */ - private $books; + private $books = []; /** * @var int @@ -22,11 +22,12 @@ class BookList implements \Countable, \Iterator public function removeBook(Book $bookToRemove) { foreach ($this->books as $key => $book) { - /** @var Book $book */ if ($book->getAuthorAndTitle() === $bookToRemove->getAuthorAndTitle()) { unset($this->books[$key]); } } + + $this->books = array_values($this->books); } public function count(): int @@ -36,12 +37,12 @@ class BookList implements \Countable, \Iterator public function current(): Book { - return $this->books[array_keys($this->books)[$this->currentIndex]]; + return $this->books[$this->currentIndex]; } public function key(): int { - return array_keys($this->books)[$this->currentIndex]; + return $this->currentIndex; } public function next() @@ -56,6 +57,6 @@ class BookList implements \Countable, \Iterator public function valid(): bool { - return isset(array_keys($this->books)[$this->currentIndex]); + return isset($this->books[$this->currentIndex]); } }