4747
4848namespace Platine \Cookie ;
4949
50+ use DateTimeInterface ;
51+ use InvalidArgumentException ;
52+
53+ /**
54+ * @class Cookie
55+ * @package Platine\Cookie
56+ */
5057class Cookie implements CookieInterface
5158{
5259 /**
@@ -112,7 +119,7 @@ class Cookie implements CookieInterface
112119 *
113120 * @param string $name
114121 * @param string $value
115- * @param int|string|\ DateTimeInterface|null $expire
122+ * @param int|string|DateTimeInterface|null $expire
116123 * @param string |null $domain
117124 * @param string |null $path
118125 * @param bool|null $secure
@@ -122,7 +129,7 @@ class Cookie implements CookieInterface
122129 public function __construct (
123130 string $ name ,
124131 string $ value = '' ,
125- $ expire = null ,
132+ int | string | DateTimeInterface | null $ expire = null ,
126133 ?string $ domain = null ,
127134 ?string $ path = '/ ' ,
128135 ?bool $ secure = true ,
@@ -216,7 +223,7 @@ public function expire(): self
216223 /**
217224 * {@inheritdoc}
218225 */
219- public function withExpires ($ expire = null ): self
226+ public function withExpires (int | string | DateTimeInterface | null $ expire = null ): self
220227 {
221228 if ($ expire === $ this ->expires ) {
222229 return $ this ;
@@ -306,7 +313,7 @@ public function withSecure(bool $secure = true): self
306313 */
307314 public function isHttpOnly (): bool
308315 {
309- return $ this ->httpOnly ? $ this -> httpOnly : false ;
316+ return $ this ->httpOnly ?? false ;
310317 }
311318
312319 /**
@@ -364,7 +371,7 @@ public function __toString(): string
364371 {
365372 $ cookie = $ this ->name . '= ' . rawurlencode ($ this ->value );
366373
367- if (! $ this ->isSession ()) {
374+ if ($ this ->isSession () === false ) {
368375 $ cookie .= '; Expires= ' . gmdate ('D, d-M-Y H:i:s T ' , $ this ->expires );
369376 $ cookie .= '; Max-Age= ' . $ this ->getMaxAge ();
370377 }
@@ -399,14 +406,14 @@ public function __toString(): string
399406 private function setName (string $ name ): self
400407 {
401408 if (empty ($ name )) {
402- throw new \ InvalidArgumentException (sprintf (
409+ throw new InvalidArgumentException (sprintf (
403410 'The cookie name [%s] could not be empty ' ,
404411 $ name
405412 ));
406413 }
407414
408415 if (!preg_match ('/^[a-zA-Z0-9!#$%& \' *+\-.^_`|~]+$/ ' , $ name )) {
409- throw new \ InvalidArgumentException (sprintf (
416+ throw new InvalidArgumentException (sprintf (
410417 'The cookie name [%s] contains invalid characters; must contain any US-ASCII '
411418 . ' characters, except control and separator characters, spaces, or tabs. ' ,
412419 $ name
@@ -431,35 +438,22 @@ private function setValue(string $value): self
431438
432439 /**
433440 * Set the cookie expires
434- * @param mixed $expire
441+ * @param int|string|DateTimeInterface|null $expire
435442 */
436- private function setExpires ($ expire ): self
443+ private function setExpires (int | string | DateTimeInterface | null $ expire ): self
437444 {
438- if (
439- $ expire !== null
440- && !is_int ($ expire )
441- && !is_string ($ expire )
442- && !$ expire instanceof \DateTimeInterface
443- ) {
444- throw new \InvalidArgumentException (sprintf (
445- 'The cookie expire time is not valid; must be null, or string, '
446- . ' or integer, or DateTimeInterface instance; received [%s] ' ,
447- is_object ($ expire ) ? get_class ($ expire ) : gettype ($ expire )
448- ));
449- }
450-
451- if (empty ($ expire )) {
445+ if ($ expire === null || (is_string ($ expire ) && empty ($ expire ))) {
452446 $ expire = 0 ;
453447 }
454448
455- if ($ expire instanceof \ DateTimeInterface) {
449+ if ($ expire instanceof DateTimeInterface) {
456450 $ expire = $ expire ->format ('U ' );
457451 } elseif (!is_numeric ($ expire )) {
458452 $ strExpire = $ expire ;
459453 $ expire = strtotime ($ strExpire );
460454
461455 if ($ expire === false ) {
462- throw new \ InvalidArgumentException (sprintf (
456+ throw new InvalidArgumentException (sprintf (
463457 'The string representation of the cookie expire time [%s] is not valid ' ,
464458 $ strExpire
465459 ));
@@ -521,19 +515,23 @@ private function setHttpOnly(?bool $httpOnly): self
521515 */
522516 private function setSameSite (?string $ sameSite ): self
523517 {
524- $ sameSite = empty ($ sameSite ) ? null : ucfirst (strtolower ($ sameSite ));
518+ $ sameSiteValue = empty ($ sameSite ) ? null : ucfirst (strtolower ($ sameSite ));
525519
526- $ sameSiteList = [self ::SAME_SITE_NONE , self ::SAME_SITE_LAX , self ::SAME_SITE_STRICT ];
520+ $ sameSiteList = [
521+ self ::SAME_SITE_NONE ,
522+ self ::SAME_SITE_LAX ,
523+ self ::SAME_SITE_STRICT ,
524+ ];
527525
528- if ($ sameSite !== null && !in_array ($ sameSite , $ sameSiteList , true )) {
529- throw new \ InvalidArgumentException (sprintf (
526+ if ($ sameSiteValue !== null && !in_array ($ sameSiteValue , $ sameSiteList , true )) {
527+ throw new InvalidArgumentException (sprintf (
530528 'The same site attribute `%s` is not valid; must be one of (%s). ' ,
531- $ sameSite ,
529+ $ sameSiteValue ,
532530 implode (', ' , $ sameSiteList )
533531 ));
534532 }
535533
536- $ this ->sameSite = $ sameSite ;
534+ $ this ->sameSite = $ sameSiteValue ;
537535
538536 return $ this ;
539537 }
0 commit comments