Skip to content

Commit de41228

Browse files
author
Florian Krämer
committed
Minor changes and doc blocks for the cookie implementation
1 parent c204d46 commit de41228

File tree

6 files changed

+50
-28
lines changed

6 files changed

+50
-28
lines changed

src/Http/Cookie/Cookie.php

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
use InvalidArgumentException;
2020
use RuntimeException;
2121

22+
/**
23+
* Cookie object to build a cookie and turn it into a header value
24+
*/
2225
class Cookie implements CookieInterface
2326
{
2427

@@ -29,12 +32,12 @@ class Cookie implements CookieInterface
2932
*
3033
* @var string
3134
*/
32-
protected $name;
35+
protected $name = '';
3336

3437
/**
3538
* Raw Cookie value
3639
*/
37-
protected $value;
40+
protected $value = '';
3841

3942
/**
4043
* Cookie data
@@ -44,33 +47,33 @@ class Cookie implements CookieInterface
4447
*
4548
* @var array
4649
*/
47-
protected $data;
50+
protected $data = [];
4851

4952
/**
5053
* @var bool
5154
*/
5255
protected $isExpanded = false;
5356

5457
/**
55-
* HTTP only
58+
* Expiration time
5659
*
57-
* @var bool
60+
* @var int
5861
*/
5962
protected $expiresAt = 0;
6063

6164
/**
6265
* Path
6366
*
64-
* @var string
67+
* @var string|null
6568
*/
66-
protected $path = '';
69+
protected $path = null;
6770

6871
/**
6972
* Domain
7073
*
71-
* @var string
74+
* @var string|null
7275
*/
73-
protected $domain = '';
76+
protected $domain = null;
7477

7578
/**
7679
* Secure
@@ -84,14 +87,14 @@ class Cookie implements CookieInterface
8487
*
8588
* @var bool
8689
*/
87-
protected $httpOnly;
90+
protected $httpOnly = false;
8891

8992
/**
90-
* The key for en- end decrypting the cookie
93+
* The key for encrypting and decrypting the cookie
9194
*
9295
* @var string
9396
*/
94-
protected $encryptionKey;
97+
protected $encryptionKey = '';
9598

9699
/**
97100
* Constructor
@@ -106,6 +109,19 @@ public function __construct($name, $value)
106109
$this->setValue($value);
107110
}
108111

112+
/**
113+
* Builds the expiration value part of the header string
114+
*
115+
* @return string
116+
*/
117+
protected function _buildExpirationValue()
118+
{
119+
return sprintf(
120+
'; expires=%s',
121+
gmdate('D, d-M-Y H:i:s T', $this->expiresAt)
122+
);
123+
}
124+
109125
/**
110126
* Returns a header value as string
111127
*
@@ -115,15 +131,12 @@ public function toHeaderValue()
115131
{
116132
$headerValue = sprintf('%s=%s', $this->name, urlencode($this->value));
117133
if ($this->expiresAt !== 0) {
118-
$headerValue .= sprintf(
119-
'; expires=%s',
120-
gmdate('D, d-M-Y H:i:s T', $this->expiresAt)
121-
);
134+
$headerValue .= $this->_buildExpirationValue();
122135
}
123-
if (empty($this->path) === false) {
136+
if (!empty($this->path)) {
124137
$headerValue .= sprintf('; path=%s', $this->path);
125138
}
126-
if (empty($this->domain) === false) {
139+
if (!empty($this->domain)) {
127140
$headerValue .= sprintf('; domain=%s', $this->domain);
128141
}
129142
if ($this->secure) {
@@ -163,9 +176,9 @@ public function getName()
163176
/**
164177
* Validates the cookie name
165178
*
166-
* @throws \InvalidArgumentException
167179
* @param string $name Name of the cookie
168180
* @return void
181+
* @throws \InvalidArgumentException
169182
*/
170183
protected function validateName($name)
171184
{
@@ -206,7 +219,7 @@ public function setValue($value)
206219
/**
207220
* Sets the path
208221
*
209-
* @param string $path Sets the path
222+
* @param string|null $path Sets the path
210223
* @return $this
211224
*/
212225
public function setPath($path)

src/Http/Cookie/CookieCollection.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,18 @@
1313
*/
1414
namespace Cake\Http\Cookie;
1515

16-
use \ArrayIterator ;
16+
use ArrayIterator;
17+
use Countable;
18+
use InvalidArgumentException;
1719

18-
class CookieCollection extends ArrayIterator
20+
/**
21+
* Cookie Collection
22+
*/
23+
class CookieCollection extends ArrayIterator implements Countable
1924
{
2025
/**
26+
* Cookie objects
27+
*
2128
* @var Cookie[]
2229
*/
2330
protected $cookies = [];
@@ -35,21 +42,24 @@ public function __construct(array $cookies = [])
3542
$key = mb_strtolower($name);
3643
$this->cookies[$key] = $cookie;
3744
}
45+
46+
parent::__construct($this->cookies);
3847
}
3948

4049
/**
4150
* Checks if only valid cookie objects are in the array
4251
*
4352
* @param array $cookies Array of cookie objects
4453
* @return void
54+
* @throws \InvalidArgumentException
4555
*/
4656
protected function checkCookies(array $cookies)
4757
{
4858
foreach ($cookies as $index => $cookie) {
4959
if (!$cookie instanceof CookieInterface) {
5060
throw new InvalidArgumentException(
5161
sprintf(
52-
'Expected %s[] as $cookies but instead got `%s` at index %d',
62+
'Expected `%s[]` as $cookies but instead got `%s` at index %d',
5363
static::class,
5464
is_object($cookie) ? get_class($cookie) : gettype($cookie),
5565
$index

src/Http/Cookie/CookieCryptTrait.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
1111
* @link http://cakephp.org CakePHP(tm) Project
12-
* @since 3.1.6
12+
* @since 3.5.0
1313
* @license http://www.opensource.org/licenses/mit-license.php MIT License
1414
*/
1515
namespace Cake\Http\Cookie;
@@ -26,7 +26,6 @@
2626
*/
2727
trait CookieCryptTrait
2828
{
29-
3029
/**
3130
* Valid cipher names for encrypted cookies.
3231
*

src/Http/Cookie/CookieInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
*/
1414
namespace Cake\Http\Cookie;
1515

16+
/**
17+
* Cookie Interface
18+
*/
1619
interface CookieInterface
1720
{
18-
1921
/**
2022
* Returns the cookie as header value
2123
*

src/Http/Cookie/RequestCookies.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace Cake\Http\Cookie;
1515

1616
use InvalidArgumentException;
17-
use Iterator;
1817
use Psr\Http\Message\ServerRequestInterface;
1918

2019
class RequestCookies extends CookieCollection

src/Http/Cookie/ResponseCookies.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
class ResponseCookies extends CookieCollection
1919
{
20-
2120
/**
2221
* Adds the cookies to the response
2322
*

0 commit comments

Comments
 (0)