This repository has been archived by the owner on Jun 29, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
CookieCollection.php
364 lines (325 loc) · 10.2 KB
/
CookieCollection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?php
declare(strict_types=1);
namespace Yiisoft\Yii\Web;
use ArrayAccess;
use ArrayIterator;
use Closure;
use Countable;
use Exception;
use InvalidArgumentException;
use IteratorAggregate;
use Psr\Http\Message\ResponseInterface;
use Yiisoft\Http\Header;
use function array_keys;
use function array_values;
use function array_walk;
use function count;
use function in_array;
/**
* A CookieCollection helps to work with many cookies at once and to read / modify response cookies.
*
* @see Cookie
*/
final class CookieCollection implements IteratorAggregate, ArrayAccess, Countable
{
/**
* @var Cookie[] the cookies in this collection (indexed by the cookie name)
*/
private array $cookies = [];
/**
* CookieCollection constructor.
*
* @param Cookie[] $cookies the cookies that this collection initially contains.
*/
public function __construct(array $cookies = [])
{
foreach ($cookies as $cookie) {
if (!($cookie instanceof Cookie)) {
throw new InvalidArgumentException('CookieCollection can contain only Cookie instances.');
}
$this->cookies[$cookie->getName()] = $cookie;
}
}
/**
* Returns the collection as a PHP array.
* The array keys are cookie names, and the array values are the corresponding cookie objects.
*
* @return Cookie[]
*/
public function toArray(): array
{
return $this->cookies;
}
/**
* Returns an iterator for traversing the cookies in the collection.
* This method is required by the SPL interface [[\IteratorAggregate]].
* It will be implicitly called when you use `foreach` to traverse the collection.
*
* @return ArrayIterator
*/
public function getIterator(): ArrayIterator
{
return new ArrayIterator($this->cookies);
}
/**
* Returns whether there is a cookie with the specified name.
* This method is required by the SPL interface [[\ArrayAccess]].
* It is implicitly called when you use something like `isset($collection[$name])`.
* This is equivalent to [[has()]].
*
* @param string $name the cookie name
* @return bool whether the named cookie exists
*/
public function offsetExists($name): bool
{
return $this->has($name);
}
/**
* Returns the cookie with the specified name.
* This method is required by the SPL interface [[\ArrayAccess]].
* It is implicitly called when you use something like `$cookie = $collection[$name];`.
* This is equivalent to [[get()]].
*
* @param string $name the cookie name
* @return Cookie the cookie with the specified name, null if the named cookie does not exist.
*/
public function offsetGet($name): Cookie
{
return $this->get($name);
}
/**
* Adds the cookie to the collection.
* This method is required by the SPL interface [[\ArrayAccess]].
* It is implicitly called when you use something like `$collection[$name] = $cookie;`.
* This is equivalent to [[add()]].
*
* @param string $name the cookie name
* @param Cookie $cookie the cookie to be added
*/
public function offsetSet($name, $cookie): void
{
$this->add($cookie);
}
/**
* Removes the named cookie.
* This method is required by the SPL interface [[\ArrayAccess]].
* It is implicitly called when you use something like `unset($collection[$name])`.
* This is equivalent to [[remove()]].
*
* @param string $name the cookie name
*/
public function offsetUnset($name): void
{
$this->remove($name);
}
/**
* Returns the number of cookies in the collection.
* This method is required by the SPL `Countable` interface.
* It will be implicitly called when you use `count($collection)`.
*
* @return int the number of cookies in the collection.
*/
public function count(): int
{
return count($this->cookies);
}
/**
* Returns the cookie with the specified name.
*
* @param string $name the cookie name
* @return Cookie|null the cookie with the specified name. Null if the named cookie does not exist.
* @see getValue()
*/
public function get(string $name): ?Cookie
{
return $this->cookies[$name] ?? null;
}
/**
* Returns the value of the named cookie.
*
* @param string $name the cookie name
* @param mixed $defaultValue the value that should be returned when the named cookie does not exist.
* @return string|null the value of the named cookie or the default value if cookie is not set.
* @see get()
*/
public function getValue(string $name, $defaultValue = null): ?string
{
return isset($this->cookies[$name]) ? $this->cookies[$name]->getValue() : $defaultValue;
}
/**
* Adds a cookie to the collection.
* If there is already a cookie with the same name in the collection, it will be removed first.
*
* @param Cookie $cookie the cookie to be added
*/
public function add(Cookie $cookie): void
{
$this->cookies[$cookie->getName()] = $cookie;
}
/**
* Returns whether there is a cookie with the specified name.
*
* @param string $name the cookie name
* @return bool whether the named cookie exists
* @see remove()
*/
public function has(string $name): bool
{
return isset($this->cookies[$name]);
}
/**
* Removes a cookie.
*
* @param string $name the name of the cookie to be removed.
* @return Cookie|null cookie that is removed
*/
public function remove(string $name): ?Cookie
{
if (!isset($this->cookies[$name])) {
return null;
}
$removed = $this->cookies[$name];
unset($this->cookies[$name]);
return $removed;
}
/**
* Removes all cookies.
*/
public function clear(): void
{
$this->cookies = [];
}
/**
* Returns whether the collection already contains the cookie.
*
* @param Cookie $cookie the cookie to check for
* @return bool whether cookie exists
* @see has()
*/
public function contains(Cookie $cookie): bool
{
return in_array($cookie, $this->cookies, true);
}
/**
* Tests for the existence of the cookie that satisfies the given predicate.
*
* @param Closure $p The predicate.
* @return bool whether the predicate is true for at least on cookie.
*/
public function exists(Closure $p): bool
{
foreach ($this->cookies as $name => $cookie) {
if ($p($cookie, $name)) {
return true;
}
}
return false;
}
/**
* Expire the cookie with the specified name
*
* @param string $name the cookie name
*/
public function expire(string $name): void
{
if (!isset($this->cookies[$name])) {
return;
}
$this->cookies[$name] = $this->cookies[$name]->expire();
}
/**
* Apply user supplied function to every cookie in the collection.
* If you want to modify the cookie in the collection, specify the first
* parameter of Closure as reference.
*
* @param Closure $p
*/
public function walk(Closure $p): void
{
array_walk($this->cookies, $p);
}
/**
* Gets all keys/indices of the collection.
*
* @return string[] The keys/indices of the collection.
*/
public function getKeys(): array
{
return array_keys($this->cookies);
}
/**
* Gets all cookies of the collection as an indexed array.
*
* @return Cookie[] The cookies in the collection, in the order they appear in the collection.
*/
public function getValues(): array
{
return array_values($this->cookies);
}
/**
* Checks whether the collection is empty (contains no cookies).
*
* @return bool whether the collection is empty.
*/
public function isEmpty(): bool
{
return empty($this->cookies);
}
/**
* Populates the cookie collection from an array of 'name' => 'value' pairs.
*
* @param array $array the cookies to populate from
* @return static collection created from array
*/
public static function fromArray(array $array): self
{
if (empty($array)) {
return new self();
}
// check if associative array with 'name' => 'value' pairs is passed
if (count(array_filter(array_keys($array), 'is_string')) !== count($array)) {
throw new InvalidArgumentException('Array in wrong format is passed.');
}
return new self(array_map(static fn ($name, $value) => new Cookie($name, $value), array_keys($array), $array));
}
/**
* Adds the cookies in the collection to response and returns it.
*
* @param ResponseInterface $response
* @return ResponseInterface response with added cookies.
*/
public function addToResponse(ResponseInterface $response): ResponseInterface
{
foreach ($this->cookies as $cookie) {
$response = $cookie->addToResponse($response);
}
return $response;
}
/**
* Creates a copy of the response with cookies set from the collection.
*
* @param ResponseInterface $response
* @return ResponseInterface response with new cookies.
*/
public function setToResponse(ResponseInterface $response): ResponseInterface
{
$response = $response->withoutHeader(Header::SET_COOKIE);
return $this->addToResponse($response);
}
/**
* Populates the cookie collection from a ResponseInterface.
*
* @param ResponseInterface $response the response object to populate from
* @return static collection created from response
* @throws Exception
*/
public static function fromResponse(ResponseInterface $response): self
{
$collection = new self();
foreach ($response->getHeader(Header::SET_COOKIE) as $setCookieString) {
$cookie = Cookie::fromCookieString($setCookieString);
$collection->add($cookie);
}
return $collection;
}
}