Changes to take care of unwanted (negative) indices

This commit is contained in:
Nils Rückmann
2015-09-07 02:17:17 +02:00
parent 367e5c0a20
commit c6f9dccff1
3 changed files with 8 additions and 7 deletions

View File

@@ -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];
}

View File

@@ -61,7 +61,7 @@ class BookListIterator implements \Iterator
*/
public function valid()
{
return $this->currentBook < $this->bookList->count();
return null !== $this->bookList->getBook($this->currentBook);
}
/**

View File

@@ -11,13 +11,14 @@ class BookListReverseIterator extends BookListIterator
$this->currentBook = $this->bookList->count() - 1;
}
/**
* (PHP 5 &gt;= 5.0.0)<br/>
* 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;
}
}