@@ -41,7 +41,7 @@ class Cookie
41
41
* @see https://www.php.net/manual/en/datetime.formats.php
42
42
* @see https://www.php.net/manual/en/function.setcookie.php
43
43
*
44
- * @throws CookieException if $sameSite value is wrong
44
+ * @throws CookieException if $sameSite value is wrong.
45
45
*/
46
46
public function __construct (
47
47
private string $ domain = '' ,
@@ -52,12 +52,7 @@ public function __construct(
52
52
private null |string $ sameSite = null ,
53
53
private bool $ secure = false ,
54
54
) {
55
- $ this ->failIfSameSiteValueIsWrong ();
56
-
57
- set_error_handler (
58
- fn (int $ severity , string $ message , string $ file ) =>
59
- $ file === __FILE__ && throw new CookieException ($ message )
60
- );
55
+ $ this ->throwExceptionIfSameSiteValueIsWrong ();
61
56
}
62
57
63
58
/**
@@ -97,11 +92,13 @@ public function get(string $name, mixed $default = null): mixed
97
92
*
98
93
* @see https://www.php.net/manual/en/datetime.formats.php
99
94
*
100
- * @throws CookieException if headers already sent
101
- * @throws CookieException if failure in date/time string analysis
95
+ * @throws CookieException if headers already sent.
96
+ * @throws CookieException if failure in date/time string analysis.
102
97
*/
103
98
public function set (string $ name , mixed $ value , null |int |string |DateTime $ expires = null ): void
104
99
{
100
+ $ this ->throwExceptionIfHeadersWereSent ();
101
+
105
102
$ params = [$ name , $ value , $ this ->getOptions ($ expires === null ? $ this ->expires : $ expires )];
106
103
107
104
$ this ->raw ? setrawcookie (...$ params ) : setcookie (...$ params );
@@ -119,10 +116,12 @@ public function set(string $name, mixed $value, null|int|string|DateTime $expire
119
116
*
120
117
* @see https://www.php.net/manual/en/datetime.formats.php
121
118
*
122
- * @throws CookieException if headers already sent
119
+ * @throws CookieException if headers already sent.
123
120
*/
124
121
public function replace (array $ data , null |int |string |DateTime $ expires = null ): void
125
122
{
123
+ $ this ->throwExceptionIfHeadersWereSent ();
124
+
126
125
foreach ($ data as $ name => $ value ) {
127
126
$ this ->set ($ name , $ value , $ expires );
128
127
}
@@ -133,10 +132,12 @@ public function replace(array $data, null|int|string|DateTime $expires = null):
133
132
*
134
133
* Optionally defines a default value when the cookie does not exist.
135
134
*
136
- * @throws CookieException if headers already sent
135
+ * @throws CookieException if headers already sent.
137
136
*/
138
137
public function pull (string $ name , mixed $ default = null ): mixed
139
138
{
139
+ $ this ->throwExceptionIfHeadersWereSent ();
140
+
140
141
$ value = $ _COOKIE [$ name ] ?? $ default ;
141
142
142
143
$ this ->remove ($ name );
@@ -147,11 +148,13 @@ public function pull(string $name, mixed $default = null): mixed
147
148
/**
148
149
* Deletes a cookie by name.
149
150
*
150
- * @throws CookieException if headers already sent
151
- * @throws CookieException if failure in date/time string analysis
151
+ * @throws CookieException if headers already sent.
152
+ * @throws CookieException if failure in date/time string analysis.
152
153
*/
153
154
public function remove (string $ name ): void
154
155
{
156
+ $ this ->throwExceptionIfHeadersWereSent ();
157
+
155
158
$ params = [$ name , '' , $ this ->getOptions (1 , false )];
156
159
157
160
$ this ->raw ? setrawcookie (...$ params ) : setcookie (...$ params );
@@ -160,10 +163,12 @@ public function remove(string $name): void
160
163
/**
161
164
* Deletes all cookies.
162
165
*
163
- * @throws CookieException if headers already sent
166
+ * @throws CookieException if headers already sent.
164
167
*/
165
168
public function clear (): void
166
169
{
170
+ $ this ->throwExceptionIfHeadersWereSent ();
171
+
167
172
foreach ($ _COOKIE ?? [] as $ name ) {
168
173
$ this ->remove ($ name );
169
174
}
@@ -172,7 +177,7 @@ public function clear(): void
172
177
/**
173
178
* Gets cookie options.
174
179
*
175
- * @throws CookieException if failure in date/time string analysis
180
+ * @throws CookieException if failure in date/time string analysis.
176
181
*/
177
182
private function getOptions (null |int |string |DateTime $ expires , bool $ formatTime = true ): array
178
183
{
@@ -198,7 +203,7 @@ private function getOptions(null|int|string|DateTime $expires, bool $formatTime
198
203
/**
199
204
* Format the expiration time.
200
205
*
201
- * @throws CookieException if failure in date/time string analysis
206
+ * @throws CookieException if failure in date/time string analysis.
202
207
*/
203
208
private function formatExpirationTime (int |string |DateTime $ expires ): int
204
209
{
@@ -215,12 +220,26 @@ private function formatExpirationTime(int|string|DateTime $expires): int
215
220
}
216
221
}
217
222
223
+ /**
224
+ * Throw exception if headers have already been sent.
225
+ *
226
+ * @throws CookieException if headers already sent.
227
+ */
228
+ private function throwExceptionIfHeadersWereSent (): void
229
+ {
230
+ headers_sent ($ file , $ line ) && throw new CookieException (sprintf (
231
+ 'The headers have already been sent in "%s" at line %d. ' ,
232
+ $ file ,
233
+ $ line
234
+ ));
235
+ }
236
+
218
237
/**
219
238
* Throw exception if $sameSite value is wrong.
220
239
*
221
- * @throws CookieException if $sameSite value is wrong
240
+ * @throws CookieException if $sameSite value is wrong.
222
241
*/
223
- private function failIfSameSiteValueIsWrong (): void
242
+ private function throwExceptionIfSameSiteValueIsWrong (): void
224
243
{
225
244
$ values = ['none ' , 'lax ' , 'strict ' ];
226
245
0 commit comments