1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace Venta\Http; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use DateTimeImmutable; |
7
|
|
|
use DateTimeInterface; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use Venta\Contracts\Http\Cookie as CookieContract; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Cookie |
13
|
|
|
* |
14
|
|
|
* @package Venta\Http |
15
|
|
|
*/ |
16
|
|
|
final class Cookie implements CookieContract |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $domain = ''; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var DateTimeInterface|null |
26
|
|
|
*/ |
27
|
|
|
private $expiration = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var bool |
31
|
|
|
*/ |
32
|
|
|
private $httpOnly = false; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $name; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $path = ''; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var bool |
46
|
|
|
*/ |
47
|
|
|
private $secure = false; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
private $value = ''; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Cookie constructor. |
56
|
|
|
* |
57
|
|
|
* @param string $name |
58
|
|
|
* @param string $value |
59
|
|
|
* @param DateTimeInterface|null $expiration |
60
|
|
|
* @param string $path |
61
|
|
|
* @param string $domain |
62
|
|
|
* @param bool $secure |
63
|
|
|
* @param bool $httpOnly |
64
|
|
|
* @throws InvalidArgumentException |
65
|
|
|
*/ |
66
|
11 |
|
public function __construct( |
67
|
|
|
string $name, |
68
|
|
|
string $value = '', |
69
|
|
|
DateTimeInterface $expiration = null, |
70
|
|
|
string $path = '', |
71
|
|
|
string $domain = '', |
72
|
|
|
bool $secure = false, |
73
|
|
|
bool $httpOnly = false |
74
|
|
|
) { |
75
|
11 |
|
if (empty($name)) { |
76
|
1 |
|
throw new InvalidArgumentException('The cookie name cannot be empty.'); |
77
|
|
|
} |
78
|
|
|
|
79
|
10 |
|
if (preg_match("/[=,; \t\r\n\013\014]/", $name)) { |
80
|
1 |
|
throw new InvalidArgumentException(sprintf('The cookie name "%s" contains invalid characters.', $name)); |
81
|
|
|
} |
82
|
|
|
|
83
|
9 |
|
$this->name = $name; |
84
|
9 |
|
$this->value = $value; |
85
|
9 |
|
$this->domain = $domain; |
86
|
9 |
|
$this->expiration = $expiration ? $this->freezeExpiration($expiration) : null; |
87
|
9 |
|
$this->path = $path; |
88
|
9 |
|
$this->secure = $secure; |
89
|
9 |
|
$this->httpOnly = $httpOnly; |
90
|
9 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @inheritDoc |
94
|
|
|
*/ |
95
|
3 |
|
public function __toString() |
96
|
|
|
{ |
97
|
3 |
|
$attributes = []; |
98
|
3 |
|
$name = urlencode($this->name); |
99
|
3 |
|
if ((string)$this->value === '') { |
100
|
|
|
// Cookie with empty value assumed expired. |
101
|
1 |
|
$value = 'deleted'; |
102
|
1 |
|
$attributes[] = 'expires=' . (new DateTime())->setTimestamp(1)->format(DateTime::COOKIE); |
103
|
|
|
} else { |
104
|
2 |
|
$value = urlencode($this->value); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// Create cookie crumb with name and value. |
108
|
3 |
|
$cookieCrumb = "$name=$value"; |
109
|
|
|
|
110
|
|
|
// Add cookie attributes. |
111
|
3 |
|
if ($this->expiration) { |
112
|
1 |
|
$attributes[] = 'expires=' . $this->expiration->format(DateTime::COOKIE); |
113
|
|
|
} |
114
|
3 |
|
if ($this->path) { |
115
|
1 |
|
$attributes[] = "path={$this->path}"; |
116
|
|
|
} |
117
|
3 |
|
if ($this->domain) { |
118
|
1 |
|
$attributes[] = "domain={$this->domain}"; |
119
|
|
|
} |
120
|
3 |
|
if ($this->secure) { |
121
|
1 |
|
$attributes[] = 'secure'; |
122
|
|
|
} |
123
|
3 |
|
if ($this->httpOnly) { |
124
|
1 |
|
$attributes[] = 'httponly'; |
125
|
|
|
} |
126
|
|
|
|
127
|
3 |
|
if (count($attributes) > 0) { |
128
|
|
|
// Add attributes to cookie crumb. |
129
|
2 |
|
$cookieCrumb .= '; ' . implode('; ', $attributes); |
130
|
|
|
} |
131
|
|
|
|
132
|
3 |
|
return $cookieCrumb; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @inheritDoc |
137
|
|
|
*/ |
138
|
2 |
|
public function domain(): string |
139
|
|
|
{ |
140
|
2 |
|
return $this->domain; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @inheritDoc |
145
|
|
|
*/ |
146
|
6 |
|
public function expiration() |
147
|
|
|
{ |
148
|
6 |
|
return $this->expiration; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @inheritDoc |
153
|
|
|
*/ |
154
|
2 |
|
public function httpOnly(): CookieContract |
155
|
|
|
{ |
156
|
2 |
|
$cookie = clone $this; |
157
|
2 |
|
$cookie->httpOnly = true; |
158
|
|
|
|
159
|
2 |
|
return $cookie; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @inheritDoc |
164
|
|
|
*/ |
165
|
2 |
|
public function isHttpOnly(): bool |
166
|
|
|
{ |
167
|
2 |
|
return $this->httpOnly; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @inheritDoc |
172
|
|
|
*/ |
173
|
2 |
|
public function isSecure(): bool |
174
|
|
|
{ |
175
|
2 |
|
return $this->secure; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @inheritDoc |
180
|
|
|
*/ |
181
|
5 |
|
public function name(): string |
182
|
|
|
{ |
183
|
5 |
|
return $this->name; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @inheritDoc |
188
|
|
|
*/ |
189
|
2 |
|
public function path(): string |
190
|
|
|
{ |
191
|
2 |
|
return $this->path; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @inheritDoc |
196
|
|
|
*/ |
197
|
2 |
|
public function secured(): CookieContract |
198
|
|
|
{ |
199
|
2 |
|
$cookie = clone $this; |
200
|
2 |
|
$cookie->secure = true; |
201
|
|
|
|
202
|
2 |
|
return $cookie; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @inheritDoc |
207
|
|
|
*/ |
208
|
2 |
|
public function value(): string |
209
|
|
|
{ |
210
|
2 |
|
return $this->value; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @inheritDoc |
215
|
|
|
*/ |
216
|
1 |
|
public function withDomain(string $domain): CookieContract |
217
|
|
|
{ |
218
|
1 |
|
$cookie = clone $this; |
219
|
1 |
|
$cookie->domain = $domain; |
220
|
|
|
|
221
|
1 |
|
return $cookie; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @inheritDoc |
226
|
|
|
*/ |
227
|
2 |
|
public function withExpiration(DateTimeInterface $expiration = null): CookieContract |
228
|
|
|
{ |
229
|
2 |
|
$cookie = clone $this; |
230
|
2 |
|
$cookie->expiration = $expiration ? $cookie->freezeExpiration($expiration) : null; |
231
|
|
|
|
232
|
2 |
|
return $cookie; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @inheritDoc |
237
|
|
|
*/ |
238
|
1 |
|
public function withPath(string $path): CookieContract |
239
|
|
|
{ |
240
|
1 |
|
$cookie = clone $this; |
241
|
1 |
|
$cookie->path = $path; |
242
|
|
|
|
243
|
1 |
|
return $cookie; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @inheritDoc |
248
|
|
|
*/ |
249
|
3 |
|
public function withValue(string $value): CookieContract |
250
|
|
|
{ |
251
|
3 |
|
$cookie = clone $this; |
252
|
3 |
|
$cookie->value = $value; |
253
|
|
|
|
254
|
3 |
|
return $cookie; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Creates immutable date time instance from the provided one. |
259
|
|
|
* |
260
|
|
|
* @param DateTimeInterface $expires |
261
|
|
|
* @return DateTimeImmutable |
262
|
|
|
*/ |
263
|
6 |
|
private function freezeExpiration(DateTimeInterface $expires): DateTimeImmutable |
264
|
|
|
{ |
265
|
6 |
|
if (!$expires instanceof DateTimeImmutable) { |
266
|
3 |
|
$expires = new DateTimeImmutable($expires->format(DateTime::ISO8601), $expires->getTimezone()); |
267
|
|
|
} |
268
|
|
|
|
269
|
6 |
|
return $expires; |
270
|
|
|
} |
271
|
|
|
} |