1
1
<?php
2
2
3
- namespace Corpus \HttpMessageUtils ;
3
+ namespace Corpus \HttpMessageUtils \ Cookie ;
4
4
5
5
use Psr \Http \Message \ResponseInterface ;
6
6
7
7
/**
8
8
* Utility to build an HTTP Cookie header
9
- *
10
- * Inspired by and partly based on `hansott/psr7-cookies`
11
- * MIT License Copyright (c) 2017 Hans Ott [email protected]
12
9
*/
13
- class CookieBuilder {
10
+ class CookieBuilder implements CookieInterface {
14
11
15
12
private string $ name ;
16
13
private string $ value ;
@@ -21,6 +18,8 @@ class CookieBuilder {
21
18
private bool $ httpOnly ;
22
19
private string $ sameSite ;
23
20
21
+ private CookieEncoder $ encoder ;
22
+
24
23
/**
25
24
* @param string $name The name of the cookie
26
25
* @param int $expiration The number of seconds for which this cookie will be valid. `time() + $expiration`
@@ -50,6 +49,8 @@ public function __construct(
50
49
$ this ->secure = $ secure ;
51
50
$ this ->httpOnly = $ httpOnly ;
52
51
$ this ->sameSite = $ sameSite ;
52
+
53
+ $ this ->encoder = new CookieEncoder ;
53
54
}
54
55
55
56
/**
@@ -76,15 +77,15 @@ public function apply( callable $callee = "\\setcookie" ) : bool {
76
77
* `Set-Cookie` header representing this Cookie.
77
78
*/
78
79
public function responseWithHeaderAdded ( ResponseInterface $ response ) : ResponseInterface {
79
- return $ response ->withAddedHeader ('Set-Cookie ' , $ this ->toHeaderValue ( ));
80
+ return $ response ->withAddedHeader ('Set-Cookie ' , ( $ this ->encoder )( $ this ));
80
81
}
81
82
82
83
/**
83
84
* Given a \Psr\Http\Message\ResponseInterface returns a new instance of ResponseInterface replacing any
84
85
* `Set-Cookie` headers with one representing this Cookie.
85
86
*/
86
87
public function responseWithHeader ( ResponseInterface $ response ) : ResponseInterface {
87
- return $ response ->withHeader ('Set-Cookie ' , $ this ->toHeaderValue ( ));
88
+ return $ response ->withHeader ('Set-Cookie ' , ( $ this ->encoder )( $ this ));
88
89
}
89
90
90
91
/**
@@ -180,96 +181,36 @@ public function withSameSite( string $sameSite ) : self {
180
181
return $ that ;
181
182
}
182
183
183
- /**
184
- * Get the cookie name.
185
- */
186
184
public function getName () : string {
187
185
return $ this ->name ;
188
186
}
189
187
190
- /**
191
- * Get the cookie value.
192
- */
193
188
public function getValue () : string {
194
189
return $ this ->value ;
195
190
}
196
191
197
- /**
198
- * Get the cookie expiration.
199
- */
200
192
public function getExpiration () : int {
201
193
return $ this ->expiration ;
202
194
}
203
195
204
- /**
205
- * Get the cookie path.
206
- */
207
196
public function getPath () : string {
208
197
return $ this ->path ;
209
198
}
210
199
211
- /**
212
- * Get the cookie domain.
213
- */
214
200
public function getDomain () : string {
215
201
return $ this ->domain ;
216
202
}
217
203
218
- /**
219
- * Get the cookie secure flag.
220
- */
221
204
public function isSecure () : bool {
222
205
return $ this ->secure ;
223
206
}
224
207
225
- /**
226
- * Get the cookie httpOnly flag.
227
- */
228
208
public function isHttpOnly () : bool {
229
209
return $ this ->httpOnly ;
230
210
}
231
211
232
- /**
233
- * Get the cookie SameSite value.
234
- */
235
212
public function getSameSite () : string {
236
213
return $ this ->sameSite ;
237
214
}
238
215
239
- /**
240
- * Get the cookie as a header value.
241
- */
242
- public function toHeaderValue () : string {
243
- $ headerValue = sprintf ('%s=%s ' , $ this ->name , urlencode ($ this ->value ));
244
-
245
- if ( $ this ->expiration !== 0 ) {
246
- $ headerValue .= sprintf (
247
- '; expires=%s ' ,
248
- gmdate (DATE_RFC1123 , time () + $ this ->expiration )
249
- );
250
- }
251
-
252
- if ( $ this ->path ) {
253
- $ headerValue .= sprintf ('; path=%s ' , $ this ->path );
254
- }
255
-
256
- if ( $ this ->domain ) {
257
- $ headerValue .= sprintf ('; domain=%s ' , $ this ->domain );
258
- }
259
-
260
- if ( $ this ->secure ) {
261
- $ headerValue .= '; secure ' ;
262
- }
263
-
264
- if ( $ this ->httpOnly ) {
265
- $ headerValue .= '; httponly ' ;
266
- }
267
-
268
- if ( $ this ->sameSite ) {
269
- $ headerValue .= sprintf ('; samesite=%s ' , $ this ->sameSite );
270
- }
271
-
272
- return $ headerValue ;
273
- }
274
-
275
216
}
0 commit comments