1 | <?php |
||
17 | class CollectionIterator implements \Iterator, \Countable |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * This is our collection class, defined later in article. |
||
22 | * @var CollectionInterface |
||
23 | */ |
||
24 | protected $collection = null; |
||
25 | |||
26 | /** |
||
27 | * Current index |
||
28 | * @var int |
||
29 | */ |
||
30 | protected $currentIndex = 0; |
||
31 | |||
32 | /** |
||
33 | * Keys in collection |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $keys = null; |
||
37 | |||
38 | /** |
||
39 | * Collection iterator constructor |
||
40 | * |
||
41 | * @param CollectionInterface $collection |
||
42 | */ |
||
43 | 77 | public function __construct(CollectionInterface $collection) |
|
48 | |||
49 | /** |
||
50 | * Gets the collection. |
||
51 | * @return CollectionInterface |
||
52 | */ |
||
53 | 77 | public function getCollection() |
|
57 | |||
58 | /** |
||
59 | * Sets the collection. |
||
60 | * |
||
61 | * @param CollectionInterface $collection |
||
62 | * |
||
63 | * @return CollectionIterator |
||
64 | */ |
||
65 | 77 | public function setCollection(CollectionInterface $collection) |
|
72 | |||
73 | /** |
||
74 | * This method returns current item in collection based on currentIndex. |
||
75 | * @return mixed |
||
76 | */ |
||
77 | 55 | public function current() |
|
81 | |||
82 | /** |
||
83 | * Get current key |
||
84 | * This method returns current items' key in collection based on currentIndex. |
||
85 | */ |
||
86 | 57 | public function key() |
|
90 | |||
91 | /** |
||
92 | * Move to next idex |
||
93 | * This method increases currentIndex by one. |
||
94 | */ |
||
95 | 51 | public function next() |
|
99 | |||
100 | /** |
||
101 | * Rewind |
||
102 | * This method resets currentIndex by setting it to 0 |
||
103 | */ |
||
104 | 48 | public function rewind() |
|
108 | |||
109 | /** |
||
110 | * Check if current index is valid |
||
111 | * This method checks if current index is valid by checking the keys array. |
||
112 | */ |
||
113 | 48 | public function valid() |
|
117 | |||
118 | /** |
||
119 | * Get number of ocurrences. |
||
120 | * @return int |
||
121 | */ |
||
122 | 2 | public function count() |
|
126 | } |
||
127 |