IteratorCollection::getValues()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * KNUT7 K7F (https://marciozebedeu.com/)
5
 * KNUT7 K7F (tm) : Rapid Development Framework (https://marciozebedeu.com/)
6
 *
7
 * Licensed under The MIT License
8
 * For full copyright and license information, please see the LICENSE.txt
9
 * Redistributions of files must retain the above copyright notice.
10
 *
11
 * @link      https://github.com/knut7/framework/ for the canonical source repository
12
 * @copyright (c) 2015.  KNUT7  Software Technologies AO Inc. (https://marciozebedeu.com/)
13
 * @license   https://marciozebedeu.com/license/new-bsd New BSD License
14
 * @author    Marcio Zebedeu - [email protected]
15
 * @version   1.0.2
16
 */
17
18
namespace Ballybran\Core\Collections\Collection;
19
20
21
use Ballybran\Core\Variables\Variable;
22
use Closure;
23
24
/**
25
 * Class IteratorCollection
26
 * @package Ballybran\Core\Collections\Collection
27
 */
28
class IteratorCollection extends Variable implements \ArrayAccess
29
{
30
31
    /**
32
     * @var array
33
     */
34
    private $elements;
35
    /**
36
     * @var int
37
     */
38
39
    //put your code here
40
41
    /**
42
     * IteratorCollection constructor.
43
     * @param array $elements
44
     */
45
    public function __construct(array $elements = array())
46
    {
47
48
        parent::__construct($elements);
49
50
        $this->elements = $elements;
51
    }
52
53
    protected function setElementsFromTrustedSource(array $elements)
54
    {
55
        $this->elements = $elements;
56
    }
57
58
59
    /**
60
     * @return array
61
     */
62
    public function toArray(): array
63
    {
64
        return $this->elements;
65
    }
66
67
    /**
68
     * @return \ArrayObject
69
     */
70
    public function getIterator()
71
    {
72
73
        return new \ArrayObject($this->elements);
74
    }
75
76
    public function count()
77
    {
78
        return count($this->elements);
79
    }
80
81
    public function current()
82
    {
83
        return current($this->elements);
84
    }
85
86
    /**
87
     * @param $element
88
     * Checks if a value exists in an array
89
     * @see in_array
90
     * @return bool
91
     */
92
    public function contains($element)
93
    {
94
        return in_array($element, $this->elements, true);
95
    }
96
97
    /**
98
     * @return mixed
99
     */
100
    public function next()
101
    {
102
        return next($this->elements);
103
    }
104
105
    public function last()
106
    {
107
        return end($this->elements);
108
    }
109
110
    public function first()
111
    {
112
        return reset($this->elements);
113
    }
114
115
    public function key()
116
    {
117
        return key($this->elements);
118
    }
119
120
    public function valid()
121
    {
122
        return $this->offsetExists($this->elements);
123
124
    }
125
126
    public function offsetGet($offset)
127
    {
128
        return $this->get($offset);
129
    }
130
131
    public function offsetSet($offset, $value)
132
    {
133
        if (!isset($offset)) {
134
            $this->set($offset, $value);
135
        }
136
            $this->set($offset, $value);
137
    }
138
139
    public function offsetUnset($offset)
140
    {
141
            $this->remove($offset);
142
    }
143
144
    public function containsKey($key)
145
    {
146
        return isset($this->elements[$key]) || array_key_exists($key, $this->elements);
147
    }
148
149
    public function offsetExists($offset)
150
    {
151
        return $this->containsKey($offset);
152
    }
153
154
    public function remove($key)
155
    {
156
        if (!isset($this->elements[$key]) && !array_key_exists($key, $this->elements)) {
157
            return false;
158
        } else {
159
            $removed = $this->elements[$key];
160
            unset($this->elements[$key]);
161
162
            return $removed;
163
        }
164
    }
165
166
    public function removeElement($element)
167
    {
168
        $key = array_search($element, $this->elements, true);
169
        if (false === $key) {
170
            return false;
171
        }
172
        unset($this->elements[$key]);
173
        return true;
174
    }
175
176
    public function add($value)
177
    {
178
        $this->elements[] = $value;
179
180
        return $this;
181
    }
182
183
    public function set($key, $value)
184
    {
185
        $this->elements[$key] = $value;
186
187
        return true;
188
    }
189
190
    public function ksort()
191
    {
192
193
        return ksort($this->elements);
194
    }
195
196
    public function natSort()
197
    {
198
        return natsort($this->elements);
199
    }
200
201
    public function natcasesort()
202
    {
203
        return natcasesort($this->elements);
204
    }
205
206
    public function exists(Closure $p)
207
    {
208
        foreach ($this->elements as $key => $element) {
209
            if ($p($key, $element)) {
210
                return true;
211
            }
212
        }
213
        return false;
214
    }
215
216
    /**
217
     * @return int|string
218
     * return the position of the  element
219
     */
220
    public function indexOf($element)
221
    {
222
        return array_search($element, $this->elements);
223
    }
224
225
    public function isEmpty()
226
    {
227
        return empty($this->elements);
228
    }
229
230
    public function getValues()
231
    {
232
233
        return array_values($this->elements);
234
    }
235
236
    public function getKeys()
237
    {
238
        return array_keys($this->elements);
239
    }
240
241
    public function get($key)
242
    {
243
        return isset($this->elements[$key]) ? $this->elements[$key] : null;
244
    }
245
246
247
    public function slice($start, $end)
248
    {
249
        if ($start < 0 || !is_int($start)) {
250
            throw new \InvalidArgumentException("Start must be a no-negative integer");
251
        }
252
253
        if ($end < 0 || !is_int($end)) {
254
            throw new \InvalidArgumentException("End must be a positive integer");
255
        }
256
257
        if ($start > $end) {
258
            throw new \InvalidArgumentException("End must be geater than start");
259
        }
260
261
        if ($end > $this->count() + 1) {
262
            throw new \InvalidArgumentException("End must be less than the count of the items in the Collection");
263
        }
264
265
        $length = $end - $start + 1;
266
267
        $subsetItems = array_slice($this->elements, $start, $length);
268
269
            if (null === $subsetItems) {
270
                return null;
271
            }
272
        return $this->setElementsFromTrustedSource($subsetItems);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->setElementsFromTrustedSource($subsetItems) targeting Ballybran\Core\Collectio...entsFromTrustedSource() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
273
274
    }
275
276
    public function reverse()
277
    {
278
        $item = array_reverse($this->elements);
279
            if (null === $item) {
280
                    return null;
281
            }
282
283
        return $this->setElementsFromTrustedSource($item);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->setElementsFromTrustedSource($item) targeting Ballybran\Core\Collectio...entsFromTrustedSource() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
284
285
    }
286
287
    public function find($value)
288
    {
289
        $this->get($value);
290
    }
291
292
293
}
294