mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-02-24 09:42:24 +01:00
#232 simplified things a bit
This commit is contained in:
parent
1ff4203beb
commit
57c2e4f025
@ -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]);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user