Completed
Push — master ( a265e3...58d4d9 )
by Bobby
02:21
created

Collection::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Ballen\Collection;
4
5
use ArrayIterator;
6
7
/**
8
 * Collection
9
 * 
10
 * A Collection class (library) which provides OOP replacement for the
11
 * traditional array data structure. 
12
 *
13
 * @author Bobby Allen <[email protected]>
14
 * @version 1.0.0
15
 * @license http://opensource.org/licenses/GPL-2.0
16
 * @link https://github.com/bobsta63/collection
17
 * @link http://www.bobbyallen.me
18
 *
19
 */
20
class Collection
21
{
22
23
    /**
24
     * The collection data.
25
     * @var array
26
     */
27
    private $items = [];
28
29
    /**
30
     * Create new instance of a Collection.
31
     * @param array $items
32
     */
33 26
    public function __construct($items = null)
34
    {
35 26
        if (!is_null($items) && is_array($items)) {
36 25
            $this->push($items);
37 25
        }
38 26
    }
39
40
    /**
41
     * Resets the collection with the specified array content.
42
     * @param array $items
43
     * @return Collection
44
     */
45 2
    public function reset($items = null)
46
    {
47 2
        if ((func_get_args() > 0) && is_array($items)) {
48 1
            $this->items = $items;
49 1
        } else {
50 1
            $this->items = [];
51
        }
52 2
        return $this;
53
    }
54
55
    /**
56
     * Set an item or items into the the collection.
57
     * @param mixed $key
58
     * @param mixed $item
59
     * @return Collection
60
     */
61 1
    public function put($key, $item)
62
    {
63 1
        $this->items[$key] = $item;
64 1
        return $this;
65
    }
66
67
    /**
68
     * Push a new item (or array of items) into the collection onto the end
69
     * of the collection.
70
     * @param mixed $item
71
     * @return Collection
72
     */
73 25
    public function push($item)
74
    {
75 25
        if (!is_array($item)) {
76 3
            $this->items = array_merge($this->items, [$item]);
77 3
        } else {
78 25
            $this->items = array_merge($this->items, $item);
79
        }
80 25
        return $this;
81
    }
82
83
    /**
84
     * Push an item (or array of items) onto the beginning of the collection.
85
     * @param  mixed  $item
86
     * @return Collection
87
     */
88 1
    public function prepend($item)
89
    {
90 1
        array_unshift($this->items, $item);
91 1
        return $this;
92
    }
93
94
    /**
95
     * Get and remove the first item from the collection.
96
     * @return mixed
97
     */
98 1
    public function shift()
99
    {
100 1
        return array_shift($this->items);
101
    }
102
103
    /**
104
     * Pops an item off the end of the collection.
105
     * @return Collection
106
     */
107 1
    public function pop()
108
    {
109 1
        array_pop($this->items);
110 1
        return $this;
111
    }
112
113
    /**
114
     * Get all items from the collection.
115
     * @return CollectionExport
116
     */
117 7
    public function all()
118
    {
119 7
        return new CollectionExport($this->items);
120
    }
121
122
    /**
123
     * Checks if the collection has a the specified key set.
124
     * @param mixed $key The key name to check if it exists.
125
     * @return boolean
126
     */
127 4
    public function has($key)
128
    {
129 4
        return isset($this->items[$key]);
130
    }
131
132
    /**
133
     * Get a specific item from the collection.
134
     * @param string $key The collection (array) key to return.
135
     * @param string $default Optional default value if the key doesn't exist (defaulted to false)
136
     * @return string
137
     */
138 2
    public function get($key, $default = false)
139
    {
140 2
        if (!isset($this->items[$key])) {
141 1
            return $default;
0 ignored issues
show
Bug Compatibility introduced by
The expression return $default; of type false|string is incompatible with the return type documented by Ballen\Collection\Collection::get of type string as it can also be of type false which is not included in this return type.
Loading history...
142
        }
143 1
        return $this->items[$key];
144
    }
145
146
    /**
147
     * The total number of items in the collection.
148
     * @return int
149
     */
150 6
    public function count()
151
    {
152 6
        return count($this->items);
153
    }
154
155
    /**
156
     * Iterate over each of the items in the collection and execute the callback.
157
     * @param callable $callback
158
     * @return Collection
159
     */
160 2
    public function each(callable $callback)
161
    {
162 2
        foreach ($this->items as $key => $item) {
163 2
            if ($callback($key, $item) === false) {
164 1
                break;
165
            }
166 2
        }
167 2
        return $this;
168
    }
169
170
    /**
171
     * Return the first item in the collection.
172
     * @return  mixed
173
     */
174 1
    public function first()
175
    {
176 1
        return $this->items[0];
177
    }
178
179
    /**
180
     * Returns the last item in the collection.
181
     * @return mixed
182
     */
183 1
    public function last()
184
    {
185 1
        return array_reverse($this->items)[0];
186
    }
187
188
    /**
189
     * Shuffles (randomises) the items in the collection.
190
     * @return Collection
191
     */
192 1
    public function shuffle()
193
    {
194 1
        shuffle($this->items);
195 1
        return $this;
196
    }
197
198
    /**
199
     * Converts the colletion into a string.
200
     * @return string
201
     */
202 1
    public function implode($glue = ' ')
203
    {
204 1
        return implode($glue, $this->items);
205
    }
206
207
    /**
208
     * Checks to see if the collection is empty.
209
     * @return boolean
210
     */
211 2
    public function isEmpty()
212
    {
213 2
        return empty($this->items);
214
    }
215
216
    /**
217
     * Get an iterator for the collection.
218
     * @return \ArrayIterator
219
     */
220 1
    public function getIterator()
221
    {
222 1
        return new ArrayIterator($this->items);
223
    }
224
225
    /**
226
     * Converts the collection to it's string representation (JSON)
227
     * @return string
228
     */
229 1
    public function __toString()
230
    {
231 1
        return $this->toJson();
232
    }
233
234
    /**
235
     * Converts the collection to JSON.
236
     * @return string
237
     */
238 1
    public function toJson()
239
    {
240 1
        return json_encode($this->items);
241
    }
242
}
243