ArrayList::isEmpty()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

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 2
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * Copyright (c) 2011-2015, Celestino Diaz <[email protected]>
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
25
namespace Brickoo\Component\Common;
26
27
use ArrayIterator;
28
use Brickoo\Component\Common\Exception\InvalidIndexException;
29
use Brickoo\Component\Common\Assert;
30
use Countable;
31
use InvalidArgumentException;
32
use IteratorAggregate;
33
34
/**
35
 * ArrayList
36
 *
37
 * Implementation of an array based list.
38
 */
39
class ArrayList implements IteratorAggregate, Countable {
40
41
    /** @var array */
42
    private $items;
43
44
    /**
45
     * Class constructor.
46
     * @param array $items
47
     * @throws \InvalidArgumentException
48
     */
49 1
    public function __construct(array $items = []) {
50 1
        $this->items = $items;
51 1
    }
52
53
    /**
54
     * Return the list item by index.
55
     * @param integer $index
56
     * @throws \Brickoo\Component\Common\Exception\InvalidIndexException
57
     * @return string
58
     */
59 2
    public function get($index) {
60 2
        Assert::isInteger($index);
61 2
        if (!$this->has($index)) {
62 1
            throw new InvalidIndexException($index);
63
        }
64 1
        return $this->items[$index];
65
    }
66
67
    /**
68
     * Add an item to the list.
69
     * @param mixed $value
70
     * @return \Brickoo\Component\Common\ArrayList
71
     */
72 1
    public function add($value) {
73 1
        array_push($this->items, $value);
74 1
        return $this;
75
    }
76
77
    /**
78
     * @param integer $index
79
     * @return \Brickoo\Component\Common\ArrayList
80
     * @throws InvalidIndexException
81
     */
82 2
    public function remove($index) {
83 2
        if (!$this->has($index)) {
84 1
            throw new InvalidIndexException($index);
85
        }
86 1
        unset($this->items[$index]);
87 1
        $this->items = array_values($this->items);
88 1
        return $this;
89
    }
90
91
    /**
92
     * Reverse the items order.
93
     * @return \Brickoo\Component\Common\ArrayList
94
     */
95 1
    public function reverse() {
96 1
        if (!$this->isEmpty()) {
97 1
            $this->items = array_reverse($this->items, false);
98 1
        }
99 1
        return $this;
100
    }
101
102
    /**
103
     * Shuffle the items order.
104
     * @return \Brickoo\Component\Common\ArrayList
105
     */
106 1
    public function shuffle() {
107 1
        shuffle($this->items);
108 1
        return $this;
109
    }
110
111
    /**
112
     * Remove duplicate items from list.
113
     * @return \Brickoo\Component\Common\ArrayList
114
     */
115 1
    public function uniquify() {
116 1
        $this->items = array_unique($this->items, SORT_REGULAR);
117 1
        return $this;
118
    }
119
120
    /**
121
     * Check if an item with the index is available.
122
     * @param integer $index
123
     * @return boolean
124
     */
125 1
    public function has($index) {
126 1
        Assert::isInteger($index);
127 1
        return array_key_exists($index, $this->items);
128
    }
129
130
    /**
131
     * Return the index of the first occurrence of the value.
132
     * Return -1 if the value is not in list.
133
     * @param mixed $value
134
     * @return integer
135
     */
136 1
    public function indexOf($value) {
137 1
        $index = array_search($value, $this->items, true);
138 1
        return ($index === false) ? -1 : intval($index);
139
    }
140
141
    /**
142
     * Check if list contains a value.
143
     * @param mixed $value
144
     * @return boolean
145
     */
146 1
    public function contains($value) {
147 1
        return $this->indexOf($value) !== -1;
148
    }
149
150
    /**
151
     * Return the first item in list or null if empty list.
152
     * @throws \Brickoo\Component\Common\Exception\InvalidIndexException
153
     * @return mixed
154
     */
155 1
    public function first() {
156 1
        if ($this->isEmpty()) {
157 1
            return null;
158
        }
159
160 1
        reset($this->items);
161 1
        return current($this->items);
162
    }
163
164
    /**
165
     * Return the last item in list or null if empty list.
166
     * @throws \Brickoo\Component\Common\Exception\InvalidIndexException
167
     * @return mixed
168
     */
169 1
    public function last() {
170 1
        if ($this->isEmpty()) {
171 1
            return null;
172
        }
173
174 1
        $last = end($this->items);
175 1
        reset($this->items);
176 1
        return $last;
177
    }
178
179
    /**
180
     * Check if the list is empty.
181
     * @return boolean
182
     */
183 1
    public function isEmpty() {
184 1
        return empty($this->items);
185
    }
186
187
    /**
188
     * Return the list items as an array.
189
     * @return array
190
     */
191 3
    public function toArray() {
192 3
        return $this->items;
193
    }
194
195
    /**
196
     * Retrieve an external iterator containing all items.
197
     * @return \ArrayIterator
198
     */
199 1
    public function getIterator() {
200 1
        return new ArrayIterator($this->items);
201
    }
202
203
    /**
204
     * Count number of list items.
205
     * @return integer
206
     */
207 1
    public function count() {
208 1
        return count($this->items);
209
    }
210
211
    /**
212
     * Return a string representation of the list.
213
     * The values are linefeed separated.
214
     * @return string
215
     */
216 1
    public function toString() {
217 1
        return implode("\n", array_map(
218 1
            function($value) {
219 1
                if (!is_scalar($value)) {
220 1
                    return serialize($value);
221
                }
222 1
                return is_bool($value) ? ($value ? "true" : "false") : (string)$value;
223 1
            },
224 1
            $this->items
225 1
        ));
226
    }
227
228
}
229