Completed
Push — master ( fc1f17...38251d )
by Xu
07:16
created

Collection   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 335
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 335
rs 8.8
c 0
b 0
f 0
wmc 36

29 Methods

Rating   Name   Duplication   Size   Complexity  
A merge() 0 6 2
A toArray() 0 3 1
A only() 0 10 3
A __unset() 0 3 1
A offsetGet() 0 3 2
A unserialize() 0 3 1
A has() 0 3 1
A forget() 0 3 1
A __set_state() 0 3 1
A jsonSerialize() 0 3 1
A first() 0 3 1
A offsetUnset() 0 4 2
A offsetSet() 0 3 1
A __set() 0 3 1
A getIterator() 0 3 1
A __toString() 0 3 1
A __get() 0 3 1
A last() 0 5 1
A offsetExists() 0 3 1
A all() 0 3 1
A serialize() 0 3 1
A get() 0 3 1
A add() 0 3 1
A set() 0 3 1
A count() 0 3 1
A toJson() 0 3 1
A except() 0 4 2
A __construct() 0 4 2
A __isset() 0 3 1
1
<?php
2
/**
3
 * @link http://www.tintsoft.com/
4
 * @copyright Copyright (c) 2012 TintSoft Technology Co. Ltd.
5
 * @license http://www.tintsoft.com/license/
6
 */
7
8
namespace yuncms\base;
9
10
use Countable;
11
use ArrayAccess;
12
use ArrayIterator;
13
use JsonSerializable;
14
use IteratorAggregate;
15
use Serializable;
16
use Traversable;
17
use yuncms\helpers\ArrayHelper;
18
use yuncms\helpers\Json;
19
20
/**
21
 * Collection
22
 * 
23
 * @package yuncms\base
24
 * @author Tongle Xu <[email protected]>
25
 */
26
class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Serializable
27
{
28
    /**
29
     * The collection data.
30
     *
31
     * @var array
32
     */
33
    protected $items = [];
34
35
    /**
36
     * set data.
37
     *
38
     * @param mixed $items
39
     */
40
    public function __construct(array $items = [])
41
    {
42
        foreach ($items as $key => $value) {
43
            $this->set($key, $value);
44
        }
45
    }
46
47
    /**
48
     * Return all items.
49
     *
50
     * @return array
51
     */
52
    public function all()
53
    {
54
        return $this->items;
55
    }
56
57
    /**
58
     * Return specific items.
59
     *
60
     * @param array $keys
61
     * @return array
62
     */
63
    public function only(array $keys)
64
    {
65
        $return = [];
66
        foreach ($keys as $key) {
67
            $value = $this->get($key);
68
            if (!is_null($value)) {
69
                $return[$key] = $value;
70
            }
71
        }
72
        return $return;
73
    }
74
75
    /**
76
     * Get all items except for those with the specified keys.
77
     *
78
     * @param mixed $keys
79
     * @return static
80
     */
81
    public function except($keys)
82
    {
83
        $keys = is_array($keys) ? $keys : func_get_args();
84
        return new static(ArrayHelper::except($this->items, $keys));
85
    }
86
87
    /**
88
     * Merge data.
89
     *
90
     * @param Collection|array $items
91
     *
92
     * @return array
93
     */
94
    public function merge($items)
95
    {
96
        foreach ($items as $key => $value) {
97
            $this->set($key, $value);
98
        }
99
        return $this->all();
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(ArrayHelper::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():bool
130
    {
131
        $end = end($this->items);
132
        reset($this->items);
133
        return $end;
134
    }
135
136
    /**
137
     * add the item value.
138
     *
139
     * @param string $key
140
     * @param mixed  $value
141
     */
142
    public function add($key, $value)
143
    {
144
        ArrayHelper::set($this->items, $key, $value);
145
    }
146
147
    /**
148
     * Set the item value.
149
     *
150
     * @param string $key
151
     * @param mixed  $value
152
     */
153
    public function set($key, $value)
154
    {
155
        ArrayHelper::set($this->items, $key, $value);
156
    }
157
158
    /**
159
     * Retrieve item from Collection.
160
     *
161
     * @param string $key
162
     * @param mixed  $default
163
     * @return mixed
164
     */
165
    public function get($key, $default = null)
166
    {
167
        return ArrayHelper::get($this->items, $key, $default);
168
    }
169
170
    /**
171
     * Remove item form Collection.
172
     *
173
     * @param string $key
174
     */
175
    public function forget($key)
176
    {
177
        ArrayHelper::forget($this->items, $key);
178
    }
179
180
    /**
181
     * Build to array.
182
     *
183
     * @return array
184
     */
185
    public function toArray()
186
    {
187
        return $this->all();
188
    }
189
190
    /**
191
     * Build to json.
192
     *
193
     * @param int $option
194
     * @return string
195
     */
196
    public function toJson($option = JSON_UNESCAPED_UNICODE)
197
    {
198
        return Json::encode($this->all(), $option);
199
    }
200
201
    /**
202
     * To string.
203
     *
204
     * @return string
205
     */
206
    public function __toString()
207
    {
208
        return $this->toJson();
209
    }
210
211
    /**
212
     * Specify data which should be serialized to JSON.
213
     *
214
     * @return mixed data which can be serialized by <b>json_encode</b>,
215
     *               which is a value of any type other than a resource
216
     */
217
    public function jsonSerialize()
218
    {
219
        return $this->items;
220
    }
221
222
    /**
223
     * String representation of object.
224
     *
225
     * @return string the string representation of the object or null
226
     */
227
    public function serialize()
228
    {
229
        return serialize($this->items);
230
    }
231
232
    /**
233
     * Retrieve an external iterator.
234
     *
235
     * @return ArrayIterator|Traversable An instance of an object implementing Iterator or Traversable
236
     */
237
    public function getIterator()
238
    {
239
        return new ArrayIterator($this->items);
240
    }
241
242
    /**
243
     * Count elements of an object.
244
     *
245
     * @return int The custom count as an integer.
246
     *             The return value is cast to an integer
247
     */
248
    public function count()
249
    {
250
        return count($this->items);
251
    }
252
253
    /**
254
     * Constructs the object.
255
     *
256
     * @param string $serialized The string representation of the object.
257
     * @return void
258
     */
259
    public function unserialize($serialized)
260
    {
261
        $this->items = unserialize($serialized);
262
    }
263
264
    /**
265
     * Get a data by key.
266
     *
267
     * @param string $key
268
     * @return mixed
269
     */
270
    public function __get($key)
271
    {
272
        return $this->get($key);
273
    }
274
275
    /**
276
     * Assigns a value to the specified data.
277
     *
278
     * @param string $key
279
     * @param mixed  $value
280
     */
281
    public function __set($key, $value)
282
    {
283
        $this->set($key, $value);
284
    }
285
286
    /**
287
     * Whether or not an data exists by key.
288
     *
289
     * @param string $key
290
     * @return bool
291
     */
292
    public function __isset($key)
293
    {
294
        return $this->has($key);
295
    }
296
297
    /**
298
     * Unsets an data by key.
299
     *
300
     * @param string $key
301
     */
302
    public function __unset($key)
303
    {
304
        $this->forget($key);
305
    }
306
307
    /**
308
     * var_export.
309
     *
310
     * @return array
311
     */
312
    public function __set_state()
313
    {
314
        return $this->all();
315
    }
316
317
    /**
318
     * Whether a offset exists.
319
     *
320
     * @param mixed $offset An offset to check for.
321
     * @return bool true on success or false on failure.
322
     *              The return value will be casted to boolean if non-boolean was returned
323
     */
324
    public function offsetExists($offset)
325
    {
326
        return $this->has($offset);
327
    }
328
329
    /**
330
     * Offset to unset.
331
     *
332
     * @param mixed $offset The offset to unset.
333
     */
334
    public function offsetUnset($offset)
335
    {
336
        if ($this->offsetExists($offset)) {
337
            $this->forget($offset);
338
        }
339
    }
340
341
    /**
342
     * Offset to retrieve.
343
     *
344
     * @param mixed $offset The offset to retrieve.
345
     * @return mixed Can return all value types
346
     */
347
    public function offsetGet($offset)
348
    {
349
        return $this->offsetExists($offset) ? $this->get($offset) : null;
350
    }
351
352
    /**
353
     * Offset to set.
354
     *
355
     * @param mixed $offset The offset to assign the value to.
356
     * @param mixed $value The value to set.
357
     */
358
    public function offsetSet($offset, $value)
359
    {
360
        $this->set($offset, $value);
361
    }
362
}