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