This repository was archived by the owner on Jun 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookList.php
More file actions
113 lines (102 loc) · 2.15 KB
/
BookList.php
File metadata and controls
113 lines (102 loc) · 2.15 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
namespace Kuriv\PHPDesignPatterns\Behavioral\Iterator;
use Countable;
use Iterator;
class BookList implements Countable, Iterator
{
/**
* Store several books.
*
* @var array
*/
private array $books = [];
/**
* Count the total number of books.
*
* @param void
* @return int
*/
public function count(): int
{
return count($this->books);
}
/**
* Return the current element of the specified property.
*
* @param void
* @return Book
*/
public function current(): Book
{
return current($this->books);
}
/**
* Advance the internal pointer of the specified property.
*
* @param void
* @return void
*/
public function next()
{
next($this->books);
}
/**
* Return the current key of the specified property.
*
* @param void
* @return int
*/
public function key(): int
{
return key($this->books);
}
/**
* Return the validity of the current position of the specified property.
*
* @param void
* @return bool
*/
public function valid(): bool
{
return current($this->books) !== false;
}
/**
* Set the internal pointer of the specified property to the first element.
*
* @param void
* @return void
*/
public function rewind()
{
reset($this->books);
}
/**
* Add the book to the book list.
*
* @param Book $book
* @return void
*/
public function addBook(Book $book)
{
foreach ($this->books as $value) {
if ($value->getTitleAndAuthor() == $book->getTitleAndAuthor()) {
return;
}
}
$this->books[] = $book;
}
/**
* Remove the book from the book list.
*
* @param Book $book
* @return void
*/
public function removeBook(Book $book)
{
foreach ($this->books as $key => $value) {
if ($value->getTitleAndAuthor() == $book->getTitleAndAuthor()) {
unset($this->books[$key]);
}
}
}
}