forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCookieStore.php
More file actions
214 lines (182 loc) · 5.13 KB
/
CookieStore.php
File metadata and controls
214 lines (182 loc) · 5.13 KB
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
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Cookie;
use CodeIgniter\Cookie\Exceptions\CookieException;
use Countable;
use IteratorAggregate;
use Traversable;
/**
* The CookieStore object represents an immutable collection of `Cookie` value objects.
*
* @implements IteratorAggregate<string, Cookie>
* @see \CodeIgniter\Cookie\CookieStoreTest
*/
class CookieStore implements Countable, IteratorAggregate
{
/**
* The cookie collection.
*
* @var array<string, Cookie>
*/
protected $cookies = [];
/**
* Creates a CookieStore from an array of `Set-Cookie` headers.
*
* @param list<string> $headers
*
* @return static
*
* @throws CookieException
*/
public static function fromCookieHeaders(array $headers, bool $raw = false)
{
/**
* @var list<Cookie> $cookies
*/
$cookies = array_filter(array_map(static function (string $header) use ($raw) {
try {
return Cookie::fromHeaderString($header, $raw);
} catch (CookieException $e) {
log_message('error', (string) $e);
return false;
}
}, $headers));
return new static($cookies);
}
/**
* @param array<array-key, Cookie> $cookies
*
* @throws CookieException
*/
final public function __construct(array $cookies)
{
$this->validateCookies($cookies);
foreach ($cookies as $cookie) {
$this->cookies[$cookie->getId()] = $cookie;
}
}
/**
* Checks if a `Cookie` object identified by name and
* prefix is present in the collection.
*/
public function has(string $name, string $prefix = '', ?string $value = null): bool
{
$name = $prefix . $name;
foreach ($this->cookies as $cookie) {
if ($cookie->getPrefixedName() !== $name) {
continue;
}
if ($value === null) {
return true; // for BC
}
return $cookie->getValue() === $value;
}
return false;
}
/**
* Retrieves an instance of `Cookie` identified by a name and prefix.
* This throws an exception if not found.
*
* @throws CookieException
*/
public function get(string $name, string $prefix = ''): Cookie
{
$name = $prefix . $name;
foreach ($this->cookies as $cookie) {
if ($cookie->getPrefixedName() === $name) {
return $cookie;
}
}
throw CookieException::forUnknownCookieInstance([$name, $prefix]);
}
/**
* Store a new cookie and return a new collection. The original collection
* is left unchanged.
*
* @return static
*/
public function put(Cookie $cookie)
{
$store = clone $this;
$store->cookies[$cookie->getId()] = $cookie;
return $store;
}
/**
* Removes a cookie from a collection and returns an updated collection.
* The original collection is left unchanged.
*
* Removing a cookie from the store **DOES NOT** delete it from the browser.
* If you intend to delete a cookie *from the browser*, you must put an empty
* value cookie with the same name to the store.
*
* @return static
*/
public function remove(string $name, string $prefix = '')
{
$default = Cookie::setDefaults();
$id = implode(';', [$prefix . $name, $default['path'], $default['domain']]);
$store = clone $this;
foreach (array_keys($store->cookies) as $index) {
if ($index === $id) {
unset($store->cookies[$index]);
}
}
return $store;
}
/**
* Returns all cookie instances in store.
*
* @return array<string, Cookie>
*/
public function display(): array
{
return $this->cookies;
}
/**
* Clears the cookie collection.
*/
public function clear(): void
{
$this->cookies = [];
}
/**
* Gets the Cookie count in this collection.
*/
public function count(): int
{
return count($this->cookies);
}
/**
* Gets the iterator for the cookie collection.
*
* @return Traversable<string, Cookie>
*/
public function getIterator(): Traversable
{
foreach ($this->cookies as $key => $value) {
yield $key => $value;
}
}
/**
* Validates all cookies passed to be instances of Cookie.
*
* @throws CookieException
*/
protected function validateCookies(array $cookies): void
{
foreach ($cookies as $index => $cookie) {
$type = get_debug_type($cookie);
if (! $cookie instanceof Cookie) {
throw CookieException::forInvalidCookieInstance([static::class, Cookie::class, $type, $index]);
}
}
}
}