Collection::toArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the strays/baidu-ai.
5
 *
6
 * (c) strays <[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 Strays\BaiDuAi\Kernel\Support;
13
14
use ArrayAccess;
15
use ArrayIterator;
16
use Countable;
17
use IteratorAggregate;
18
use JsonSerializable;
19
use Serializable;
20
21
class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Serializable
22
{
23
    /**
24
     * The collection data.
25
     *
26
     * @var array
27
     */
28
    protected $items = [];
29
30
    /**
31
     * set data.
32
     *
33
     * @param mixed $items
34
     */
35
    public function __construct(array $items = [])
36
    {
37
        foreach ($items as $key => $value) {
38
            $this->set($key, $value);
39
        }
40
    }
41
42
    /**
43
     * Return all items.
44
     *
45
     * @return array
46
     */
47
    public function all()
48
    {
49
        return $this->items;
50
    }
51
52
    /**
53
     * @param array $keys
54
     *
55
     * @return Collection
56
     */
57
    public function only(array $keys)
58
    {
59
        $return = [];
60
61
        foreach ($keys as $key) {
62
            $value = $this->get($key);
63
64
            if (!is_null($value)) {
65
                $return[$key] = $value;
66
            }
67
        }
68
69
        return new static($return);
70
    }
71
72
    /**
73
     * Get all items except for those with the specified keys.
74
     *
75
     * @param mixed $keys
76
     *
77
     * @return static
78
     */
79
    public function except($keys)
80
    {
81
        $keys = is_array($keys) ? $keys : func_get_args();
82
83
        return new static(Arr::except($this->items, $keys));
0 ignored issues
show
Bug introduced by
The type Strays\BaiDuAi\Kernel\Support\Arr was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
84
    }
85
86
    /**
87
     * @param $items
88
     *
89
     * @return Collection
90
     */
91
    public function merge($items)
92
    {
93
        $clone = new static($this->all());
94
95
        foreach ($items as $key => $value) {
96
            $clone->set($key, $value);
97
        }
98
99
        return $clone;
100
    }
101
102
    /**
103
     * To determine Whether the specified element exists.
104
     *
105
     * @param string $key
106
     *
107
     * @return bool
108
     */
109
    public function has($key)
110
    {
111
        return !is_null(Arr::get($this->items, $key));
112
    }
113
114
    /**
115
     * Retrieve the first item.
116
     *
117
     * @return mixed
118
     */
119
    public function first()
120
    {
121
        return reset($this->items);
122
    }
123
124
    /**
125
     * Retrieve the last item.
126
     *
127
     * @return bool
128
     */
129
    public function last()
130
    {
131
        $end = end($this->items);
132
133
        reset($this->items);
134
135
        return $end;
136
    }
137
138
    /**
139
     * add the item value.
140
     *
141
     * @param string $key
142
     * @param mixed  $value
143
     */
144
    public function add($key, $value)
145
    {
146
        Arr::set($this->items, $key, $value);
147
    }
148
149
    /**
150
     * Set the item value.
151
     *
152
     * @param string $key
153
     * @param mixed  $value
154
     */
155
    public function set($key, $value)
156
    {
157
        Arr::set($this->items, $key, $value);
158
    }
159
160
    /**
161
     * Retrieve item from Collection.
162
     *
163
     * @param string $key
164
     * @param mixed  $default
165
     *
166
     * @return mixed
167
     */
168
    public function get($key, $default = null)
169
    {
170
        return Arr::get($this->items, $key, $default);
171
    }
172
173
    /**
174
     * Remove item form Collection.
175
     *
176
     * @param string $key
177
     */
178
    public function forget($key)
179
    {
180
        Arr::forget($this->items, $key);
181
    }
182
183
    /**
184
     * Build to array.
185
     *
186
     * @return array
187
     */
188
    public function toArray()
189
    {
190
        return $this->all();
191
    }
192
193
    /**
194
     * Build to json.
195
     *
196
     * @param int $option
197
     *
198
     * @return string
199
     */
200
    public function toJson($option = JSON_UNESCAPED_UNICODE)
201
    {
202
        return json_encode($this->all(), $option);
203
    }
204
205
    /**
206
     * To string.
207
     *
208
     * @return string
209
     */
210
    public function __toString()
211
    {
212
        return $this->toJson();
213
    }
214
215
    /**
216
     * (PHP 5 &gt;= 5.4.0)<br/>
217
     * Specify data which should be serialized to JSON.
218
     *
219
     * @see http://php.net/manual/en/jsonserializable.jsonserialize.php
220
     *
221
     * @return mixed data which can be serialized by <b>json_encode</b>,
222
     *               which is a value of any type other than a resource
223
     */
224
    public function jsonSerialize()
225
    {
226
        return $this->items;
227
    }
228
229
    /**
230
     * (PHP 5 &gt;= 5.1.0)<br/>
231
     * String representation of object.
232
     *
233
     * @see http://php.net/manual/en/serializable.serialize.php
234
     *
235
     * @return string the string representation of the object or null
236
     */
237
    public function serialize()
238
    {
239
        return serialize($this->items);
240
    }
241
242
    /**
243
     * (PHP 5 &gt;= 5.0.0)<br/>
244
     * Retrieve an external iterator.
245
     *
246
     * @see http://php.net/manual/en/iteratoraggregate.getiterator.php
247
     *
248
     * @return \ArrayIterator An instance of an object implementing <b>Iterator</b> or
249
     *                        <b>Traversable</b>
250
     */
251
    public function getIterator()
252
    {
253
        return new ArrayIterator($this->items);
254
    }
255
256
    /**
257
     * (PHP 5 &gt;= 5.1.0)<br/>
258
     * Count elements of an object.
259
     *
260
     * @see http://php.net/manual/en/countable.count.php
261
     *
262
     * @return int the custom count as an integer.
263
     *             </p>
264
     *             <p>
265
     *             The return value is cast to an integer
266
     */
267
    public function count()
268
    {
269
        return count($this->items);
270
    }
271
272
    /**
273
     * (PHP 5 &gt;= 5.1.0)<br/>
274
     * Constructs the object.
275
     *
276
     * @see  http://php.net/manual/en/serializable.unserialize.php
277
     *
278
     * @param string $serialized <p>
279
     *                           The string representation of the object.
280
     *                           </p>
281
     *
282
     * @return mixed|void
283
     */
284
    public function unserialize($serialized)
285
    {
286
        return $this->items = unserialize($serialized);
287
    }
288
289
    /**
290
     * Get a data by key.
291
     *
292
     * @param string $key
293
     *
294
     * @return mixed
295
     */
296
    public function __get($key)
297
    {
298
        return $this->get($key);
299
    }
300
301
    /**
302
     * Assigns a value to the specified data.
303
     *
304
     * @param string $key
305
     * @param mixed  $value
306
     */
307
    public function __set($key, $value)
308
    {
309
        $this->set($key, $value);
310
    }
311
312
    /**
313
     * Whether or not an data exists by key.
314
     *
315
     * @param string $key
316
     *
317
     * @return bool
318
     */
319
    public function __isset($key)
320
    {
321
        return $this->has($key);
322
    }
323
324
    /**
325
     * Unset an data by key.
326
     *
327
     * @param string $key
328
     */
329
    public function __unset($key)
330
    {
331
        $this->forget($key);
332
    }
333
334
    /**
335
     * var_export.
336
     *
337
     * @return array
338
     */
339
    public function __set_state()
340
    {
341
        return $this->all();
342
    }
343
344
    /**
345
     * (PHP 5 &gt;= 5.0.0)<br/>
346
     * Whether a offset exists.
347
     *
348
     * @see http://php.net/manual/en/arrayaccess.offsetexists.php
349
     *
350
     * @param mixed $offset <p>
351
     *                      An offset to check for.
352
     *                      </p>
353
     *
354
     * @return bool true on success or false on failure.
355
     *              The return value will be casted to boolean if non-boolean was returned
356
     */
357
    public function offsetExists($offset)
358
    {
359
        return $this->has($offset);
360
    }
361
362
    /**
363
     * (PHP 5 &gt;= 5.0.0)<br/>
364
     * Offset to unset.
365
     *
366
     * @see http://php.net/manual/en/arrayaccess.offsetunset.php
367
     *
368
     * @param mixed $offset <p>
369
     *                      The offset to unset.
370
     *                      </p>
371
     */
372
    public function offsetUnset($offset)
373
    {
374
        if ($this->offsetExists($offset)) {
375
            $this->forget($offset);
376
        }
377
    }
378
379
    /**
380
     * (PHP 5 &gt;= 5.0.0)<br/>
381
     * Offset to retrieve.
382
     *
383
     * @see http://php.net/manual/en/arrayaccess.offsetget.php
384
     *
385
     * @param mixed $offset <p>
386
     *                      The offset to retrieve.
387
     *                      </p>
388
     *
389
     * @return mixed Can return all value types
390
     */
391
    public function offsetGet($offset)
392
    {
393
        return $this->offsetExists($offset) ? $this->get($offset) : null;
394
    }
395
396
    /**
397
     * (PHP 5 &gt;= 5.0.0)<br/>
398
     * Offset to set.
399
     *
400
     * @see http://php.net/manual/en/arrayaccess.offsetset.php
401
     *
402
     * @param mixed $offset <p>
403
     *                      The offset to assign the value to.
404
     *                      </p>
405
     * @param mixed $value  <p>
406
     *                      The value to set.
407
     *                      </p>
408
     */
409
    public function offsetSet($offset, $value)
410
    {
411
        $this->set($offset, $value);
412
    }
413
}
414