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