Completed
Push — master ( 5f180f...861647 )
by arto
02:09
created

Collection::calculateNumberOfElements()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since 2015-11-26
5
 */
6
namespace Net\Bazzline\Component\Cli\Arguments;
7
8
use ArrayAccess;
9
use InvalidArgumentException;
10
use Iterator;
11
12
class Collection implements ArrayAccess, Iterator
13
{
14
    /** @var array */
15
    private $elements;
16
17
    /** @var int */
18
    private $numberOfElements;
19
20
    /**
21
     * Collection constructor.
22
     * @param array $elements
23
     */
24
    public function __construct(array $elements = array())
25
    {
26
        $this->resetAll($elements);
27
    }
28
29
    //begin of ArrayAccess interface implementation
30
    /**
31
     * Whether a offset exists
32
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
33
     * @param mixed $offset <p>
34
     * An offset to check for.
35
     * </p>
36
     * @return boolean true on success or false on failure.
37
     * </p>
38
     * <p>
39
     * The return value will be casted to boolean if non-boolean was returned.
40
     * @since 5.0.0
41
     */
42
    public function offsetExists($offset)
43
    {
44
        return (isset($this->elements[$offset]));
45
    }
46
47
    /**
48
     * Offset to retrieve
49
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
50
     * @param mixed $offset <p>
51
     * The offset to retrieve.
52
     * </p>
53
     * @return mixed Can return all value types.
54
     * @since 5.0.0
55
     */
56
    public function offsetGet($offset)
57
    {
58
        return (
59
            $this->offsetExists($offset)
60
                ? $this->elements[$offset]
61
                : null
62
        );
63
    }
64
65
    /**
66
     * Offset to set
67
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
68
     * @param mixed $offset <p>
69
     * The offset to assign the value to.
70
     * </p>
71
     * @param mixed $value <p>
72
     * The value to set.
73
     * </p>
74
     * @return void
75
     * @since 5.0.0
76
     */
77
    public function offsetSet($offset, $value)
78
    {
79
        if (is_null($offset)) {
80
            $this->elements[] = $value;
81
        } else {
82
            $this->elements[$offset] = $value;
83
        }
84
        $this->reset();
85
    }
86
87
    /**
88
     * Offset to unset
89
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
90
     * @param mixed $offset <p>
91
     * The offset to unset.
92
     * </p>
93
     * @return void
94
     * @since 5.0.0
95
     */
96
    public function offsetUnset($offset)
97
    {
98
        if ($this->offsetExists($offset)) {
99
            unset($this->elements[$offset]);
100
        }
101
        $this->reset();
102
    }
103
    //end of ArrayAccess interface implementation
104
105
    //begin of Iterator interface implementation
106
    /**
107
     * Return the current element
108
     * @link http://php.net/manual/en/iterator.current.php
109
     * @return mixed Can return any type.
110
     * @since 5.0.0
111
     */
112
    public function current()
113
    {
114
        return current($this->elements);
115
    }
116
117
    /**
118
     * Move forward to next element
119
     * @link http://php.net/manual/en/iterator.next.php
120
     * @return void Any returned value is ignored.
121
     * @since 5.0.0
122
     */
123
    public function next()
124
    {
125
        next($this->elements);
126
    }
127
128
    /**
129
     * Return the key of the current element
130
     * @link http://php.net/manual/en/iterator.key.php
131
     * @return mixed scalar on success, or null on failure.
132
     * @since 5.0.0
133
     */
134
    public function key()
135
    {
136
        return key($this->elements);
137
    }
138
139
    /**
140
     * Checks if current position is valid
141
     * @link http://php.net/manual/en/iterator.valid.php
142
     * @return boolean The return value will be casted to boolean and then evaluated.
143
     * Returns true on success or false on failure.
144
     * @since 5.0.0
145
     */
146
    public function valid()
147
    {
148
        return (isset($this->elements[$this->key()]));
149
    }
150
151
    /**
152
     * Rewind the Iterator to the first element
153
     * @link http://php.net/manual/en/iterator.rewind.php
154
     * @return void Any returned value is ignored.
155
     * @since 5.0.0
156
     */
157
    public function rewind()
158
    {
159
        reset($this->elements);
160
    }
161
    //end of Iterator interface implementation
162
163
    /**
164
     * @param array $elements
165
     * @param bool $referenceKeysOfProvidedElements
166
     * @throws InvalidArgumentException
167
     */
168
    public function addMany(array $elements, $referenceKeysOfProvidedElements = false)
169
    {
170
        if ($referenceKeysOfProvidedElements) {
171
            foreach ($elements as $key => $element) {
172
                $this->addOne($element, $key);
173
            }
174
        } else {
175
            foreach ($elements as $element) {
176
                $this->addOne($element);
177
            }
178
        }
179
    }
180
181
    /**
182
     * @param mixed $element
183
     * @param mixed|null $key
184
     * @throws InvalidArgumentException
185
     */
186
    public function addOne($element, $key = null)
187
    {
188
        if ($this->elementIsValid($element)) {
189
            $this->offsetSet($key, $element);
190
        } else {
191
            throw new InvalidArgumentException(
192
                'provided element is invalid'
193
            );
194
        }
195
    }
196
197
    /**
198
     * @param mixed $key
199
     * @return bool
200
     */
201
    public function containsKey($key)
202
    {
203
        return ($this->offsetExists($key));
204
    }
205
206
    /**
207
     * @param mixed $value
208
     * @return bool
209
     */
210
    public function containsValue($value)
211
    {
212
        return (in_array($value, $this->elements));
213
    }
214
215
    /**
216
     * @return int
217
     */
218
    public function getNumberOfElements()
219
    {
220
        return $this->numberOfElements;
221
    }
222
223
    /**
224
     * @param null|mixed $key
225
     * @return mixed
226
     */
227
    public function getOne($key = null)
228
    {
229
        return (
230
            is_null($key)
231
                ? $this->current()
232
                : $this->offsetGet($key)
233
        );
234
    }
235
236
    /**
237
     * @return bool
238
     */
239
    public function isEmpty()
240
    {
241
        return ($this->numberOfElements === 0);
242
    }
243
244
    /**
245
     * @return array
246
     */
247
    public function convertToArray()
248
    {
249
        $array = array();
250
251
        foreach ($this->elements as $key => $value) {
252
            if ($value instanceof Collection) {
253
                $value = $value->convertToArray();
254
            }
255
256
            $array[$key] = $value;
257
        }
258
259
        return $array;
260
    }
261
262
    /**
263
     * @param string $separator
264
     * @return string
265
     */
266
    public function convertToString($separator = ' ')
267
    {
268
        return (implode($separator, $this->elements));
269
    }
270
271
    /**
272
     * @param array $elements
273
     * @throws InvalidArgumentException
274
     */
275
    public function resetAll(array $elements)
276
    {
277
        $this->elements = array();
278
        $this->rewind();
279
280
        if (empty($elements)) {
281
            $this->calculateNumberOfElements();
282
        } else {
283
            $this->addMany($elements);
284
        }
285
    }
286
287
    /**
288
     * @return string
289
     */
290
    public function __toString()
291
    {
292
        return $this->convertToString();
293
    }
294
295
    /**
296
     * overwrite this if you want to, default is always returning a true
297
     * @param mixed $element
298
     * @return bool
299
     */
300
    protected function elementIsValid($element)
0 ignored issues
show
Unused Code introduced by
The parameter $element is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
301
    {
302
        return true;
303
    }
304
305
    private function calculateNumberOfElements()
306
    {
307
        $this->numberOfElements = count($this->elements);
308
    }
309
310
    private function reset()
311
    {
312
        $this->calculateNumberOfElements();
313
        $this->rewind();
314
    }
315
}