-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIterator.php
61 lines (49 loc) · 859 Bytes
/
Iterator.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
use Countable, Iterator;
class Book
{
private string $name;
private string $author;
function __construct($name, $author)
{
$this->name = $name;
$this->author = $author;
}
function getName()
{
return $this->name;
}
}
class BookList implements Countable, Iterator
{
private array $books = [];
private $current = 0;
public function addBook(Book $book)
{
$this->books[] = $book;
}
public function count(): int
{
return count($this->books);
}
public function current(): Book
{
return $this->books[$this->current];
}
public function key(): int
{
return $this->current;
}
public function next()
{
$this->current++;
}
public function rewind()
{
$this->current = 0;
}
public function valid(): bool
{
return isset($this->books[$this->current]);
}
}