1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Borobudur-Http package. |
4
|
|
|
* |
5
|
|
|
* (c) Hexacodelabs <http://hexacodelabs.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Borobudur\Http\Session\Attribute; |
12
|
|
|
|
13
|
|
|
use ArrayIterator; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Iqbal Maulana <[email protected]> |
17
|
|
|
* @created 8/3/15 |
18
|
|
|
*/ |
19
|
|
|
class AttributeBag implements AttributeBagInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $name = 'attributes'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $storageKey; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $attributes = array(); |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Constructor. |
38
|
|
|
* |
39
|
|
|
* @param string $storageKey |
40
|
|
|
*/ |
41
|
|
|
public function __construct($storageKey = '__BOROBUDUR_ATTRIBUTES', $name = null) |
42
|
|
|
{ |
43
|
|
|
$this->storageKey = $storageKey; |
44
|
|
|
if (null !== $name) { |
45
|
|
|
$this->name = $name; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Clear attributes and replace with sets of it. |
51
|
|
|
* |
52
|
|
|
* @param array $attributes |
53
|
|
|
* |
54
|
|
|
* @return $this |
55
|
|
|
*/ |
56
|
|
|
public function replace(array $attributes) |
57
|
|
|
{ |
58
|
|
|
$this->attributes = array(); |
59
|
|
|
|
60
|
|
|
return $this->add($attributes); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Add sets of attribute. |
65
|
|
|
* |
66
|
|
|
* @param array $attributes |
67
|
|
|
* |
68
|
|
|
* @return $this |
69
|
|
|
*/ |
70
|
|
|
public function add(array $attributes) |
71
|
|
|
{ |
72
|
|
|
foreach ($attributes as $key => $value) { |
73
|
|
|
$this->set($key, $value); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set attribute. |
81
|
|
|
* |
82
|
|
|
* @param string $key |
83
|
|
|
* @param mixed $value |
84
|
|
|
* |
85
|
|
|
* @return $this |
86
|
|
|
*/ |
87
|
|
|
public function set($key, $value) |
88
|
|
|
{ |
89
|
|
|
$this->attributes[$key] = $value; |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get attribute value by key or return default value if not exist. |
96
|
|
|
* |
97
|
|
|
* @param string $key |
98
|
|
|
* @param mixed|null $default |
99
|
|
|
* |
100
|
|
|
* @return mixed |
101
|
|
|
*/ |
102
|
|
|
public function get($key, $default = null) |
103
|
|
|
{ |
104
|
|
|
if ($this->has($key)) { |
105
|
|
|
return $this->attributes[$key]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $default; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Check if attribute exist by key. |
113
|
|
|
* |
114
|
|
|
* @param string $key |
115
|
|
|
* |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
|
|
public function has($key) |
119
|
|
|
{ |
120
|
|
|
return array_key_exists($key, $this->attributes); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get all attributes. |
125
|
|
|
* |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
|
|
public function all() |
129
|
|
|
{ |
130
|
|
|
return $this->attributes; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Remove attribute by key. |
135
|
|
|
* |
136
|
|
|
* @param string $key |
137
|
|
|
* |
138
|
|
|
* @return $this |
139
|
|
|
*/ |
140
|
|
|
public function remove($key) |
141
|
|
|
{ |
142
|
|
|
if ($this->has($key)) { |
143
|
|
|
unset($this->attributes[$key]); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritdoc} |
151
|
|
|
*/ |
152
|
|
|
public function getIterator() |
153
|
|
|
{ |
154
|
|
|
return new ArrayIterator($this->attributes); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* {@inheritdoc} |
159
|
|
|
*/ |
160
|
|
|
public function count() |
161
|
|
|
{ |
162
|
|
|
return count($this->attributes); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Set attribute name. |
167
|
|
|
* |
168
|
|
|
* @param string $name |
169
|
|
|
* |
170
|
|
|
* @return $this |
171
|
|
|
*/ |
172
|
|
|
public function setName($name) |
173
|
|
|
{ |
174
|
|
|
$this->name = $name; |
175
|
|
|
|
176
|
|
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Get session bag name. |
181
|
|
|
* |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
|
|
public function getName() |
185
|
|
|
{ |
186
|
|
|
return $this->name; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Identity storage. |
191
|
|
|
* |
192
|
|
|
* @return string |
193
|
|
|
*/ |
194
|
|
|
public function getStorageKey() |
195
|
|
|
{ |
196
|
|
|
return $this->storageKey; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Initialize sessions. |
201
|
|
|
* |
202
|
|
|
* @param array $sessions |
203
|
|
|
*/ |
204
|
|
|
public function initialize(array &$sessions) |
205
|
|
|
{ |
206
|
|
|
$this->attributes = &$sessions; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Clear session and return all data. |
211
|
|
|
* |
212
|
|
|
* @return array |
213
|
|
|
*/ |
214
|
|
|
public function clear() |
215
|
|
|
{ |
216
|
|
|
$attributes = $this->all(); |
217
|
|
|
$this->attributes = array(); |
218
|
|
|
|
219
|
|
|
return $attributes; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|