Skip to content

Commit

Permalink
Changing the Cookie constructor to be similar to setcookie()
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Krämer committed Feb 27, 2017
1 parent a4376f3 commit ac33536
Showing 1 changed file with 51 additions and 13 deletions.
64 changes: 51 additions & 13 deletions src/Http/Cookie/Cookie.php
Expand Up @@ -51,6 +51,8 @@ class Cookie implements CookieInterface

/**
* Raw Cookie value
*
* @var string|array
*/
protected $value = '';

Expand All @@ -69,16 +71,16 @@ class Cookie implements CookieInterface
/**
* Path
*
* @var string|null
* @var string
*/
protected $path = null;
protected $path = '';

/**
* Domain
*
* @var string|null
* @var string
*/
protected $domain = null;
protected $domain = '';

/**
* Secure
Expand All @@ -97,14 +99,29 @@ class Cookie implements CookieInterface
/**
* Constructor
*
* The constructors args are similar to the native php setcookie() method.
*
* @link http://php.net/manual/en/function.setcookie.php
* @param string $name Cookie name
* @param string|array $value Value of the cookie
* @param \DateTimeInterface|null $expiresAt Expiration time and date
* @param string $path Path
* @param string $domain Domain
* @param bool $secure Is secure
* @param bool $httpOnly HTTP Only
*/
public function __construct($name, $value = '')
public function __construct($name, $value = '', $expiresAt = null, $path = '', $domain = '', $secure = false, $httpOnly = false)
{
$this->validateName($name);
$this->setName($name);
$this->setValue($value);
$this->setDomain($domain);
$this->setHttpOnly($httpOnly);
$this->setPath($path);

if ($expiresAt !== null) {
$this->expiresAt($expiresAt);
}
}

/**
Expand All @@ -115,7 +132,7 @@ public function __construct($name, $value = '')
protected function _buildExpirationValue()
{
return sprintf(
'; expires=%s',
'expires=%s',
gmdate('D, d-M-Y H:i:s T', $this->expiresAt)
);
}
Expand All @@ -127,24 +144,25 @@ protected function _buildExpirationValue()
*/
public function toHeaderValue()
{
$headerValue = sprintf('%s=%s', $this->name, urlencode($this->value));
$headerValue[] = sprintf('%s=%s', $this->name, urlencode($this->value));

if ($this->expiresAt !== 0) {
$headerValue .= $this->_buildExpirationValue();
$headerValue[] = $this->_buildExpirationValue();
}
if (!empty($this->path)) {
$headerValue .= sprintf('; path=%s', $this->path);
$headerValue[] = sprintf('path=%s', $this->path);
}
if (!empty($this->domain)) {
$headerValue .= sprintf('; domain=%s', $this->domain);
$headerValue[] = sprintf('domain=%s', $this->domain);
}
if ($this->secure) {
$headerValue .= '; secure';
$headerValue[] = 'secure';
}
if ($this->httpOnly) {
$headerValue .= '; httponly';
$headerValue[] = 'httponly';
}

return $headerValue;
return implode('; ', $headerValue);
}

/**
Expand Down Expand Up @@ -254,6 +272,26 @@ public function isSecure()
return $this->secure;
}

/**
* Set HTTP Only
*
* @param bool $httpOnly HTTP Only
* @return $this
*/
public function setHttpOnly($httpOnly)
{
if (!is_bool($httpOnly)) {
throw new InvalidArgumentException(sprintf(
'The provided arg must be of type `bool` but `%s` given',
gettype($httpOnly)
));
}

$this->httpOnly = $httpOnly;

return $this;
}

/**
* Check if the cookie is HTTP only
*
Expand Down

0 comments on commit ac33536

Please sign in to comment.