Fix notice error when reading empty values.

When reading empty values a notice error would be triggered.
Slicing the first char off and comparing that solves this.

Fixes #2537
This commit is contained in:
mark_story 2012-02-11 11:32:44 -05:00
parent 6f914174a6
commit 7e17da0ae8
2 changed files with 18 additions and 1 deletions

View file

@ -483,7 +483,8 @@ class CookieComponent extends Component {
* @return array Map of key and values * @return array Map of key and values
*/ */
protected function _explode($string) { protected function _explode($string) {
if ($string[0] === '{' || $string[0] === '[') { $first = substr($string, 0, 1);
if ($first !== false && $first === '{' || $first === '[') {
$ret = json_decode($string, true); $ret = json_decode($string, true);
return ($ret != null) ? $ret : $string; return ($ret != null) ? $ret : $string;
} }

View file

@ -471,6 +471,21 @@ class CookieComponentTest extends CakeTestCase {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/**
* Test reading empty values.
*/
public function testReadEmpty() {
$_COOKIE['CakeTestCookie'] = array(
'JSON' => '{"name":"value"}',
'Empty' => '',
'String' => '{"somewhat:"broken"}'
);
$this->assertEqual(array('name' => 'value'), $this->Cookie->read('JSON'));
$this->assertEqual('value', $this->Cookie->read('JSON.name'));
$this->assertEqual('', $this->Cookie->read('Empty'));
$this->assertEqual('{"somewhat:"broken"}', $this->Cookie->read('String'));
}
/** /**
* test that no error is issued for non array data. * test that no error is issued for non array data.
* *
@ -483,6 +498,7 @@ class CookieComponentTest extends CakeTestCase {
$this->assertNull($this->Cookie->read('value')); $this->assertNull($this->Cookie->read('value'));
} }
/** /**
* test that deleting a top level keys kills the child elements too. * test that deleting a top level keys kills the child elements too.
* *