Collection::serialize()   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
 * This file is part of the dingtalk.
4
 * User: Ilham Tahir <[email protected]>
5
 * This source file is subject to the MIT license that is bundled
6
 * with this source code in the file LICENSE.
7
 */
8
9
namespace Aplisin\DingTalk\Kernel\Support;
10
11
use ArrayAccess;
12
use Countable;
13
use Aplisin\DingTalk\Kernel\Contracts\Arrayable;
14
use IteratorAggregate;
15
use JsonSerializable;
16
use Serializable;
17
use ArrayIterator;
18
19
class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Serializable, Arrayable
20
{
21
    /**
22
     * @var array
23
     */
24
    protected $items = [];
25
26
    public function __construct(array $items = [])
27
    {
28
        foreach ($items as $key => $value) {
29
            $this->set($key, $value);
30
        }
31
    }
32
33
    /**
34
     * Return all items.
35
     *
36
     * @return array
37
     */
38
    public function all()
39
    {
40
        return $this->items;
41
    }
42
43
    public function only(array $keys)
44
    {
45
        $return = [];
46
        foreach ($keys as $key) {
47
            $value = $this->get($key);
48
            if (!is_null($value)) {
49
                $return[$key] = $value;
50
            }
51
        }
52
        return new static($return);
53
    }
54
55
    public function except($keys)
56
    {
57
        $keys = is_array($keys) ? $keys : func_get_args();
58
        return new static(Arr::except($this->items, $keys));
59
    }
60
61
    public function merge($items)
62
    {
63
        foreach ($items as $key => $value) {
64
            $this->set($key, $value);
65
        }
66
        return new static($this->all());
67
    }
68
69
    public function has($key)
70
    {
71
        return !is_null(Arr::get($this->items, $key));
72
    }
73
74
    public function first()
75
    {
76
        return reset($this->items);
77
    }
78
79
    public function last()
80
    {
81
        $end = end($this->items);
82
        reset($this->items);
83
        return $end;
84
    }
85
86
    public function add($key, $value)
87
    {
88
        Arr::set($this->items, $key, $value);
89
    }
90
91
    public function set($key, $value)
92
    {
93
        Arr::set($this->items, $key, $value);
94
    }
95
96
    public function get($key, $default = null)
97
    {
98
        return Arr::get($this->items, $key, $default);
99
    }
100
101
    public function forget($key)
102
    {
103
        Arr::forget($this->items, $key);
104
    }
105
106
    public function toArray()
107
    {
108
        return $this->all();
109
    }
110
111
    public function toJson($option = JSON_UNESCAPED_UNICODE)
112
    {
113
        return json_encode($this->all(), $option);
114
    }
115
116
    public function __toString()
117
    {
118
        return $this->toJson();
119
    }
120
121
    public function jsonSerialize()
122
    {
123
        return $this->items;
124
    }
125
126
127
    public function serialize()
128
    {
129
        return serialize($this->items);
130
    }
131
132
    public function getIterator()
133
    {
134
        return new ArrayIterator($this->items);
135
    }
136
137
    public function count()
138
    {
139
        return count($this->items);
140
    }
141
142
    public function unserialize($serialized)
143
    {
144
        return $this->items = unserialize($serialized);
145
    }
146
147
    public function __get($key)
148
    {
149
        return $this->get($key);
150
    }
151
152
    public function __set($key, $value)
153
    {
154
        $this->set($key, $value);
155
    }
156
157
    public function __isset($key)
158
    {
159
        return $this->has($key);
160
    }
161
162
    public function __unset($key)
163
    {
164
        $this->forget($key);
165
    }
166
167
    public function __set_state()
168
    {
169
        return $this->all();
170
    }
171
172
    public function offsetExists($offset)
173
    {
174
        return $this->has($offset);
175
    }
176
177
    public function offsetUnset($offset)
178
    {
179
        if ($this->offsetExists($offset)) {
180
            $this->forget($offset);
181
        }
182
    }
183
184
    public function offsetGet($offset)
185
    {
186
        return $this->offsetExists($offset) ? $this->get($offset) : null;
187
    }
188
189
    public function offsetSet($offset, $value)
190
    {
191
        $this->set($offset, $value);
192
    }
193
}
194