Skip to content

Commit

Permalink
Minor changes and doc blocks for the cookie implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Krämer committed Feb 15, 2017
1 parent c204d46 commit de41228
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 28 deletions.
53 changes: 33 additions & 20 deletions src/Http/Cookie/Cookie.php
Expand Up @@ -19,6 +19,9 @@
use InvalidArgumentException;
use RuntimeException;

/**
* Cookie object to build a cookie and turn it into a header value
*/
class Cookie implements CookieInterface
{

Expand All @@ -29,12 +32,12 @@ class Cookie implements CookieInterface
*
* @var string
*/
protected $name;
protected $name = '';

/**
* Raw Cookie value
*/
protected $value;
protected $value = '';

/**
* Cookie data
Expand All @@ -44,33 +47,33 @@ class Cookie implements CookieInterface
*
* @var array
*/
protected $data;
protected $data = [];

/**
* @var bool
*/
protected $isExpanded = false;

/**
* HTTP only
* Expiration time
*
* @var bool
* @var int
*/
protected $expiresAt = 0;

/**
* Path
*
* @var string
* @var string|null
*/
protected $path = '';
protected $path = null;

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

/**
* Secure
Expand All @@ -84,14 +87,14 @@ class Cookie implements CookieInterface
*
* @var bool
*/
protected $httpOnly;
protected $httpOnly = false;

/**
* The key for en- end decrypting the cookie
* The key for encrypting and decrypting the cookie
*
* @var string
*/
protected $encryptionKey;
protected $encryptionKey = '';

/**
* Constructor
Expand All @@ -106,6 +109,19 @@ public function __construct($name, $value)
$this->setValue($value);
}

/**
* Builds the expiration value part of the header string
*
* @return string
*/
protected function _buildExpirationValue()
{
return sprintf(
'; expires=%s',
gmdate('D, d-M-Y H:i:s T', $this->expiresAt)
);
}

/**
* Returns a header value as string
*
Expand All @@ -115,15 +131,12 @@ public function toHeaderValue()
{
$headerValue = sprintf('%s=%s', $this->name, urlencode($this->value));
if ($this->expiresAt !== 0) {
$headerValue .= sprintf(
'; expires=%s',
gmdate('D, d-M-Y H:i:s T', $this->expiresAt)
);
$headerValue .= $this->_buildExpirationValue();
}
if (empty($this->path) === false) {
if (!empty($this->path)) {
$headerValue .= sprintf('; path=%s', $this->path);
}
if (empty($this->domain) === false) {
if (!empty($this->domain)) {
$headerValue .= sprintf('; domain=%s', $this->domain);
}
if ($this->secure) {
Expand Down Expand Up @@ -163,9 +176,9 @@ public function getName()
/**
* Validates the cookie name
*
* @throws \InvalidArgumentException
* @param string $name Name of the cookie
* @return void
* @throws \InvalidArgumentException
*/
protected function validateName($name)
{
Expand Down Expand Up @@ -206,7 +219,7 @@ public function setValue($value)
/**
* Sets the path
*
* @param string $path Sets the path
* @param string|null $path Sets the path
* @return $this
*/
public function setPath($path)
Expand Down
16 changes: 13 additions & 3 deletions src/Http/Cookie/CookieCollection.php
Expand Up @@ -13,11 +13,18 @@
*/
namespace Cake\Http\Cookie;

use \ArrayIterator ;
use ArrayIterator;
use Countable;
use InvalidArgumentException;

class CookieCollection extends ArrayIterator
/**
* Cookie Collection
*/
class CookieCollection extends ArrayIterator implements Countable
{
/**
* Cookie objects
*
* @var Cookie[]
*/
protected $cookies = [];
Expand All @@ -35,21 +42,24 @@ public function __construct(array $cookies = [])
$key = mb_strtolower($name);
$this->cookies[$key] = $cookie;
}

parent::__construct($this->cookies);
}

/**
* Checks if only valid cookie objects are in the array
*
* @param array $cookies Array of cookie objects
* @return void
* @throws \InvalidArgumentException
*/
protected function checkCookies(array $cookies)
{
foreach ($cookies as $index => $cookie) {
if (!$cookie instanceof CookieInterface) {
throw new InvalidArgumentException(
sprintf(
'Expected %s[] as $cookies but instead got `%s` at index %d',
'Expected `%s[]` as $cookies but instead got `%s` at index %d',
static::class,
is_object($cookie) ? get_class($cookie) : gettype($cookie),
$index
Expand Down
3 changes: 1 addition & 2 deletions src/Http/Cookie/CookieCryptTrait.php
Expand Up @@ -9,7 +9,7 @@
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.1.6
* @since 3.5.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Http\Cookie;
Expand All @@ -26,7 +26,6 @@
*/
trait CookieCryptTrait
{

/**
* Valid cipher names for encrypted cookies.
*
Expand Down
4 changes: 3 additions & 1 deletion src/Http/Cookie/CookieInterface.php
Expand Up @@ -13,9 +13,11 @@
*/
namespace Cake\Http\Cookie;

/**
* Cookie Interface
*/
interface CookieInterface
{

/**
* Returns the cookie as header value
*
Expand Down
1 change: 0 additions & 1 deletion src/Http/Cookie/RequestCookies.php
Expand Up @@ -14,7 +14,6 @@
namespace Cake\Http\Cookie;

use InvalidArgumentException;
use Iterator;
use Psr\Http\Message\ServerRequestInterface;

class RequestCookies extends CookieCollection
Expand Down
1 change: 0 additions & 1 deletion src/Http/Cookie/ResponseCookies.php
Expand Up @@ -17,7 +17,6 @@

class ResponseCookies extends CookieCollection
{

/**
* Adds the cookies to the response
*
Expand Down

0 comments on commit de41228

Please sign in to comment.