Skip to content
Permalink
Browse files

Add Cookie::getStringValue()

Add a new method to get a cookie value as a string. I considered
changing how `getValue()` works but thought that had bigger
compatibility implications. Instead I've modified the
interface/implementation. Normally I would shy away from changing
interfaces, but this is a very new class and the likelihood of there be
user-space implementations is quite low.

Refs #11208
  • Loading branch information...
markstory committed Sep 19, 2017
1 parent 98f4673 commit 56a56a21537ebb1f2bf0f77c5b3b8ccadbe8c2e6
Showing with 38 additions and 0 deletions.
  1. +12 −0 src/Http/Cookie/Cookie.php
  2. +9 −0 src/Http/Cookie/CookieInterface.php
  3. +17 −0 tests/TestCase/Http/Cookie/CookieTest.php
@@ -243,6 +243,18 @@ public function getValue()
return $this->value;
}
/**
* {@inheritDoc}
*/
public function getStringValue()
{
if ($this->isExpanded) {
return $this->_flatten($this->value);
}
return $this->value;
}
/**
* {@inheritDoc}
*/
@@ -48,6 +48,15 @@ public function getName();
*/
public function getValue();
/**
* Gets the cookie value as a string.
*
* This will collapse any complex data in the cookie with json_encode()
*
* @return string
*/
public function getStringValue();
/**
* Create a cookie with an updated value.
*
@@ -117,6 +117,23 @@ public function testWithValue()
$this->assertSame('new', $new->getValue());
}
/**
* Test getting the value from the cookie
*
* @return void
*/
public function testGetStringValue()
{
$cookie = new Cookie('cakephp', 'thing');
$this->assertSame('thing', $cookie->getStringValue());
$value = ['user_id' => 1, 'token' => 'abc123'];
$cookie = new Cookie('cakephp', $value);
$this->assertSame($value, $cookie->getValue());
$this->assertSame(json_encode($value), $cookie->getStringValue());
}
/**
* Test setting domain in cookies
*

0 comments on commit 56a56a2

Please sign in to comment.
You can’t perform that action at this time.