mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-26 17:50:11 +02:00
Changes to take care of unwanted (negative) indices
This commit is contained in:
@@ -9,7 +9,7 @@ class BookList implements \Countable
|
|||||||
|
|
||||||
public function getBook($bookNumberToGet)
|
public function getBook($bookNumberToGet)
|
||||||
{
|
{
|
||||||
if ((int)$bookNumberToGet <= $this->count()) {
|
if (isset($this->books[$bookNumberToGet])) {
|
||||||
return $this->books[$bookNumberToGet];
|
return $this->books[$bookNumberToGet];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -61,7 +61,7 @@ class BookListIterator implements \Iterator
|
|||||||
*/
|
*/
|
||||||
public function valid()
|
public function valid()
|
||||||
{
|
{
|
||||||
return $this->currentBook < $this->bookList->count();
|
return null !== $this->bookList->getBook($this->currentBook);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -11,13 +11,14 @@ class BookListReverseIterator extends BookListIterator
|
|||||||
$this->currentBook = $this->bookList->count() - 1;
|
$this->currentBook = $this->bookList->count() - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (PHP 5 >= 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()
|
public function next()
|
||||||
{
|
{
|
||||||
$this->currentBook--;
|
$this->currentBook--;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function valid()
|
|
||||||
{
|
|
||||||
return 0 <= $this->currentBook;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user