1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RafaelDms\Cookie; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* |
7
|
|
|
*/ |
8
|
|
|
final class Cookie |
9
|
|
|
{ |
10
|
|
|
/** @var string the name of the cookie which is also the key for future accesses via `$_COOKIE[...]` */ |
11
|
|
|
private string $name; |
12
|
|
|
|
13
|
|
|
/** @var string|null the value of the cookie that will be stored on the client's machine */ |
14
|
|
|
private ?string $value; |
15
|
|
|
|
16
|
|
|
/** @var int the Unix timestamp indicating the time that the cookie will expire at, i.e. usually `time() + $seconds` */ |
17
|
|
|
private int $expiryTime; |
18
|
|
|
|
19
|
|
|
/** @var string|null the path on the server that the cookie will be valid for (including all sub-directories), e.g. an empty string for the current directory or `/` for the root directory */ |
20
|
|
|
private ?string $path; |
21
|
|
|
|
22
|
|
|
/** @var string|null the domain that the cookie will be valid for (including subdomains) or `null` for the current host (excluding subdomains) */ |
23
|
|
|
private ?string $domain; |
24
|
|
|
|
25
|
|
|
/** @var bool indicates that the cookie should be accessible through the HTTP protocol only and not through scripting languages */ |
26
|
|
|
private bool $httpOnly; |
27
|
|
|
|
28
|
|
|
/** @var bool indicates that the cookie should be sent back by the client over secure HTTPS connections only */ |
29
|
|
|
private bool $secureOnly; |
30
|
|
|
|
31
|
|
|
/** @var bool indicates that the cookie encrypt this value */ |
32
|
|
|
private bool $encrypt; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Prepares a new cookie |
36
|
|
|
* |
37
|
|
|
* @param string $name the name of the cookie which is also the key for future accesses via `$_COOKIE[...]` |
38
|
|
|
*/ |
39
|
|
|
public function __construct(string $name) |
40
|
|
|
{ |
41
|
|
|
$this->name = $name; |
42
|
|
|
$this->value = null; |
43
|
|
|
$this->expiryTime = 0; |
44
|
|
|
$this->path = "/"; |
45
|
|
|
$this->domain = null; |
46
|
|
|
$this->httpOnly = true; |
47
|
|
|
$this->secureOnly = false; |
48
|
|
|
$this->encrypt = true; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Returns the name of the cookie |
53
|
|
|
* |
54
|
|
|
* @return string the name of the cookie which is also the key for future acesses via `$_COOKIE[...]` |
55
|
|
|
*/ |
56
|
|
|
public function getName(): string |
57
|
|
|
{ |
58
|
|
|
return $this->name; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Return the values of the cookie |
64
|
|
|
* @return string|null the value of the cookie that will be stored on the client's machine |
65
|
|
|
*/ |
66
|
|
|
public function getValue(): null|string |
67
|
|
|
{ |
68
|
|
|
return $this->value; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Sets the value for the cookie |
73
|
|
|
* |
74
|
|
|
* @param mixed|null $value the value of the cookie that will be stored on the client's machine |
75
|
|
|
* @return Cookie this instance ofr chaining |
76
|
|
|
*/ |
77
|
|
|
public function setValue(mixed $value): Cookie |
78
|
|
|
{ |
79
|
|
|
$this->value = $value; |
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
public function isEncrypt(): bool |
87
|
|
|
{ |
88
|
|
|
return $this->encrypt; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param bool $encrypt |
93
|
|
|
* @return Cookie |
94
|
|
|
*/ |
95
|
|
|
public function setEncrypt(bool $encrypt): Cookie |
96
|
|
|
{ |
97
|
|
|
$this->encrypt = $encrypt; |
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns the expiry time of the cookie |
103
|
|
|
* |
104
|
|
|
* @return int the Unix timestamp indicating the time that the cookie will expire at, i.e. usually `time() + $seconds` |
105
|
|
|
*/ |
106
|
|
|
public function getExpiryTime(): int |
107
|
|
|
{ |
108
|
|
|
return $this->expiryTime; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Sets the expiry time for the cookie |
113
|
|
|
* |
114
|
|
|
* @param int $expiryTime the Unix timestamp indicating the time that the cookie will expire at, i.e. usually `time() + $seconds` |
115
|
|
|
* @return static this instance for chaining |
116
|
|
|
*/ |
117
|
|
|
public function setExpiryTime(int $expiryTime): Cookie |
118
|
|
|
{ |
119
|
|
|
$this->expiryTime = $expiryTime; |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Returns the path of the cookie |
127
|
|
|
* |
128
|
|
|
* @return string|null the path on the server that the cookie will be valid for (including all sub-directories), e.g. an empty string for the current directory or `/` for the root directory |
129
|
|
|
*/ |
130
|
|
|
public function getPath(): ?string |
131
|
|
|
{ |
132
|
|
|
return $this->path; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Sets the path for the cookie |
137
|
|
|
* |
138
|
|
|
* @param string|null $path the path on the server that the cookie will be valid for (including all sub-directories), e.g. an empty string for the current directory or `/` for the root directory |
139
|
|
|
* @return static this instance for chaining |
140
|
|
|
*/ |
141
|
|
|
public function setPath(?string $path): Cookie |
142
|
|
|
{ |
143
|
|
|
$this->path = $path; |
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Returns the domain of the cookie |
149
|
|
|
* |
150
|
|
|
* @return string|null the domain that the cookie will be valid for (including subdomains) or `null` for the current host (excluding subdomains) |
151
|
|
|
*/ |
152
|
|
|
public function getDomain(): ?string |
153
|
|
|
{ |
154
|
|
|
return $this->domain; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Sets the domain for the cookie |
159
|
|
|
* |
160
|
|
|
* @param string|null $domain the domain that the cookie will be valid for (including subdomains) or `null` for the current host (excluding subdomains) |
161
|
|
|
* @return static this instance for chaining |
162
|
|
|
*/ |
163
|
|
|
public function setDomain(?string $domain): Cookie |
164
|
|
|
{ |
165
|
|
|
$this->domain = $domain; |
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Returns whether the cookie should be accessible through HTTP only |
171
|
|
|
* @return bool whether the cookie should be accessible through the HTTP protocol only and not through scripting languages |
172
|
|
|
*/ |
173
|
|
|
public function isHttpOnly(): bool |
174
|
|
|
{ |
175
|
|
|
return $this->httpOnly; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Sets whether the cookie should be accessible through HTTP only |
180
|
|
|
* |
181
|
|
|
* @param bool $httpOnly indicates that the cookie should be accessible through the HTTP protocol only and not through scripting languages |
182
|
|
|
* @return static this instance for chaining |
183
|
|
|
*/ |
184
|
|
|
public function setHttpOnly(bool $httpOnly): Cookie |
185
|
|
|
{ |
186
|
|
|
$this->httpOnly = $httpOnly; |
187
|
|
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Returns whether the cookie should be sent over HTTPS only |
192
|
|
|
* |
193
|
|
|
* @return bool whether the cookie should be sent back by the client over secure HTTPS connections only |
194
|
|
|
*/ |
195
|
|
|
public function isSecureOnly(): bool |
196
|
|
|
{ |
197
|
|
|
return $this->secureOnly; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Sets whether the cookie should be sent over HTTPS only |
202
|
|
|
* @param bool $secureOnly indicates that the cookie should be sent back by the client over secure HTTPS connections only |
203
|
|
|
* @return static this instance for chaining |
204
|
|
|
*/ |
205
|
|
|
public function setSecureOnly(bool $secureOnly): Cookie |
206
|
|
|
{ |
207
|
|
|
$this->secureOnly = $secureOnly; |
208
|
|
|
return $this; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Saves the cookie |
213
|
|
|
* @return bool whether the cookie header has successfully been sent (and will *probably* cause the client to set the cookie) |
214
|
|
|
*/ |
215
|
|
|
public function save(): bool |
216
|
|
|
{ |
217
|
|
|
return StaticCookie::set($this->name, $this->value, $this->expiryTime, $this->path, $this->domain, $this->secureOnly, $this->encrypt); |
|
|
|
|
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @return bool |
222
|
|
|
*/ |
223
|
|
|
public function saveAndSet(): bool |
224
|
|
|
{ |
225
|
|
|
$_COOKIE[$this->name] = $this->value; |
226
|
|
|
return $this->save(); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Deletes the cookie |
231
|
|
|
* |
232
|
|
|
* @return bool whether the cookie header has successfully been sent (and will *probably* cause the client to delete the cookie) |
233
|
|
|
*/ |
234
|
|
|
public function delete(): bool |
235
|
|
|
{ |
236
|
|
|
// set the copied cookie's value to an empty string which internally sets the required options for a deletion |
237
|
|
|
$this->setValue(''); |
238
|
|
|
|
239
|
|
|
// save the copied "deletion" cookie |
240
|
|
|
return $this->save(); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Deletes the cookie and immediately removes the corresponding variable from the superglobal `$_COOKIE` array |
245
|
|
|
* |
246
|
|
|
* The variable would otherwise only be deleted at the start of the next HTTP request |
247
|
|
|
* |
248
|
|
|
* @return bool whether the cookie header has successfully been sent (and will *probably* cause the client to delete the cookie) |
249
|
|
|
*/ |
250
|
|
|
public function deleteAndUnset(): bool |
251
|
|
|
{ |
252
|
|
|
StaticCookie::unset($this->name); |
253
|
|
|
|
254
|
|
|
return $this->delete(); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Verify if exists cookie |
259
|
|
|
* @return bool |
260
|
|
|
*/ |
261
|
|
|
public function hasCookie(): bool |
262
|
|
|
{ |
263
|
|
|
if (StaticCookie::has($this->name, $this->value)) { |
264
|
|
|
return true; |
265
|
|
|
} |
266
|
|
|
return false; |
267
|
|
|
} |
268
|
|
|
} |