1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dflydev\FigCookies; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use DateTimeInterface; |
7
|
|
|
|
8
|
|
|
class SetCookie |
9
|
|
|
{ |
10
|
|
|
private $name; |
11
|
|
|
private $value; |
12
|
|
|
private $expires = 0; |
13
|
|
|
private $maxAge = 0; |
14
|
|
|
private $path; |
15
|
|
|
private $domain; |
16
|
|
|
private $secure = false; |
17
|
|
|
private $httpOnly = false; |
18
|
|
|
|
19
|
31 |
|
private function __construct($name, $value = null) |
20
|
|
|
{ |
21
|
31 |
|
$this->name = $name; |
22
|
31 |
|
$this->value = $value; |
23
|
31 |
|
} |
24
|
|
|
|
25
|
19 |
|
public function getName() |
26
|
|
|
{ |
27
|
19 |
|
return $this->name; |
28
|
|
|
} |
29
|
|
|
|
30
|
3 |
|
public function getValue() |
31
|
|
|
{ |
32
|
3 |
|
return $this->value; |
33
|
|
|
} |
34
|
|
|
|
35
|
2 |
|
public function getExpires() |
36
|
|
|
{ |
37
|
2 |
|
return $this->expires; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getMaxAge() |
41
|
|
|
{ |
42
|
|
|
return $this->maxAge; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getPath() |
46
|
|
|
{ |
47
|
|
|
return $this->path; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getDomain() |
51
|
|
|
{ |
52
|
|
|
return $this->domain; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getSecure() |
56
|
|
|
{ |
57
|
|
|
return $this->secure; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getHttpOnly() |
61
|
|
|
{ |
62
|
|
|
return $this->httpOnly; |
63
|
|
|
} |
64
|
|
|
|
65
|
29 |
|
public function withValue($value = null) |
66
|
|
|
{ |
67
|
29 |
|
$clone = clone($this); |
68
|
|
|
|
69
|
29 |
|
$clone->value = $value; |
70
|
|
|
|
71
|
29 |
|
return $clone; |
72
|
|
|
} |
73
|
|
|
|
74
|
13 |
|
private function resolveExpires($expires = null) |
75
|
|
|
{ |
76
|
13 |
|
if (is_null($expires)) { |
77
|
|
|
return null; |
78
|
|
|
} |
79
|
|
|
|
80
|
13 |
|
if ($expires instanceof DateTime || $expires instanceof DateTimeInterface) { |
81
|
2 |
|
return $expires->getTimestamp(); |
82
|
|
|
} |
83
|
|
|
|
84
|
11 |
|
if (is_numeric($expires)) { |
85
|
|
|
return $expires; |
86
|
|
|
} |
87
|
|
|
|
88
|
11 |
|
return strtotime($expires); |
89
|
|
|
} |
90
|
|
|
|
91
|
13 |
|
public function withExpires($expires = null) |
92
|
|
|
{ |
93
|
13 |
|
$expires = $this->resolveExpires($expires); |
94
|
|
|
|
95
|
13 |
|
$clone = clone($this); |
96
|
|
|
|
97
|
13 |
|
$clone->expires = $expires; |
98
|
|
|
|
99
|
13 |
|
return $clone; |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
public function rememberForever() |
103
|
|
|
{ |
104
|
1 |
|
return $this->withExpires(new DateTime('+5 years')); |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
public function expire() |
108
|
|
|
{ |
109
|
1 |
|
return $this->withExpires(new DateTime('-5 years')); |
110
|
|
|
} |
111
|
|
|
|
112
|
4 |
|
public function withMaxAge($maxAge = null) |
113
|
|
|
{ |
114
|
4 |
|
$clone = clone($this); |
115
|
|
|
|
116
|
4 |
|
$clone->maxAge = $maxAge; |
117
|
|
|
|
118
|
4 |
|
return $clone; |
119
|
|
|
} |
120
|
|
|
|
121
|
12 |
|
public function withPath($path = null) |
122
|
|
|
{ |
123
|
12 |
|
$clone = clone($this); |
124
|
|
|
|
125
|
12 |
|
$clone->path = $path; |
126
|
|
|
|
127
|
12 |
|
return $clone; |
128
|
|
|
} |
129
|
|
|
|
130
|
7 |
|
public function withDomain($domain = null) |
131
|
|
|
{ |
132
|
7 |
|
$clone = clone($this); |
133
|
|
|
|
134
|
7 |
|
$clone->domain = $domain; |
135
|
|
|
|
136
|
7 |
|
return $clone; |
137
|
|
|
} |
138
|
|
|
|
139
|
10 |
|
public function withSecure($secure = null) |
140
|
|
|
{ |
141
|
10 |
|
$clone = clone($this); |
142
|
|
|
|
143
|
10 |
|
$clone->secure = $secure; |
144
|
|
|
|
145
|
10 |
|
return $clone; |
146
|
|
|
} |
147
|
|
|
|
148
|
12 |
|
public function withHttpOnly($httpOnly = null) |
149
|
|
|
{ |
150
|
12 |
|
$clone = clone($this); |
151
|
|
|
|
152
|
12 |
|
$clone->httpOnly = $httpOnly; |
153
|
|
|
|
154
|
12 |
|
return $clone; |
155
|
|
|
} |
156
|
|
|
|
157
|
17 |
|
public function __toString() |
158
|
|
|
{ |
159
|
|
|
$cookieStringParts = [ |
160
|
17 |
|
urlencode($this->name).'='.urlencode($this->value), |
161
|
17 |
|
]; |
162
|
|
|
|
163
|
17 |
|
$cookieStringParts = $this->appendFormattedDomainPartIfSet($cookieStringParts); |
164
|
17 |
|
$cookieStringParts = $this->appendFormattedPathPartIfSet($cookieStringParts); |
165
|
17 |
|
$cookieStringParts = $this->appendFormattedExpiresPartIfSet($cookieStringParts); |
166
|
17 |
|
$cookieStringParts = $this->appendFormattedMaxAgePartIfSet($cookieStringParts); |
167
|
17 |
|
$cookieStringParts = $this->appendFormattedSecurePartIfSet($cookieStringParts); |
168
|
17 |
|
$cookieStringParts = $this->appendFormattedHttpOnlyPartIfSet($cookieStringParts); |
169
|
|
|
|
170
|
17 |
|
return implode('; ', $cookieStringParts); |
171
|
|
|
} |
172
|
|
|
|
173
|
8 |
|
public static function create($name, $value = null) |
174
|
|
|
{ |
175
|
8 |
|
return new static($name, $value); |
176
|
|
|
} |
177
|
|
|
|
178
|
1 |
|
public static function createRememberedForever($name, $value = null) |
179
|
|
|
{ |
180
|
1 |
|
return static::create($name, $value)->rememberForever(); |
181
|
|
|
} |
182
|
|
|
|
183
|
1 |
|
public static function createExpired($name) |
184
|
|
|
{ |
185
|
1 |
|
return static::create($name)->expire(); |
186
|
|
|
} |
187
|
|
|
|
188
|
29 |
|
public static function fromSetCookieString($string) |
189
|
|
|
{ |
190
|
29 |
|
$rawAttributes = StringUtil::splitOnAttributeDelimiter($string); |
191
|
|
|
|
192
|
29 |
|
list ($cookieName, $cookieValue) = StringUtil::splitCookiePair(array_shift($rawAttributes)); |
193
|
|
|
|
194
|
|
|
/** @var SetCookie $setCookie */ |
195
|
29 |
|
$setCookie = new static($cookieName); |
196
|
|
|
|
197
|
29 |
|
if (! is_null($cookieValue)) { |
198
|
29 |
|
$setCookie = $setCookie->withValue($cookieValue); |
199
|
29 |
|
} |
200
|
|
|
|
201
|
29 |
|
while ($rawAttribute = array_shift($rawAttributes)) { |
202
|
12 |
|
$rawAttributePair = explode('=', $rawAttribute, 2); |
203
|
|
|
|
204
|
12 |
|
$attributeKey = $rawAttributePair[0]; |
205
|
12 |
|
$attributeValue = count($rawAttributePair) > 1 ? $rawAttributePair[1] : null; |
206
|
|
|
|
207
|
12 |
|
$attributeKey = strtolower($attributeKey); |
208
|
|
|
|
209
|
|
|
switch ($attributeKey) { |
210
|
12 |
|
case 'expires': |
211
|
11 |
|
$setCookie = $setCookie->withExpires($attributeValue); |
212
|
11 |
|
break; |
213
|
12 |
|
case 'max-age': |
214
|
4 |
|
$setCookie = $setCookie->withMaxAge($attributeValue); |
215
|
4 |
|
break; |
216
|
12 |
|
case 'domain': |
217
|
7 |
|
$setCookie = $setCookie->withDomain($attributeValue); |
218
|
7 |
|
break; |
219
|
12 |
|
case 'path': |
220
|
12 |
|
$setCookie = $setCookie->withPath($attributeValue); |
221
|
12 |
|
break; |
222
|
12 |
|
case 'secure': |
223
|
10 |
|
$setCookie = $setCookie->withSecure(true); |
224
|
10 |
|
break; |
225
|
12 |
|
case 'httponly': |
226
|
12 |
|
$setCookie = $setCookie->withHttpOnly(true); |
227
|
12 |
|
break; |
228
|
|
|
} |
229
|
|
|
|
230
|
12 |
|
} |
231
|
|
|
|
232
|
29 |
|
return $setCookie; |
233
|
|
|
} |
234
|
17 |
|
private function appendFormattedDomainPartIfSet(array $cookieStringParts) |
235
|
|
|
{ |
236
|
17 |
|
if ($this->domain) { |
237
|
7 |
|
$cookieStringParts[] = sprintf("Domain=%s", $this->domain); |
238
|
7 |
|
} |
239
|
|
|
|
240
|
17 |
|
return $cookieStringParts; |
241
|
|
|
} |
242
|
|
|
|
243
|
17 |
|
private function appendFormattedPathPartIfSet(array $cookieStringParts) |
244
|
|
|
{ |
245
|
17 |
|
if ($this->path) { |
246
|
8 |
|
$cookieStringParts[] = sprintf("Path=%s", $this->path); |
247
|
8 |
|
} |
248
|
|
|
|
249
|
17 |
|
return $cookieStringParts; |
250
|
|
|
} |
251
|
|
|
|
252
|
17 |
|
private function appendFormattedExpiresPartIfSet(array $cookieStringParts) |
253
|
|
|
{ |
254
|
17 |
|
if ($this->expires) { |
255
|
7 |
|
$cookieStringParts[] = sprintf("Expires=%s", gmdate('D, d M Y H:i:s T', $this->expires)); |
256
|
7 |
|
} |
257
|
|
|
|
258
|
17 |
|
return $cookieStringParts; |
259
|
|
|
} |
260
|
|
|
|
261
|
17 |
|
private function appendFormattedMaxAgePartIfSet(array $cookieStringParts) |
262
|
|
|
{ |
263
|
17 |
|
if ($this->maxAge) { |
264
|
4 |
|
$cookieStringParts[] = sprintf("Max-Age=%s", $this->maxAge); |
265
|
4 |
|
} |
266
|
|
|
|
267
|
17 |
|
return $cookieStringParts; |
268
|
|
|
} |
269
|
|
|
|
270
|
17 |
|
private function appendFormattedSecurePartIfSet(array $cookieStringParts) |
271
|
|
|
{ |
272
|
17 |
|
if ($this->secure) { |
273
|
6 |
|
$cookieStringParts[] = 'Secure'; |
274
|
6 |
|
} |
275
|
|
|
|
276
|
17 |
|
return $cookieStringParts; |
277
|
|
|
} |
278
|
|
|
|
279
|
17 |
|
private function appendFormattedHttpOnlyPartIfSet(array $cookieStringParts) |
280
|
|
|
{ |
281
|
17 |
|
if ($this->httpOnly) { |
282
|
8 |
|
$cookieStringParts[] = 'HttpOnly'; |
283
|
8 |
|
} |
284
|
|
|
|
285
|
17 |
|
return $cookieStringParts; |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|