Passed
Branch validation-refactor (de59e2)
by Jason
03:09
created

AbstractCollection::current()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * Part of the Fusion.Collection package.
5
 *
6
 * @license MIT
7
 */
8
9
declare(strict_types=1);
10
11
namespace Fusion\Collection\Contracts;
12
13
use ArrayAccess;
14
use Iterator;
15
16
/**
17
 * Implements core functionality for collections.
18
 *
19
 * @since 1.0.0
20
 */
21
abstract class AbstractCollection implements CollectionCoreInterface, ArrayAccess, Iterator
22
{
23
    /**
24
     * The internal collection container.
25
     *
26
     * @var array
27
     */
28
    protected $collection = [];
29
30
    /** @var \Fusion\Collection\Contracts\CollectionValidationInterface */
31
    protected $validator;
32
33
    /** {@inheritdoc} */
34 1
    public function clear(): void
35
    {
36 1
        $this->collection = [];
37 1
    }
38
39
    /** {@inheritdoc} */
40 3
    public function remove($item): void
41
    {
42 3
        foreach ($this->collection as $key => $value)
43
        {
44 3
            if ($item === $value)
45
            {
46 3
                $this->removeAt($key);
47
            }
48
        }
49 3
    }
50
51
    /** {@inheritdoc} */
52 6
    public function removeAt($key): void
53
    {
54 6
        if ($this->offsetExists($key))
55
        {
56 6
            if (is_int($key))
57
            {
58 3
                array_splice($this->collection, $key, 1);
59
            }
60 3
            else if (is_string($key))
61
            {
62 3
                unset($this->collection[$key]);
63
            }
64
        }
65 6
    }
66
67
    /**
68
     * Checks if an offset exists in the collection.
69
     *
70
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
71
     *
72
     * @param mixed $offset
73
     *
74
     * @return bool
75
     */
76 38
    public function offsetExists($offset)
77
    {
78 38
        return array_key_exists($offset, $this->collection);
79
    }
80
81
    /**
82
     * Retrieves a value at the given offset.
83
     *
84
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
85
     *
86
     * @param mixed $offset
87
     *
88
     * @return mixed
89
     */
90 16
    public function offsetGet($offset)
91
    {
92 16
        return $this->collection[$offset];
93
    }
94
95
    /**
96
     * Sets a value at the given offset.
97
     *
98
     * This method will throw a `CollectionException` if the offset does not exist.
99
     *
100
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
101
     *
102
     * @param mixed $offset
103
     * @param mixed $value
104
     *
105
     * @return void
106
     *
107
     * @throws \Fusion\Collection\Exceptions\CollectionException
108
     */
109 26
    public function offsetSet($offset, $value): void
110
    {
111 26
        $this->validator->validateNonNullValue($value);
112 23
        $this->collection[$offset] = $value;
113 23
    }
114
115
    /**
116
     * Removes a value at the given offset.
117
     *
118
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
119
     *
120
     * @param mixed $offset
121
     *
122
     * @return void
123
     */
124 3
    public function offsetUnset($offset): void
125
    {
126 3
        if ($this->offsetExists($offset))
127
        {
128 3
            $this->removeAt($offset);
129
        }
130 3
    }
131
132
    /**
133
     * Returns the current element in the collection.
134
     *
135
     * @link http://php.net/manual/en/iterator.current.php
136
     *
137
     * @return mixed
138
     */
139 4
    public function current()
140
    {
141 4
        return current($this->collection);
142
    }
143
144
    /**
145
     * Move forward to the next element in the collection.
146
     *
147
     * @link http://php.net/manual/en/iterator.next.php
148
     *
149
     * @return void
150
     */
151 4
    public function next(): void
152
    {
153 4
        next($this->collection);
154 4
    }
155
156
    /**
157
     * Return the key of the current element in the collection.
158
     *
159
     * @link http://php.net/manual/en/iterator.key.php
160
     *
161
     * @return mixed
162
     */
163 2
    public function key()
164
    {
165 2
        return key($this->collection);
166
    }
167
168
    /**
169
     * Checks if the current element position is valid.
170
     *
171
     * @link http://php.net/manual/en/iterator.valid.php
172
     *
173
     * @return bool
174
     */
175 3
    public function valid(): bool
176
    {
177 3
        return key($this->collection) !== null;
178
    }
179
180
    /**
181
     * Rewind the collection's position to the first index.
182
     *
183
     * @link http://php.net/manual/en/iterator.rewind.php
184
     *
185
     * @return void
186
     */
187 2
    public function rewind(): void
188
    {
189 2
        reset($this->collection);
190 2
    }
191
192
    /**
193
     * Returns of count of the elements in a collection.
194
     *
195
     * @link http://php.net/manual/en/countable.count.php
196
     *
197
     * @return int
198
     */
199 24
    public function count(): int
200
    {
201 24
        return count($this->collection);
202
    }
203
}