Skip to content

Commit

Permalink
Fix fatal error thrown when replacing scalar with array
Browse files Browse the repository at this point in the history
Refs #11280
  • Loading branch information
chinpei215 committed Oct 6, 2017
1 parent b3d83af commit 959f45a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
23 changes: 5 additions & 18 deletions lib/Cake/Controller/Component/CookieComponent.php
Expand Up @@ -229,27 +229,14 @@ public function write($key, $value = null, $encrypt = true, $expires = null) {
}

foreach ($key as $name => $value) {
$names = array($name);
if (strpos($name, '.') !== false) {
$names = explode('.', $name, 2);
}
$firstName = $names[0];
$isMultiValue = (is_array($value) || count($names) > 1);

if (!isset($this->_values[$this->name][$firstName]) && $isMultiValue) {
$this->_values[$this->name][$firstName] = array();
}

if (count($names) > 1) {
$this->_values[$this->name][$firstName] = Hash::insert(
$this->_values[$this->name][$firstName],
$names[1],
$value
);
$this->_values[$this->name] = Hash::insert($this->_values[$this->name], $name, $value);
list($name) = explode('.', $name, 2);
$value = $this->_values[$this->name][$name];
} else {
$this->_values[$this->name][$firstName] = $value;
$this->_values[$this->name][$name] = $value;
}
$this->_write('[' . $firstName . ']', $this->_values[$this->name][$firstName]);
$this->_write('[' . $name . ']', $value);
}
$this->_encrypted = true;
}
Expand Down
18 changes: 18 additions & 0 deletions lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php
Expand Up @@ -429,6 +429,24 @@ public function testWriteMixedArray() {
$this->assertEquals($expected, $result);
}

/**
* Test that replacing scalar with array works.
*
* @return void
*/
public function testReplaceScalarWithArray() {
$this->Cookie->write('foo', 1);
$this->Cookie->write('foo.bar', 2);

$data = $this->Cookie->read();
$expected = array(
'foo' => array(
'bar' => 2
)
);
$this->assertEquals($expected, $data);
}

/**
* testReadingCookieValue
*
Expand Down

0 comments on commit 959f45a

Please sign in to comment.