Completed
Push — master ( 7ae06a...d412b0 )
by Bobby
03:13
created

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

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
269 1
    }
270
}
271