Skip to content

Commit

Permalink
Start making Cookie immutable.
Browse files Browse the repository at this point in the history
Convert setValue() to withValue() which supports immutable workflows.
  • Loading branch information
markstory committed Mar 19, 2017
1 parent e67639c commit ba5887e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
33 changes: 26 additions & 7 deletions src/Http/Cookie/Cookie.php
Expand Up @@ -33,6 +33,13 @@
* the past). They can also be used to remember arbitrary pieces of information
* that the user previously entered into form fields such as names, and preferences.
*
* Cookie objects are immutable, and you must re-assign variables when modifying
* cookie objects:
*
* ```
* $cookie = $cookie->withValue('0');
* ```
*
* @link https://tools.ietf.org/html/rfc6265
* @link https://en.wikipedia.org/wiki/HTTP_cookie
*/
Expand Down Expand Up @@ -116,8 +123,8 @@ class Cookie implements CookieInterface
public function __construct($name, $value = '', $expiresAt = null, $path = '', $domain = '', $secure = false, $httpOnly = false)
{
$this->validateName($name);
$this->setName($name);
$this->setValue($value);
$this->name = $name;
$this->_setValue($value);
$this->setDomain($domain);
$this->setHttpOnly($httpOnly);
$this->setPath($path);
Expand Down Expand Up @@ -223,20 +230,32 @@ public function getValue()
}

/**
* Sets the raw cookie data
* Create a cookie with an updated value.
*
* @param string|array $value Value of the cookie to set
* @return $this
* @return static
*/
public function withValue($value)
{
$new = clone $this;
$new->_setValue($value);

return $new;
}

/**
* Setter for the value attribute.
*
* @param mixed $value The value to store.
* @return void
*/
public function setValue($value)
protected function _setValue($value)
{
if (is_array($value)) {
$this->isExpanded = true;
}

$this->value = $value;

return $this;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Http/Cookie/CookieInterface.php
Expand Up @@ -41,12 +41,12 @@ public function getName();
public function getValue();

/**
* Sets the raw cookie data
* Create a cookie with an updated value.
*
* @param string|array $value Value of the cookie to set
* @return $this
* @return static
*/
public function setValue($value);
public function withValue($value);

/**
* Returns the cookie as header value
Expand Down
14 changes: 14 additions & 0 deletions tests/TestCase/Http/Cookie/CookieTest.php
Expand Up @@ -122,6 +122,20 @@ public function testGetValue()
$this->assertEquals('', $result);
}

/**
* Test setting values in cookies
*
* @return void
*/
public function testWithValue()
{
$cookie = new Cookie('cakephp', 'cakephp-rocks');
$new = $cookie->withValue('new');
$this->assertNotSame($new, $cookie, 'Should make a clone');
$this->assertSame('cakephp-rocks', $cookie->getValue(), 'old instance not modified');
$this->assertSame('new', $new->getValue());
}

/**
* testInflateAndExpand
*
Expand Down

0 comments on commit ba5887e

Please sign in to comment.