Test Failed
Push — master ( 90f1e5...8cd561 )
by Jim
02:25
created

Collection::forget()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: lenovo
5
 * Date: 6/13/2018
6
 * Time: 11:29 PM
7
 */
8
9
namespace TimSDK\Support;
10
11
use ArrayAccess;
12
use ArrayIterator;
13
use Countable;
14
use IteratorAggregate;
15
use JsonSerializable;
16
use Serializable;
17
use TimSDK\Core\Exceptions\JsonParseException;
18
19
class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Serializable
20
{
21
    /**
22
     * The collection data.
23
     *
24
     * @var array
25
     */
26
    protected $items = [];
27
28
    public function __construct($items = [])
29
    {
30
        $this->items = $this->getArrayableItems($items);
31
    }
32
33
    /**
34
     * Return all items.
35
     *
36
     * @return array
37
     */
38
    public function all()
39
    {
40
        return $this->items;
41
    }
42
43
    /**
44
     * Return specific items.
45
     *
46
     * @param array $keys
47
     *
48
     * @return array
49
     */
50
    public function only($keys)
51
    {
52
        if (is_string($keys)) {
0 ignored issues
show
introduced by
The condition is_string($keys) is always false.
Loading history...
53
            $keys = explode(',', str_replace(' ', '', $keys));
54
        }
55
56
        $return = [];
57
        foreach ($keys as $key) {
58
            $value = $this->get($key);
59
            if (!is_null($value)) {
60
                $return[$key] = $value;
61
            }
62
        }
63
        return $return;
64
    }
65
66
    /**
67
     * Replace the items
68
     *
69
     * @param $items
70
     */
71
    public function setAll($items)
72
    {
73
        $this->items = $this->getArrayableItems($items);
74
    }
75
76
    /**
77
     * Set the item value.
78
     *
79
     * @param string $key
80
     * @param mixed  $value
81
     */
82
    public function set($key, $value)
83
    {
84
        if (is_null($key)) {
0 ignored issues
show
introduced by
The condition is_null($key) is always false.
Loading history...
85
            $this->items[] = $value;
86
        } else {
87
            Arr::set($this->items, $key, $value);
88
        }
89
    }
90
91
    /**
92
     * Retrieve item from Collection.
93
     *
94
     * @param string $key
95
     * @param mixed  $default
96
     *
97
     * @return mixed
98
     */
99
    public function get($key, $default = null)
100
    {
101
        return Arr::get($this->items, $key, $default);
102
    }
103
104
    /**
105
     * To determine Whether the specified element exists
106
     *
107
     * @param string $key
108
     * @return bool
109
     */
110
    public function has($key)
111
    {
112
        return Arr::has($this->items, $key);
113
    }
114
115
    /**
116
     * Remove item form Collection.
117
     *
118
     * @param array|string $keys
119
     */
120
    public function forget($keys)
121
    {
122
        Arr::forget($this->items, $keys);
123
    }
124
125
    /**
126
     * Build to array.
127
     *
128
     * @return array
129
     */
130
    public function toArray()
131
    {
132
        return $this->all();
133
    }
134
135
    /**
136
     * Build to json.
137
     *
138
     * @param int $option
139
     *
140
     * @return string
141
     */
142
    public function toJson($option = JSON_UNESCAPED_UNICODE)
143
    {
144
        return json_encode($this->all(), $option);
145
    }
146
147
    /**
148
     * Count elements of an object
149
     * @link  http://php.net/manual/en/countable.count.php
150
     * @return int The custom count as an integer.
151
     * </p>
152
     * <p>
153
     * The return value is cast to an integer.
154
     * @since 5.1.0
155
     */
156
    public function count()
157
    {
158
        return count($this->items);
159
    }
160
161
    /**
162
     * Retrieve an external iterator
163
     * @link  http://php.net/manual/en/iteratoraggregate.getiterator.php
164
     * @return ArrayIterator
165
     * <b>Traversable</b>
166
     * @since 5.0.0
167
     */
168
    public function getIterator()
169
    {
170
        return new ArrayIterator($this->items);
171
    }
172
173
    /**
174
     * Specify data which should be serialized to JSON
175
     * @link  http://php.net/manual/en/jsonserializable.jsonserialize.php
176
     * @return mixed data which can be serialized by <b>json_encode</b>,
177
     * which is a value of any type other than a resource.
178
     * @since 5.4.0
179
     */
180
    public function jsonSerialize()
181
    {
182
        return $this->items;
183
    }
184
185
    /**
186
     * String representation of object
187
     * @link  http://php.net/manual/en/serializable.serialize.php
188
     * @return string the string representation of the object or null
189
     * @since 5.1.0
190
     */
191
    public function serialize()
192
    {
193
        return serialize($this->items);
194
    }
195
196
    /**
197
     * Constructs the object
198
     * @link  http://php.net/manual/en/serializable.unserialize.php
199
     * @param string $serialized <p>
200
     *                           The string representation of the object.
201
     *                           </p>
202
     * @return array
203
     * @since 5.1.0
204
     */
205
    public function unserialize($serialized)
206
    {
207
        return $this->items = unserialize($serialized);
208
    }
209
210
    /**
211
     * To string.
212
     *
213
     * @return string
214
     */
215
    public function __toString()
216
    {
217
        return $this->toJson();
218
    }
219
220
    /**
221
     * Assigns a value to the specified data.
222
     *
223
     * @param string $key
224
     * @param mixed  $value
225
     */
226
    public function __set($key, $value)
227
    {
228
        $this->set($key, $value);
229
    }
230
231
    /**
232
     * Get a data by key.
233
     *
234
     * @param string $key
235
     *
236
     * @return mixed
237
     */
238
    public function __get($key)
239
    {
240
        return $this->get($key);
241
    }
242
243
    /**
244
     * Whether or not an data exists by key.
245
     *
246
     * @param string $key
247
     *
248
     * @return bool
249
     */
250
    public function __isset($key)
251
    {
252
        return $this->has($key);
253
    }
254
255
    /**
256
     * Unsets an data by key.
257
     *
258
     * @param string $key
259
     */
260
    public function __unset($key)
261
    {
262
        $this->forget($key);
263
    }
264
265
    /**
266
     * var_export.
267
     *
268
     * @return array
269
     */
270
    public function __set_state()
271
    {
272
        return $this->all();
273
    }
274
275
    /**
276
     * (PHP 5 &gt;= 5.0.0)<br/>
277
     * Whether a offset exists.
278
     *
279
     * @see http://php.net/manual/en/arrayaccess.offsetexists.php
280
     *
281
     * @param mixed $offset <p>
282
     *                      An offset to check for.
283
     *                      </p>
284
     *
285
     * @return bool true on success or false on failure.
286
     *              The return value will be casted to boolean if non-boolean was returned
287
     */
288
    public function offsetExists($offset)
289
    {
290
        return $this->has($offset);
291
    }
292
293
    /**
294
     * (PHP 5 &gt;= 5.0.0)<br/>
295
     * Offset to unset.
296
     *
297
     * @see http://php.net/manual/en/arrayaccess.offsetunset.php
298
     *
299
     * @param mixed $offset <p>
300
     *                      The offset to unset.
301
     *                      </p>
302
     */
303
    public function offsetUnset($offset)
304
    {
305
        if ($this->offsetExists($offset)) {
306
            $this->forget($offset);
307
        }
308
    }
309
310
    /**
311
     * (PHP 5 &gt;= 5.0.0)<br/>
312
     * Offset to retrieve.
313
     *
314
     * @see http://php.net/manual/en/arrayaccess.offsetget.php
315
     *
316
     * @param mixed $offset <p>
317
     *                      The offset to retrieve.
318
     *                      </p>
319
     *
320
     * @return mixed Can return all value types
321
     */
322
    public function offsetGet($offset)
323
    {
324
        return $this->offsetExists($offset) ? $this->get($offset) : null;
325
    }
326
327
    /**
328
     * (PHP 5 &gt;= 5.0.0)<br/>
329
     * Offset to set.
330
     *
331
     * @see http://php.net/manual/en/arrayaccess.offsetset.php
332
     *
333
     * @param mixed $offset <p>
334
     *                      The offset to assign the value to.
335
     *                      </p>
336
     * @param mixed $value  <p>
337
     *                      The value to set.
338
     *                      </p>
339
     */
340
    public function offsetSet($offset, $value)
341
    {
342
        $this->set($offset, $value);
343
    }
344
345
    /**
346
     * Results array of items from Collection or Arrayable.
347
     *
348
     * @param  mixed  $items
349
     * @return array
350
     */
351
    protected function getArrayableItems($items)
352
    {
353
        if ($items instanceof self) {
354
            return $items->all();
355
        } elseif ($items instanceof JsonSerializable) {
356
            return $items->jsonSerialize();
357
        } elseif (is_string($items)) {
358
            try {
359
                return Json::decode($items, true);
360
            } catch (JsonParseException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
361
            }
362
        }
363
364
        return (array) $items;
365
    }
366
}
367