Skip to content

Commit

Permalink
Adding more methods to the Cookie class
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Krämer committed Feb 17, 2017
1 parent 7524716 commit 7ccc59d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
49 changes: 48 additions & 1 deletion src/Http/Cookie/Cookie.php
Expand Up @@ -13,6 +13,7 @@
*/
namespace Cake\Http\Cookie;

use Cake\Chronos\Chronos;
use Cake\Utility\Hash;
use DateTimeInterface;
use InvalidArgumentException;
Expand Down Expand Up @@ -99,7 +100,7 @@ class Cookie implements CookieInterface
* @param string $name Cookie name
* @param string|array $value Value of the cookie
*/
public function __construct($name, $value)
public function __construct($name, $value = '')
{
$this->validateName($name);
$this->setName($name);
Expand Down Expand Up @@ -243,6 +244,26 @@ public function setDomain($domain)
return $this;
}

/**
* Check if the cookie is secure
*
* @return bool
*/
public function isSecure()
{
return $this->secure;
}

/**
* Check if the cookie is HTTP only
*
* @return bool
*/
public function isHttpOnly()
{
return $this->isHttpOnly();
}

/**
* Sets the expiration date
*
Expand Down Expand Up @@ -304,6 +325,32 @@ public function read($path = null)
return Hash::get($this->value, $path);
}

/**
* Sets the cookies date to a far future so it will virtually never expire
*
* @return $this
*/
public function willNeverExpire()
{
$this->expiresAt = Chronos::now()->addYears(50)->format('U');

return $this;
}

/**
* Deletes the cookie from the browser
*
* This is done by setting the expiration time to "now"
*
* @return $this
*/
public function willBeDeleted()
{
$this->expiresAt = Chronos::now()->format('U');

return $this;
}

/**
* Encrypts the cookie value
*
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Cookie/CookieCryptTrait.php
Expand Up @@ -108,7 +108,7 @@ protected function _encrypt($value, $encrypt, $key = null)
return $value;
}
$this->checkCipher($encrypt);
$prefix = "Q2FrZQ==.";
$prefix = 'Q2FrZQ==.';
$cipher = null;
if ($key === null) {
$key = $this->getCryptoKey();
Expand Down

0 comments on commit 7ccc59d

Please sign in to comment.