Skip to content

Commit

Permalink
add cookie propertiy SameSite
Browse files Browse the repository at this point in the history
  • Loading branch information
sentrychen committed Apr 21, 2020
1 parent cfc0927 commit db4c3b4
Showing 1 changed file with 56 additions and 16 deletions.
72 changes: 56 additions & 16 deletions src/http-message/src/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,21 @@ class Cookie
* Default cookie properties
*/
public const DEFAULTS = [
'value' => '',
'domain' => '',
'path' => '',
'expires' => 0,
'secure' => false,
'value' => '',
'domain' => '',
'path' => '',
'expires' => 0,
'secure' => false,
'httpOnly' => false,
'hostOnly' => false,
'sameSite' => ''
];

/**
* SameSite Values
*/
public const SAME_SITE_VALUES = ['Strict','Lax','None'];

/**
* @var string
*/
Expand Down Expand Up @@ -57,7 +63,7 @@ class Cookie
/**
* @var bool
*/
private $secure = false;
private $secure = false;

/**
* @var bool
Expand All @@ -69,10 +75,18 @@ class Cookie
*/
private $httpOnly = false;

/**
* @var string
*/
private $sameSite = '';


/**
* @param array $config
*
* @return self
* @throws \ReflectionException
* @throws \Swoft\Bean\Exception\ContainerException
*/
public static function new(array $config = []): self
{
Expand All @@ -91,13 +105,14 @@ public static function new(array $config = []): self
public function toArray(): array
{
return [
'value' => $this->value,
'domain' => $this->domain,
'path' => $this->path,
'expires' => $this->expires,
'secure' => $this->secure,
'value' => $this->value,
'domain' => $this->domain,
'path' => $this->path,
'expires' => $this->expires,
'secure' => $this->secure,
'httpOnly' => $this->httpOnly,
'hostOnly' => $this->hostOnly,
'sameSite' => $this->sameSite,
];
}

Expand All @@ -124,18 +139,19 @@ public function toString(): string
$result .= '; expires=' . gmdate('D, d-M-Y H:i:s e', $timestamp);
}

if ($this->sameSite) {
$result .= '; SameSite=' . $this->sameSite;
}

if ($this->secure) {
$result .= '; secure';
}

// if ($hostOnly) {
// $result .= '; HostOnly';
// }

if ($this->httpOnly) {
$result .= '; HttpOnly';
}


return $result;
}

Expand All @@ -152,7 +168,7 @@ public function __toString(): string
*/
public function delete(): void
{
$this->value = '';
$this->value = '';
$this->expires = -60;
}

Expand Down Expand Up @@ -307,4 +323,28 @@ public function setHttpOnly(bool $httpOnly): Cookie
$this->httpOnly = $httpOnly;
return $this;
}

/**
* @return string
*/
public function getSameSite(): string
{
return $this->sameSite;
}

/**
* @param string $sameSite
*
* @return Cookie
*/
public function setSameSite(string $sameSite): Cookie
{
if (in_array($sameSite,static::SAME_SITE_VALUES)){
$this->sameSite = $sameSite;
}else{
$this->sameSite = '';
}

return $this;
}
}

0 comments on commit db4c3b4

Please sign in to comment.