|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* slince config component |
|
4
|
|
|
* @author Tao <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace Slince\Config; |
|
7
|
|
|
|
|
8
|
|
|
class Collection implements \ArrayAccess, \Countable, \IteratorAggregate |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Array of data |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $data; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct(array $data = []) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->data = $data; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Returns data |
|
23
|
|
|
* @return array |
|
24
|
|
|
*/ |
|
25
|
|
|
public function toArray() |
|
26
|
|
|
{ |
|
27
|
|
|
return $this->data; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Sets a item with its key and value |
|
32
|
|
|
* @param int|string $key |
|
33
|
|
|
* @param mixed $value |
|
34
|
|
|
* @return void |
|
35
|
|
|
*/ |
|
36
|
|
|
public function set($key, $value) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->data[$key] = $value; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Gets the value by the specified key |
|
43
|
|
|
* @param int|string $key |
|
44
|
|
|
* @param mixed $defaultValue |
|
45
|
|
|
* @return mixed |
|
46
|
|
|
*/ |
|
47
|
|
|
public function get($key, $defaultValue = null) |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->exists($key) ? $this->data[$key] : $defaultValue; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Merges a array to the collection |
|
54
|
|
|
* @param array $data |
|
55
|
|
|
*/ |
|
56
|
|
|
public function merge(array $data) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->data = array_replace($this->data, $data); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Checks whether the item exists |
|
63
|
|
|
* @param int|string $key |
|
64
|
|
|
* @return boolean |
|
65
|
|
|
*/ |
|
66
|
|
|
public function exists($key) |
|
67
|
|
|
{ |
|
68
|
|
|
return isset($this->data[$key]); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Remove a item by its key |
|
73
|
|
|
* @param mixed $key |
|
74
|
|
|
*/ |
|
75
|
|
|
public function delete($key) |
|
76
|
|
|
{ |
|
77
|
|
|
unset($this->data[$key]); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Clears the collection |
|
82
|
|
|
*/ |
|
83
|
|
|
public function clear() |
|
84
|
|
|
{ |
|
85
|
|
|
$this->data = []; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Clears the collection |
|
90
|
|
|
* @deprecated |
|
91
|
|
|
*/ |
|
92
|
|
|
public function flush() |
|
93
|
|
|
{ |
|
94
|
|
|
$this->clear(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* {@inheritdoc} |
|
99
|
|
|
*/ |
|
100
|
|
|
public function offsetGet($offset) |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->get($offset); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* {@inheritdoc} |
|
107
|
|
|
*/ |
|
108
|
|
|
public function offsetSet($offset, $value) |
|
109
|
|
|
{ |
|
110
|
|
|
$this->set($offset, $value); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* {@inheritdoc} |
|
115
|
|
|
*/ |
|
116
|
|
|
public function offsetUnset($offset) |
|
117
|
|
|
{ |
|
118
|
|
|
$this->delete($offset); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* {@inheritdoc} |
|
123
|
|
|
*/ |
|
124
|
|
|
public function offsetExists($offset) |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->exists($offset); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* {@inheritdoc} |
|
132
|
|
|
*/ |
|
133
|
|
|
public function count() |
|
134
|
|
|
{ |
|
135
|
|
|
return count($this->data); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* {@inheritdoc} |
|
140
|
|
|
*/ |
|
141
|
|
|
public function getIterator() |
|
142
|
|
|
{ |
|
143
|
|
|
return new \ArrayIterator($this->data); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|