1 | <?php |
||
27 | class CookieCollection extends Object implements \IteratorAggregate, \ArrayAccess, \Countable |
||
28 | { |
||
29 | /** |
||
30 | * @var bool whether this collection is read only. |
||
31 | */ |
||
32 | public $readOnly = false; |
||
33 | |||
34 | /** |
||
35 | * @var Cookie[] the cookies in this collection (indexed by the cookie names) |
||
36 | */ |
||
37 | private $_cookies = []; |
||
38 | |||
39 | |||
40 | /** |
||
41 | * Constructor. |
||
42 | * @param array $cookies the cookies that this collection initially contains. This should be |
||
43 | * an array of name-value pairs. |
||
44 | * @param array $config name-value pairs that will be used to initialize the object properties |
||
45 | */ |
||
46 | 33 | public function __construct($cookies = [], $config = []) |
|
51 | |||
52 | /** |
||
53 | * Returns an iterator for traversing the cookies in the collection. |
||
54 | * This method is required by the SPL interface [[\IteratorAggregate]]. |
||
55 | * It will be implicitly called when you use `foreach` to traverse the collection. |
||
56 | * @return ArrayIterator an iterator for traversing the cookies in the collection. |
||
57 | */ |
||
58 | public function getIterator() |
||
62 | |||
63 | /** |
||
64 | * Returns the number of cookies in the collection. |
||
65 | * This method is required by the SPL `Countable` interface. |
||
66 | * It will be implicitly called when you use `count($collection)`. |
||
67 | * @return int the number of cookies in the collection. |
||
68 | */ |
||
69 | public function count() |
||
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() |
||
82 | |||
83 | /** |
||
84 | * Returns the cookie with the specified name. |
||
85 | * @param string $name the cookie name |
||
86 | * @return Cookie the cookie with the specified name. Null if the named cookie does not exist. |
||
87 | * @see getValue() |
||
88 | */ |
||
89 | public function get($name) |
||
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 | 32 | public function getValue($name, $defaultValue = null) |
|
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, 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 | 4 | && ($this->_cookies[$name]->expire === null || $this->_cookies[$name]->expire >= time()); |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * Adds a cookie to the collection. |
||
121 | * If there is already a cookie with the same name in the collection, it will be removed first. |
||
122 | * @param Cookie $cookie the cookie to be added |
||
123 | * @throws InvalidCallException if the cookie collection is read only |
||
124 | */ |
||
125 | 33 | public function add($cookie) |
|
132 | |||
133 | /** |
||
134 | * Removes a cookie. |
||
135 | * If `$removeFromBrowser` is true, the cookie will be removed from the browser. |
||
136 | * In this case, a cookie with outdated expiry will be added to the collection. |
||
137 | * @param Cookie|string $cookie the cookie object or the name of the cookie to be removed. |
||
138 | * @param bool $removeFromBrowser whether to remove the cookie from browser |
||
139 | * @throws InvalidCallException if the cookie collection is read only |
||
140 | */ |
||
141 | 1 | public function remove($cookie, $removeFromBrowser = true) |
|
161 | |||
162 | /** |
||
163 | * Removes all cookies. |
||
164 | * @throws InvalidCallException if the cookie collection is read only |
||
165 | */ |
||
166 | public function removeAll() |
||
173 | |||
174 | /** |
||
175 | * Returns the collection as a PHP array. |
||
176 | * @return array the array representation of the collection. |
||
177 | * The array keys are cookie names, and the array values are the corresponding cookie objects. |
||
178 | */ |
||
179 | 3 | public function toArray() |
|
180 | { |
||
181 | 3 | return $this->_cookies; |
|
182 | } |
||
183 | |||
184 | /** |
||
185 | * Populates the cookie collection from an array. |
||
186 | * @param array $array the cookies to populate from |
||
187 | * @since 2.0.3 |
||
188 | */ |
||
189 | 3 | public function fromArray(array $array) |
|
190 | { |
||
191 | 3 | $this->_cookies = $array; |
|
192 | 3 | } |
|
193 | |||
194 | /** |
||
195 | * Returns whether there is a cookie with the specified name. |
||
196 | * This method is required by the SPL interface [[\ArrayAccess]]. |
||
197 | * It is implicitly called when you use something like `isset($collection[$name])`. |
||
198 | * @param string $name the cookie name |
||
199 | * @return bool whether the named cookie exists |
||
200 | */ |
||
201 | public function offsetExists($name) |
||
205 | |||
206 | /** |
||
207 | * Returns the 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 `$cookie = $collection[$name];`. |
||
210 | * This is equivalent to [[get()]]. |
||
211 | * @param string $name the cookie name |
||
212 | * @return Cookie the cookie with the specified name, null if the named cookie does not exist. |
||
213 | */ |
||
214 | public function offsetGet($name) |
||
218 | |||
219 | /** |
||
220 | * Adds the cookie to the collection. |
||
221 | * This method is required by the SPL interface [[\ArrayAccess]]. |
||
222 | * It is implicitly called when you use something like `$collection[$name] = $cookie;`. |
||
223 | * This is equivalent to [[add()]]. |
||
224 | * @param string $name the cookie name |
||
225 | * @param Cookie $cookie the cookie to be added |
||
226 | */ |
||
227 | public function offsetSet($name, $cookie) |
||
231 | |||
232 | /** |
||
233 | * Removes the named cookie. |
||
234 | * This method is required by the SPL interface [[\ArrayAccess]]. |
||
235 | * It is implicitly called when you use something like `unset($collection[$name])`. |
||
236 | * This is equivalent to [[remove()]]. |
||
237 | * @param string $name the cookie name |
||
238 | */ |
||
239 | public function offsetUnset($name) |
||
243 | } |
||
244 |