|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link https://www.yiiframework.com/ |
|
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
|
5
|
|
|
* @license https://www.yiiframework.com/license/ |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace yii\web; |
|
9
|
|
|
|
|
10
|
|
|
use ArrayIterator; |
|
11
|
|
|
use Yii; |
|
12
|
|
|
use yii\base\BaseObject; |
|
13
|
|
|
use yii\base\InvalidCallException; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* CookieCollection maintains the cookies available in the current request. |
|
17
|
|
|
* |
|
18
|
|
|
* For more details and usage information on CookieCollection, see the [guide article on handling cookies](guide:runtime-sessions-cookies). |
|
19
|
|
|
* |
|
20
|
|
|
* @property-read int $count The number of cookies in the collection. |
|
21
|
|
|
* |
|
22
|
|
|
* @author Qiang Xue <[email protected]> |
|
23
|
|
|
* @since 2.0 |
|
24
|
|
|
*/ |
|
25
|
|
|
class CookieCollection extends BaseObject implements \IteratorAggregate, \ArrayAccess, \Countable |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var bool whether this collection is read only. |
|
29
|
|
|
*/ |
|
30
|
|
|
public $readOnly = false; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var Cookie[] the cookies in this collection (indexed by the cookie names) |
|
34
|
|
|
*/ |
|
35
|
|
|
private $_cookies; |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Constructor. |
|
40
|
|
|
* @param array $cookies the cookies that this collection initially contains. This should be |
|
41
|
|
|
* an array of name-value pairs. |
|
42
|
|
|
* @param array $config name-value pairs that will be used to initialize the object properties |
|
43
|
|
|
*/ |
|
44
|
93 |
|
public function __construct($cookies = [], $config = []) |
|
45
|
|
|
{ |
|
46
|
93 |
|
$this->_cookies = $cookies; |
|
47
|
93 |
|
parent::__construct($config); |
|
48
|
93 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Returns an iterator for traversing the cookies in the collection. |
|
52
|
|
|
* This method is required by the SPL interface [[\IteratorAggregate]]. |
|
53
|
|
|
* It will be implicitly called when you use `foreach` to traverse the collection. |
|
54
|
|
|
* @return ArrayIterator<string, Cookie> an iterator for traversing the cookies in the collection. |
|
55
|
|
|
*/ |
|
56
|
|
|
#[\ReturnTypeWillChange] |
|
57
|
9 |
|
public function getIterator() |
|
58
|
|
|
{ |
|
59
|
9 |
|
return new ArrayIterator($this->_cookies); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Returns the number of cookies in the collection. |
|
64
|
|
|
* This method is required by the SPL `Countable` interface. |
|
65
|
|
|
* It will be implicitly called when you use `count($collection)`. |
|
66
|
|
|
* @return int the number of cookies in the collection. |
|
67
|
|
|
*/ |
|
68
|
|
|
#[\ReturnTypeWillChange] |
|
69
|
|
|
public function count() |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->getCount(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Returns the number of cookies in the collection. |
|
76
|
|
|
* @return int the number of cookies in the collection. |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getCount() |
|
79
|
|
|
{ |
|
80
|
|
|
return count($this->_cookies); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Returns the cookie with the specified name. |
|
85
|
|
|
* @param string $name the cookie name |
|
86
|
|
|
* @return Cookie|null the cookie with the specified name. Null if the named cookie does not exist. |
|
87
|
|
|
* @see getValue() |
|
88
|
|
|
*/ |
|
89
|
|
|
public function get($name) |
|
90
|
|
|
{ |
|
91
|
|
|
return isset($this->_cookies[$name]) ? $this->_cookies[$name] : null; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Returns the value of the named cookie. |
|
96
|
|
|
* @param string $name the cookie name |
|
97
|
|
|
* @param mixed $defaultValue the value that should be returned when the named cookie does not exist. |
|
98
|
|
|
* @return mixed the value of the named cookie. |
|
99
|
|
|
* @see get() |
|
100
|
|
|
*/ |
|
101
|
87 |
|
public function getValue($name, $defaultValue = null) |
|
102
|
|
|
{ |
|
103
|
87 |
|
return isset($this->_cookies[$name]) ? $this->_cookies[$name]->value : $defaultValue; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Returns whether there is a cookie with the specified name. |
|
108
|
|
|
* Note that if a cookie is marked for deletion from browser or its value is an empty string, this method will return false. |
|
109
|
|
|
* @param string $name the cookie name |
|
110
|
|
|
* @return bool whether the named cookie exists |
|
111
|
|
|
* @see remove() |
|
112
|
|
|
*/ |
|
113
|
4 |
|
public function has($name) |
|
114
|
|
|
{ |
|
115
|
4 |
|
return isset($this->_cookies[$name]) && $this->_cookies[$name]->value !== '' |
|
116
|
3 |
|
&& ($this->_cookies[$name]->expire === null |
|
117
|
3 |
|
|| $this->_cookies[$name]->expire === 0 |
|
118
|
|
|
|| ( |
|
119
|
3 |
|
(is_string($this->_cookies[$name]->expire) && strtotime($this->_cookies[$name]->expire) >= time()) |
|
|
|
|
|
|
120
|
|
|
|| ( |
|
121
|
3 |
|
interface_exists('\\DateTimeInterface') |
|
122
|
3 |
|
&& $this->_cookies[$name]->expire instanceof \DateTimeInterface |
|
123
|
3 |
|
&& $this->_cookies[$name]->expire->getTimestamp() >= time() |
|
|
|
|
|
|
124
|
|
|
) |
|
125
|
4 |
|
|| $this->_cookies[$name]->expire >= time() |
|
126
|
|
|
) |
|
127
|
|
|
); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Adds a cookie to the collection. |
|
132
|
|
|
* If there is already a cookie with the same name in the collection, it will be removed first. |
|
133
|
|
|
* @param Cookie $cookie the cookie to be added |
|
134
|
|
|
* @throws InvalidCallException if the cookie collection is read only |
|
135
|
|
|
*/ |
|
136
|
93 |
|
public function add($cookie) |
|
137
|
|
|
{ |
|
138
|
93 |
|
if ($this->readOnly) { |
|
139
|
|
|
throw new InvalidCallException('The cookie collection is read only.'); |
|
140
|
|
|
} |
|
141
|
93 |
|
$this->_cookies[$cookie->name] = $cookie; |
|
142
|
93 |
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Removes a cookie. |
|
146
|
|
|
* If `$removeFromBrowser` is true, the cookie will be removed from the browser. |
|
147
|
|
|
* In this case, a cookie with outdated expiry will be added to the collection. |
|
148
|
|
|
* @param Cookie|string $cookie the cookie object or the name of the cookie to be removed. |
|
149
|
|
|
* @param bool $removeFromBrowser whether to remove the cookie from browser |
|
150
|
|
|
* @throws InvalidCallException if the cookie collection is read only |
|
151
|
|
|
*/ |
|
152
|
2 |
|
public function remove($cookie, $removeFromBrowser = true) |
|
153
|
|
|
{ |
|
154
|
2 |
|
if ($this->readOnly) { |
|
155
|
|
|
throw new InvalidCallException('The cookie collection is read only.'); |
|
156
|
|
|
} |
|
157
|
2 |
|
if ($cookie instanceof Cookie) { |
|
158
|
2 |
|
$cookie->expire = 1; |
|
159
|
2 |
|
$cookie->value = ''; |
|
160
|
|
|
} else { |
|
161
|
|
|
$cookie = Yii::createObject([ |
|
162
|
|
|
'class' => 'yii\web\Cookie', |
|
163
|
|
|
'name' => $cookie, |
|
164
|
|
|
'expire' => 1, |
|
165
|
|
|
]); |
|
166
|
|
|
} |
|
167
|
2 |
|
if ($removeFromBrowser) { |
|
168
|
2 |
|
$this->_cookies[$cookie->name] = $cookie; |
|
169
|
|
|
} else { |
|
170
|
|
|
unset($this->_cookies[$cookie->name]); |
|
171
|
|
|
} |
|
172
|
2 |
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Removes all cookies. |
|
176
|
|
|
* @throws InvalidCallException if the cookie collection is read only |
|
177
|
|
|
*/ |
|
178
|
|
|
public function removeAll() |
|
179
|
|
|
{ |
|
180
|
|
|
if ($this->readOnly) { |
|
181
|
|
|
throw new InvalidCallException('The cookie collection is read only.'); |
|
182
|
|
|
} |
|
183
|
|
|
$this->_cookies = []; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Returns the collection as a PHP array. |
|
188
|
|
|
* @return Cookie[] the array representation of the collection. |
|
189
|
|
|
* The array keys are cookie names, and the array values are the corresponding cookie objects. |
|
190
|
|
|
*/ |
|
191
|
3 |
|
public function toArray() |
|
192
|
|
|
{ |
|
193
|
3 |
|
return $this->_cookies; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Populates the cookie collection from an array. |
|
198
|
|
|
* @param array $array the cookies to populate from |
|
199
|
|
|
* @since 2.0.3 |
|
200
|
|
|
*/ |
|
201
|
3 |
|
public function fromArray(array $array) |
|
202
|
|
|
{ |
|
203
|
3 |
|
$this->_cookies = $array; |
|
204
|
3 |
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Returns whether there is a cookie with the specified name. |
|
208
|
|
|
* This method is required by the SPL interface [[\ArrayAccess]]. |
|
209
|
|
|
* It is implicitly called when you use something like `isset($collection[$name])`. |
|
210
|
|
|
* @param string $name the cookie name |
|
211
|
|
|
* @return bool whether the named cookie exists |
|
212
|
|
|
*/ |
|
213
|
|
|
#[\ReturnTypeWillChange] |
|
214
|
|
|
public function offsetExists($name) |
|
215
|
|
|
{ |
|
216
|
|
|
return $this->has($name); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Returns the cookie with the specified name. |
|
221
|
|
|
* This method is required by the SPL interface [[\ArrayAccess]]. |
|
222
|
|
|
* It is implicitly called when you use something like `$cookie = $collection[$name];`. |
|
223
|
|
|
* This is equivalent to [[get()]]. |
|
224
|
|
|
* @param string $name the cookie name |
|
225
|
|
|
* @return Cookie|null the cookie with the specified name, null if the named cookie does not exist. |
|
226
|
|
|
*/ |
|
227
|
|
|
#[\ReturnTypeWillChange] |
|
228
|
|
|
public function offsetGet($name) |
|
229
|
|
|
{ |
|
230
|
|
|
return $this->get($name); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Adds the cookie to the collection. |
|
235
|
|
|
* This method is required by the SPL interface [[\ArrayAccess]]. |
|
236
|
|
|
* It is implicitly called when you use something like `$collection[$name] = $cookie;`. |
|
237
|
|
|
* This is equivalent to [[add()]]. |
|
238
|
|
|
* @param string $name the cookie name |
|
239
|
|
|
* @param Cookie $cookie the cookie to be added |
|
240
|
|
|
*/ |
|
241
|
|
|
#[\ReturnTypeWillChange] |
|
242
|
|
|
public function offsetSet($name, $cookie) |
|
243
|
|
|
{ |
|
244
|
|
|
$this->add($cookie); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Removes the named cookie. |
|
249
|
|
|
* This method is required by the SPL interface [[\ArrayAccess]]. |
|
250
|
|
|
* It is implicitly called when you use something like `unset($collection[$name])`. |
|
251
|
|
|
* This is equivalent to [[remove()]]. |
|
252
|
|
|
* @param string $name the cookie name |
|
253
|
|
|
*/ |
|
254
|
|
|
#[\ReturnTypeWillChange] |
|
255
|
|
|
public function offsetUnset($name) |
|
256
|
|
|
{ |
|
257
|
|
|
$this->remove($name); |
|
258
|
|
|
} |
|
259
|
|
|
} |
|
260
|
|
|
|