Skip to content

Commit 57c2e4f

Browse files
author
Dominik Liebler
committed
DesignPatternsPHP#232 simplified things a bit
1 parent 1ff4203 commit 57c2e4f

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

Behavioral/Iterator/BookList.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class BookList implements \Countable, \Iterator
77
/**
88
* @var Book[]
99
*/
10-
private $books;
10+
private $books = [];
1111

1212
/**
1313
* @var int
@@ -22,11 +22,12 @@ public function addBook(Book $book)
2222
public function removeBook(Book $bookToRemove)
2323
{
2424
foreach ($this->books as $key => $book) {
25-
/** @var Book $book */
2625
if ($book->getAuthorAndTitle() === $bookToRemove->getAuthorAndTitle()) {
2726
unset($this->books[$key]);
2827
}
2928
}
29+
30+
$this->books = array_values($this->books);
3031
}
3132

3233
public function count(): int
@@ -36,12 +37,12 @@ public function count(): int
3637

3738
public function current(): Book
3839
{
39-
return $this->books[array_keys($this->books)[$this->currentIndex]];
40+
return $this->books[$this->currentIndex];
4041
}
4142

4243
public function key(): int
4344
{
44-
return array_keys($this->books)[$this->currentIndex];
45+
return $this->currentIndex;
4546
}
4647

4748
public function next()
@@ -56,6 +57,6 @@ public function rewind()
5657

5758
public function valid(): bool
5859
{
59-
return isset(array_keys($this->books)[$this->currentIndex]);
60+
return isset($this->books[$this->currentIndex]);
6061
}
6162
}

0 commit comments

Comments
 (0)