CollectionIterator::getCollection()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the ClassGeneration package.
5
 *
6
 * (c) Antonio Spinelli <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ClassGeneration\Collection;
13
14
/**
15
 * @author Antonio Spinelli <[email protected]>
16
 */
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)
44
    {
45 77
        $this->setCollection($collection);
46 77
        $this->keys = $this->getCollection()->getKeys();
47 77
    }
48
49
    /**
50
     * Gets the collection.
51
     * @return CollectionInterface
52
     */
53 77
    public function getCollection()
54
    {
55 77
        return $this->collection;
56
    }
57
58
    /**
59
     * Sets the collection.
60
     *
61
     * @param CollectionInterface $collection
62
     *
63
     * @return CollectionIterator
64
     */
65 77
    public function setCollection(CollectionInterface $collection)
66
    {
67 77
        $this->currentIndex = 0;
68 77
        $this->collection = $collection;
69
70 77
        return $this;
71
    }
72
73
    /**
74
     * This method returns current item in collection based on currentIndex.
75
     * @return mixed
76
     */
77 55
    public function current()
78
    {
79 55
        return $this->collection->get($this->key());
80
    }
81
82
    /**
83
     * Get current key
84
     * This method returns current items' key in collection based on currentIndex.
85
     */
86 57
    public function key()
87
    {
88 57
        return $this->keys[$this->currentIndex];
89
    }
90
91
    /**
92
     * Move to next idex
93
     * This method increases currentIndex by one.
94
     */
95 51
    public function next()
96
    {
97 51
        ++$this->currentIndex;
98 51
    }
99
100
    /**
101
     * Rewind
102
     * This method resets currentIndex by setting it to 0
103
     */
104 48
    public function rewind()
105
    {
106 48
        $this->currentIndex = 0;
107 48
    }
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()
114
    {
115 48
        return array_key_exists($this->currentIndex, $this->keys);
116
    }
117
118
    /**
119
     * Get number of ocurrences.
120
     * @return int
121
     */
122 2
    public function count()
123
    {
124 2
        return $this->collection->count();
125
    }
126
}
127