File tree Expand file tree Collapse file tree 8 files changed +553
-353
lines changed Expand file tree Collapse file tree 8 files changed +553
-353
lines changed Original file line number Diff line number Diff line change 4
4
5
5
class Book
6
6
{
7
+ /**
8
+ * @var string
9
+ */
7
10
private $ author ;
8
11
12
+ /**
13
+ * @var string
14
+ */
9
15
private $ title ;
10
16
11
- public function __construct ($ title , $ author )
17
+ public function __construct (string $ title , string $ author )
12
18
{
13
19
$ this ->author = $ author ;
14
20
$ this ->title = $ title ;
15
21
}
16
22
17
- public function getAuthor ()
23
+ public function getAuthor (): string
18
24
{
19
25
return $ this ->author ;
20
26
}
21
27
22
- public function getTitle ()
28
+ public function getTitle (): string
23
29
{
24
30
return $ this ->title ;
25
31
}
26
32
27
- public function getAuthorAndTitle ()
33
+ public function getAuthorAndTitle (): string
28
34
{
29
35
return $ this ->getTitle ().' by ' .$ this ->getAuthor ();
30
36
}
Original file line number Diff line number Diff line change 2
2
3
3
namespace DesignPatterns \Behavioral \Iterator ;
4
4
5
- class BookList implements \Countable
5
+ class BookList implements \Countable, \Iterator
6
6
{
7
+ /**
8
+ * @var Book[]
9
+ */
7
10
private $ books ;
8
11
9
- public function getBook ($ bookNumberToGet )
10
- {
11
- if (isset ($ this ->books [$ bookNumberToGet ])) {
12
- return $ this ->books [$ bookNumberToGet ];
13
- }
14
-
15
- return ;
16
- }
12
+ /**
13
+ * @var int
14
+ */
15
+ private $ currentIndex = 0 ;
17
16
18
17
public function addBook (Book $ book )
19
18
{
@@ -30,8 +29,33 @@ public function removeBook(Book $bookToRemove)
30
29
}
31
30
}
32
31
33
- public function count ()
32
+ public function count (): int
34
33
{
35
34
return count ($ this ->books );
36
35
}
36
+
37
+ public function current (): Book
38
+ {
39
+ return $ this ->books [array_keys ($ this ->books )[$ this ->currentIndex ]];
40
+ }
41
+
42
+ public function key (): int
43
+ {
44
+ return array_keys ($ this ->books )[$ this ->currentIndex ];
45
+ }
46
+
47
+ public function next ()
48
+ {
49
+ $ this ->currentIndex ++;
50
+ }
51
+
52
+ public function rewind ()
53
+ {
54
+ $ this ->currentIndex = 0 ;
55
+ }
56
+
57
+ public function valid (): bool
58
+ {
59
+ return isset (array_keys ($ this ->books )[$ this ->currentIndex ]);
60
+ }
37
61
}
Load Diff This file was deleted.
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 4
4
Purpose
5
5
-------
6
6
7
- To make an object iterable and to make it appear like a collection of
8
- objects.
7
+ To make an object iterable and to make it appear like a collection of objects.
9
8
10
9
Examples
11
10
--------
@@ -45,18 +44,6 @@ BookList.php
45
44
:language: php
46
45
:linenos:
47
46
48
- BookListIterator.php
49
-
50
- .. literalinclude :: BookListIterator.php
51
- :language: php
52
- :linenos:
53
-
54
- BookListReverseIterator.php
55
-
56
- .. literalinclude :: BookListReverseIterator.php
57
- :language: php
58
- :linenos:
59
-
60
47
Test
61
48
----
62
49
@@ -67,4 +54,4 @@ Tests/IteratorTest.php
67
54
:linenos:
68
55
69
56
.. _`GitHub` : https://github.com/domnikl/DesignPatternsPHP/tree/master/Behavioral/Iterator
70
- .. __ : http://en.wikipedia.org/wiki/Iterator_pattern
57
+ .. __ : http://en.wikipedia.org/wiki/Iterator_pattern
You can’t perform that action at this time.
0 commit comments