Passed
Pull Request — master (#1252)
by Keal
02:21
created

Collection::except()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
ccs 0
cts 4
cp 0
crap 6
1
<?php
2
3
/*
4
 * This file is part of the overtrue/wechat.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace EasyWeChat\Kernel\Support;
13
14
use ArrayAccess;
15
use ArrayIterator;
16
use Countable;
17
use EasyWeChat\Kernel\Contracts\Arrayable;
18
use IteratorAggregate;
19
use JsonSerializable;
20
use Serializable;
21
22
/**
23
 * Class Collection.
24
 */
25
class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Serializable, Arrayable
26
{
27
    /**
28
     * The collection data.
29
     *
30
     * @var array
31
     */
32
    protected $items = [];
33
34
    /**
35
     * set data.
36
     *
37
     * @param mixed $items
38
     */
39
    public function __construct(array $items = [])
40
    {
41
        foreach ($items as $key => $value) {
42
            $this->set($key, $value);
43
        }
44
    }
45
46
    /**
47
     * Return all items.
48
     *
49
     * @return array
50
     */
51
    public function all()
52
    {
53
        return $this->items;
54
    }
55
56
    /**
57
     * Return specific items.
58
     *
59
     * @param array $keys
60
     *
61
     * @return \EasyWeChat\Kernel\Support\Collection
62
     */
63
    public function only(array $keys)
64
    {
65
        $return = [];
66
67
        foreach ($keys as $key) {
68
            $value = $this->get($key);
69
70
            if (!is_null($value)) {
71
                $return[$key] = $value;
72
            }
73
        }
74
75
        return new static($return);
76
    }
77
78
    /**
79
     * Get all items except for those with the specified keys.
80
     *
81
     * @param mixed $keys
82
     *
83
     * @return static
84
     */
85
    public function except($keys)
86
    {
87
        $keys = is_array($keys) ? $keys : func_get_args();
88
89
        return new static(Arr::except($this->items, $keys));
90
    }
91
92
    /**
93
     * Merge data.
94
     *
95
     * @param Collection|array $items
96
     *
97
     * @return \EasyWeChat\Kernel\Support\Collection
98
     */
99
    public function merge($items)
100
    {
101
        foreach ($items as $key => $value) {
102
            $this->set($key, $value);
103
        }
104
105
        return new static($this->all());
106
    }
107
108
    /**
109
     * To determine Whether the specified element exists.
110
     *
111
     * @param string $key
112
     *
113
     * @return bool
114
     */
115
    public function has($key)
116
    {
117
        return !is_null(Arr::get($this->items, $key));
118
    }
119
120
    /**
121
     * Retrieve the first item.
122
     *
123
     * @return mixed
124
     */
125
    public function first()
126
    {
127
        return reset($this->items);
128
    }
129
130
    /**
131
     * Retrieve the last item.
132
     *
133
     * @return bool
134
     */
135
    public function last()
136
    {
137
        $end = end($this->items);
138
139
        reset($this->items);
140
141
        return $end;
142
    }
143
144
    /**
145
     * add the item value.
146
     *
147
     * @param string $key
148
     * @param mixed  $value
149
     */
150
    public function add($key, $value)
151
    {
152
        Arr::set($this->items, $key, $value);
153
    }
154
155
    /**
156
     * Set the item value.
157
     *
158
     * @param string $key
159
     * @param mixed  $value
160
     */
161
    public function set($key, $value)
162
    {
163
        Arr::set($this->items, $key, $value);
164
    }
165
166
    /**
167
     * Retrieve item from Collection.
168
     *
169
     * @param string $key
170
     * @param mixed  $default
171
     *
172
     * @return mixed
173
     */
174
    public function get($key, $default = null)
175
    {
176
        return Arr::get($this->items, $key, $default);
177
    }
178
179
    /**
180
     * Remove item form Collection.
181
     *
182
     * @param string $key
183
     */
184
    public function forget($key)
185
    {
186
        Arr::forget($this->items, $key);
187
    }
188
189
    /**
190
     * Build to array.
191
     *
192
     * @return array
193
     */
194
    public function toArray()
195
    {
196
        return $this->all();
197
    }
198
199
    /**
200
     * Build to json.
201
     *
202
     * @param int $option
203
     *
204
     * @return string
205
     */
206
    public function toJson($option = JSON_UNESCAPED_UNICODE)
207
    {
208
        return json_encode($this->all(), $option);
209
    }
210
211
    /**
212
     * To string.
213
     *
214
     * @return string
215
     */
216
    public function __toString()
217
    {
218
        return $this->toJson();
219
    }
220
221
    /**
222
     * (PHP 5 &gt;= 5.4.0)<br/>
223
     * Specify data which should be serialized to JSON.
224
     *
225
     * @see http://php.net/manual/en/jsonserializable.jsonserialize.php
226
     *
227
     * @return mixed data which can be serialized by <b>json_encode</b>,
228
     *               which is a value of any type other than a resource
229
     */
230
    public function jsonSerialize()
231
    {
232
        return $this->items;
233
    }
234
235
    /**
236
     * (PHP 5 &gt;= 5.1.0)<br/>
237
     * String representation of object.
238
     *
239
     * @see http://php.net/manual/en/serializable.serialize.php
240
     *
241
     * @return string the string representation of the object or null
242
     */
243
    public function serialize()
244
    {
245
        return serialize($this->items);
246
    }
247
248
    /**
249
     * (PHP 5 &gt;= 5.0.0)<br/>
250
     * Retrieve an external iterator.
251
     *
252
     * @see http://php.net/manual/en/iteratoraggregate.getiterator.php
253
     *
254
     * @return \ArrayIterator An instance of an object implementing <b>Iterator</b> or
255
     *                        <b>Traversable</b>
256
     */
257
    public function getIterator()
258
    {
259
        return new ArrayIterator($this->items);
260
    }
261
262
    /**
263
     * (PHP 5 &gt;= 5.1.0)<br/>
264
     * Count elements of an object.
265
     *
266
     * @see http://php.net/manual/en/countable.count.php
267
     *
268
     * @return int The custom count as an integer.
269
     *             </p>
270
     *             <p>
271
     *             The return value is cast to an integer
272
     */
273
    public function count()
274
    {
275
        return count($this->items);
276
    }
277
278
    /**
279
     * (PHP 5 &gt;= 5.1.0)<br/>
280
     * Constructs the object.
281
     *
282
     * @see  http://php.net/manual/en/serializable.unserialize.php
283
     *
284
     * @param string $serialized <p>
285
     *                           The string representation of the object.
286
     *                           </p>
287
     *
288
     * @return mixed|void
289
     */
290
    public function unserialize($serialized)
291
    {
292
        return $this->items = unserialize($serialized);
293
    }
294
295
    /**
296
     * Get a data by key.
297
     *
298
     * @param string $key
299
     *
300
     * @return mixed
301
     */
302
    public function __get($key)
303
    {
304
        return $this->get($key);
305
    }
306
307
    /**
308
     * Assigns a value to the specified data.
309
     *
310
     * @param string $key
311
     * @param mixed  $value
312
     */
313
    public function __set($key, $value)
314
    {
315
        $this->set($key, $value);
316
    }
317
318
    /**
319
     * Whether or not an data exists by key.
320
     *
321
     * @param string $key
322
     *
323
     * @return bool
324
     */
325
    public function __isset($key)
326
    {
327
        return $this->has($key);
328
    }
329
330
    /**
331
     * Unset an data by key.
332
     *
333
     * @param string $key
334
     */
335
    public function __unset($key)
336
    {
337
        $this->forget($key);
338
    }
339
340
    /**
341
     * var_export.
342
     *
343
     * @return array
344
     */
345
    public function __set_state()
346
    {
347
        return $this->all();
348
    }
349
350
    /**
351
     * (PHP 5 &gt;= 5.0.0)<br/>
352
     * Whether a offset exists.
353
     *
354
     * @see http://php.net/manual/en/arrayaccess.offsetexists.php
355
     *
356
     * @param mixed $offset <p>
357
     *                      An offset to check for.
358
     *                      </p>
359
     *
360
     * @return bool true on success or false on failure.
361
     *              The return value will be casted to boolean if non-boolean was returned
362
     */
363
    public function offsetExists($offset)
364
    {
365
        return $this->has($offset);
366
    }
367
368
    /**
369
     * (PHP 5 &gt;= 5.0.0)<br/>
370
     * Offset to unset.
371
     *
372
     * @see http://php.net/manual/en/arrayaccess.offsetunset.php
373
     *
374
     * @param mixed $offset <p>
375
     *                      The offset to unset.
376
     *                      </p>
377
     */
378
    public function offsetUnset($offset)
379
    {
380
        if ($this->offsetExists($offset)) {
381
            $this->forget($offset);
382
        }
383
    }
384
385
    /**
386
     * (PHP 5 &gt;= 5.0.0)<br/>
387
     * Offset to retrieve.
388
     *
389
     * @see http://php.net/manual/en/arrayaccess.offsetget.php
390
     *
391
     * @param mixed $offset <p>
392
     *                      The offset to retrieve.
393
     *                      </p>
394
     *
395
     * @return mixed Can return all value types
396
     */
397
    public function offsetGet($offset)
398
    {
399
        return $this->offsetExists($offset) ? $this->get($offset) : null;
400
    }
401
402
    /**
403
     * (PHP 5 &gt;= 5.0.0)<br/>
404
     * Offset to set.
405
     *
406
     * @see http://php.net/manual/en/arrayaccess.offsetset.php
407
     *
408
     * @param mixed $offset <p>
409
     *                      The offset to assign the value to.
410
     *                      </p>
411
     * @param mixed $value  <p>
412
     *                      The value to set.
413
     *                      </p>
414
     */
415
    public function offsetSet($offset, $value)
416
    {
417
        $this->set($offset, $value);
418
    }
419
}
420