ArrayCollection::slice()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[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 League\CommonMark\Util;
13
14
/**
15
 * Array collection
16
 *
17
 * Provides a wrapper around a standard PHP array.
18
 *
19
 * @internal
20
 */
21
class ArrayCollection implements \IteratorAggregate, \Countable, \ArrayAccess
22
{
23
    /**
24
     * @var array
25
     */
26
    private $elements;
27
28
    /**
29
     * Constructor
30
     *
31
     * @param array $elements
32
     */
33 2199
    public function __construct(array $elements = [])
34
    {
35 2199
        $this->elements = $elements;
36 2199
    }
37
38
    /**
39
     * @return mixed
40
     */
41 117
    public function first()
42
    {
43 117
        return \reset($this->elements);
44
    }
45
46
    /**
47
     * @return mixed
48
     */
49 3
    public function last()
50
    {
51 3
        return \end($this->elements);
52
    }
53
54
    /**
55
     * Retrieve an external iterator
56
     *
57
     * @return \ArrayIterator
58
     */
59 3
    public function getIterator()
60
    {
61 3
        return new \ArrayIterator($this->elements);
62
    }
63
64
    /**
65
     * @param mixed $element
66
     *
67
     * @return bool
68
     */
69 2082
    public function add($element): bool
70
    {
71 2082
        $this->elements[] = $element;
72
73 2082
        return true;
74
    }
75
76
    /**
77
     * @param mixed $key
78
     * @param mixed $value
79
     */
80 6
    public function set($key, $value)
81
    {
82 6
        $this->elements[$key] = $value;
83 6
    }
84
85
    /**
86
     * @param mixed $key
87
     *
88
     * @return mixed
89
     */
90 6
    public function get($key)
91
    {
92 6
        return isset($this->elements[$key]) ? $this->elements[$key] : null;
93
    }
94
95
    /**
96
     * @param mixed $key
97
     *
98
     * @return mixed|null
99
     */
100 12
    public function remove($key)
101
    {
102 12
        if (!\array_key_exists($key, $this->elements)) {
103 9
            return;
104
        }
105
106 12
        $removed = $this->elements[$key];
107 12
        unset($this->elements[$key]);
108
109 12
        return $removed;
110
    }
111
112
    /**
113
     * @return bool
114
     */
115 3
    public function isEmpty(): bool
116
    {
117 3
        return empty($this->elements);
118
    }
119
120
    /**
121
     * @param mixed $element
122
     *
123
     * @return bool
124
     */
125 3
    public function contains($element): bool
126
    {
127 3
        return \in_array($element, $this->elements, true);
128
    }
129
130
    /**
131
     * @param mixed $element
132
     *
133
     * @return mixed|false
134
     */
135 3
    public function indexOf($element)
136
    {
137 3
        return \array_search($element, $this->elements, true);
138
    }
139
140
    /**
141
     * @param mixed $key
142
     *
143
     * @return bool
144
     */
145 6
    public function containsKey($key): bool
146
    {
147 6
        return \array_key_exists($key, $this->elements);
148
    }
149
150
    /**
151
     * Count elements of an object
152
     *
153
     * @return int The count as an integer.
154
     */
155 117
    public function count(): int
156
    {
157 117
        return \count($this->elements);
158
    }
159
160
    /**
161
     * Whether an offset exists
162
     *
163
     * @param mixed $offset An offset to check for.
164
     *
165
     * @return bool true on success or false on failure.
166
     */
167 3
    public function offsetExists($offset): bool
168
    {
169 3
        return $this->containsKey($offset);
170
    }
171
172
    /**
173
     * Offset to retrieve
174
     *
175
     * @param mixed $offset The offset to retrieve.
176
     *
177
     * @return mixed
178
     */
179 3
    public function offsetGet($offset)
180
    {
181 3
        return $this->get($offset);
182
    }
183
184
    /**
185
     * Offset to set
186
     *
187
     * @param mixed $offset The offset to assign the value to.
188
     * @param mixed $value  The value to set.
189
     *
190
     * @return void
191
     */
192 3
    public function offsetSet($offset, $value)
193
    {
194 3
        if ($offset === null) {
195 3
            $this->add($value);
196
        } else {
197 3
            $this->set($offset, $value);
198
        }
199 3
    }
200
201
    /**
202
     * Offset to unset
203
     *
204
     * @param mixed $offset The offset to unset.
205
     *
206
     * @return void
207
     */
208 3
    public function offsetUnset($offset)
209
    {
210 3
        $this->remove($offset);
211 3
    }
212
213
    /**
214
     * Returns a subset of the array
215
     *
216
     * @param int      $offset
217
     * @param int|null $length
218
     *
219
     * @return array
220
     */
221 105
    public function slice(int $offset, ?int $length = null): array
222
    {
223 105
        return \array_slice($this->elements, $offset, $length, true);
224
    }
225
226
    /**
227
     * @return array
228
     */
229 1986
    public function toArray(): array
230
    {
231 1986
        return $this->elements;
232
    }
233
234
    /**
235
     * @param array $elements
236
     *
237
     * @return $this
238
     */
239 3
    public function replaceWith(array $elements)
240
    {
241 3
        $this->elements = $elements;
242
243 3
        return $this;
244
    }
245
246 3
    public function removeGaps()
247
    {
248 3
        $this->elements = \array_filter($this->elements);
249 3
    }
250
}
251