Collection::next()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
namespace AppBundle\Sync\Entity;
3
4
/**
5
 * Simple collection
6
 *
7
 * @author Sergey Sadovoi <[email protected]>
8
 */
9
abstract class Collection implements \Iterator, \Countable
10
{
11
    /**
12
     * @var array  Collection Entities
13
     */
14
    protected $items = [];
15
    /**
16
     * @var string  Current index of the collection
17
     */
18
    protected $current;
19
20
    /**
21
     * Init the collection
22
     */
23 11
    public function __construct()
24
    {
25 11
        reset($this->items);
26 11
        $this->current = key($this->items);
27 11
    }
28
29
    /**
30
     * Rewind
31
     */
32 2
    public function rewind()
33
    {
34 2
        reset($this->items);
35 2
        $this->current = key($this->items);
36 2
    }
37
38
    /**
39
     * Get the current element
40
     *
41
     * @return mixed
42
     */
43 2
    public function current()
44
    {
45 2
        return $this->items[$this->current];
46
    }
47
48
    /**
49
     * Get the current key
50
     *
51
     * @return string
52
     */
53 1
    public function key()
54
    {
55 1
        return $this->current;
56
    }
57
58
    /**
59
     * Next
60
     */
61 2
    public function next()
62
    {
63 2
        next($this->items);
64 2
        $this->current = key($this->items);
65 2
    }
66
67
    /**
68
     * Check if current element exists
69
     *
70
     * @return bool
71
     */
72 2
    public function valid()
73
    {
74 2
        return isset($this->items[$this->current]);
75
    }
76
77
    /**
78
     * Get total number of collection elements
79
     *
80
     * @return int
81
     */
82 6
    public function count()
83
    {
84 6
        return count($this->items);
85
    }
86
87
    /**
88
     * Check if element exists
89
     *
90
     * @param string $key  Element key
91
     *
92
     * @return bool
93
     */
94 6
    public function has($key)
95
    {
96 6
        return isset($this->items[$key]);
97
    }
98
99
    /**
100
     * Add an item to the collection
101
     *
102
     * @param mixed  $item  Item
103
     * @param string $key   Item key
104
     */
105 10
    public function add($item, $key = null)
106
    {
107 10
        if (is_null($key)) {
108 3
            $this->items[] = $item;
109 3
        } else {
110 9
            $this->items[$key] = $item;
111
        }
112 10
    }
113
114
    /**
115
     * Delete item from collection
116
     *
117
     * @param string $key  Item key
118
     */
119 2
    public function del($key)
120
    {
121 2
        if ($this->has($key)) {
122 2
            unset($this->items[$key]);
123 2
        }
124 2
    }
125
126
    /**
127
     * Get item from collection
128
     *
129
     * @param string $key  Item key
130
     *
131
     * @return null|mixed  Collection item
132
     */
133 6
    public function get($key)
134
    {
135 6
        return $this->has($key) ? $this->items[$key] : null;
136
    }
137
}
138