diff --git a/Behavioral/Iterator/BookList.php b/Behavioral/Iterator/BookList.php index 6fcf265..9c0b9d4 100644 --- a/Behavioral/Iterator/BookList.php +++ b/Behavioral/Iterator/BookList.php @@ -9,7 +9,7 @@ class BookList implements \Countable public function getBook($bookNumberToGet) { - if ((int)$bookNumberToGet <= $this->count()) { + if (isset($this->books[$bookNumberToGet])) { return $this->books[$bookNumberToGet]; } diff --git a/Behavioral/Iterator/BookListIterator.php b/Behavioral/Iterator/BookListIterator.php index bc15ffb..aff7e46 100644 --- a/Behavioral/Iterator/BookListIterator.php +++ b/Behavioral/Iterator/BookListIterator.php @@ -61,7 +61,7 @@ class BookListIterator implements \Iterator */ public function valid() { - return $this->currentBook < $this->bookList->count(); + return null !== $this->bookList->getBook($this->currentBook); } /** diff --git a/Behavioral/Iterator/BookListReverseIterator.php b/Behavioral/Iterator/BookListReverseIterator.php index 973bd0b..5ab2be9 100644 --- a/Behavioral/Iterator/BookListReverseIterator.php +++ b/Behavioral/Iterator/BookListReverseIterator.php @@ -11,13 +11,14 @@ class BookListReverseIterator extends BookListIterator $this->currentBook = $this->bookList->count() - 1; } + /** + * (PHP 5 >= 5.0.0)
+ * Move forward to next element + * @link http://php.net/manual/en/iterator.next.php + * @return void Any returned value is ignored. + */ public function next() { $this->currentBook--; } - - public function valid() - { - return 0 <= $this->currentBook; - } }