forked from DesignPatternsPHP/DesignPatternsPHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBookList.php
42 lines (32 loc) · 833 Bytes
/
BookList.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
namespace DesignPatterns\Behavioral\Iterator;
class BookList implements \Countable
{
private $books;
public function getBook($bookNumberToGet)
{
if ((int)$bookNumberToGet <= $this->count()) {
return $this->books[$bookNumberToGet];
}
return null;
}
public function addBook(Book $book)
{
$this->books[] = $book;
return $this->count();
}
public function removeBook(Book $bookToRemove)
{
foreach ($this as $key => $book) {
/** @var Book $book */
if ($book->getAuthorAndTitle() === $bookToRemove->getAuthorAndTitle()) {
unset($this->books[$key]);
}
}
return $this->count();
}
public function count()
{
return count($this->books);
}
}