File tree Expand file tree Collapse file tree 1 file changed +6
-5
lines changed Expand file tree Collapse file tree 1 file changed +6
-5
lines changed Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ class BookList implements \Countable, \Iterator
7
7
/**
8
8
* @var Book[]
9
9
*/
10
- private $ books ;
10
+ private $ books = [] ;
11
11
12
12
/**
13
13
* @var int
@@ -22,11 +22,12 @@ public function addBook(Book $book)
22
22
public function removeBook (Book $ bookToRemove )
23
23
{
24
24
foreach ($ this ->books as $ key => $ book ) {
25
- /** @var Book $book */
26
25
if ($ book ->getAuthorAndTitle () === $ bookToRemove ->getAuthorAndTitle ()) {
27
26
unset($ this ->books [$ key ]);
28
27
}
29
28
}
29
+
30
+ $ this ->books = array_values ($ this ->books );
30
31
}
31
32
32
33
public function count (): int
@@ -36,12 +37,12 @@ public function count(): int
36
37
37
38
public function current (): Book
38
39
{
39
- return $ this ->books [array_keys ( $ this ->books )[ $ this -> currentIndex ] ];
40
+ return $ this ->books [$ this ->currentIndex ];
40
41
}
41
42
42
43
public function key (): int
43
44
{
44
- return array_keys ( $ this ->books )[ $ this -> currentIndex ] ;
45
+ return $ this ->currentIndex ;
45
46
}
46
47
47
48
public function next ()
@@ -56,6 +57,6 @@ public function rewind()
56
57
57
58
public function valid (): bool
58
59
{
59
- return isset (array_keys ( $ this ->books ) [$ this ->currentIndex ]);
60
+ return isset ($ this ->books [$ this ->currentIndex ]);
60
61
}
61
62
}
You can’t perform that action at this time.
0 commit comments