mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-26 09:40:15 +02:00
Merge branch 'iterator-bug' into php7
This commit is contained in:
@@ -7,7 +7,7 @@ class BookList implements \Countable, \Iterator
|
|||||||
/**
|
/**
|
||||||
* @var Book[]
|
* @var Book[]
|
||||||
*/
|
*/
|
||||||
private $books;
|
private $books = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var int
|
* @var int
|
||||||
@@ -22,11 +22,12 @@ class BookList implements \Countable, \Iterator
|
|||||||
public function removeBook(Book $bookToRemove)
|
public function removeBook(Book $bookToRemove)
|
||||||
{
|
{
|
||||||
foreach ($this->books as $key => $book) {
|
foreach ($this->books as $key => $book) {
|
||||||
/** @var Book $book */
|
|
||||||
if ($book->getAuthorAndTitle() === $bookToRemove->getAuthorAndTitle()) {
|
if ($book->getAuthorAndTitle() === $bookToRemove->getAuthorAndTitle()) {
|
||||||
unset($this->books[$key]);
|
unset($this->books[$key]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->books = array_values($this->books);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function count(): int
|
public function count(): int
|
||||||
@@ -36,12 +37,12 @@ class BookList implements \Countable, \Iterator
|
|||||||
|
|
||||||
public function current(): Book
|
public function current(): Book
|
||||||
{
|
{
|
||||||
return $this->books[array_keys($this->books)[$this->currentIndex]];
|
return $this->books[$this->currentIndex];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function key(): int
|
public function key(): int
|
||||||
{
|
{
|
||||||
return array_keys($this->books)[$this->currentIndex];
|
return $this->currentIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function next()
|
public function next()
|
||||||
@@ -56,6 +57,6 @@ class BookList implements \Countable, \Iterator
|
|||||||
|
|
||||||
public function valid(): bool
|
public function valid(): bool
|
||||||
{
|
{
|
||||||
return isset(array_keys($this->books)[$this->currentIndex]);
|
return isset($this->books[$this->currentIndex]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user