Skip to content

Commit 959f45a

Browse files
committed
Fix fatal error thrown when replacing scalar with array
Refs #11280
1 parent b3d83af commit 959f45a

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

lib/Cake/Controller/Component/CookieComponent.php

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -229,27 +229,14 @@ public function write($key, $value = null, $encrypt = true, $expires = null) {
229229
}
230230

231231
foreach ($key as $name => $value) {
232-
$names = array($name);
233232
if (strpos($name, '.') !== false) {
234-
$names = explode('.', $name, 2);
235-
}
236-
$firstName = $names[0];
237-
$isMultiValue = (is_array($value) || count($names) > 1);
238-
239-
if (!isset($this->_values[$this->name][$firstName]) && $isMultiValue) {
240-
$this->_values[$this->name][$firstName] = array();
241-
}
242-
243-
if (count($names) > 1) {
244-
$this->_values[$this->name][$firstName] = Hash::insert(
245-
$this->_values[$this->name][$firstName],
246-
$names[1],
247-
$value
248-
);
233+
$this->_values[$this->name] = Hash::insert($this->_values[$this->name], $name, $value);
234+
list($name) = explode('.', $name, 2);
235+
$value = $this->_values[$this->name][$name];
249236
} else {
250-
$this->_values[$this->name][$firstName] = $value;
237+
$this->_values[$this->name][$name] = $value;
251238
}
252-
$this->_write('[' . $firstName . ']', $this->_values[$this->name][$firstName]);
239+
$this->_write('[' . $name . ']', $value);
253240
}
254241
$this->_encrypted = true;
255242
}

lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,24 @@ public function testWriteMixedArray() {
429429
$this->assertEquals($expected, $result);
430430
}
431431

432+
/**
433+
* Test that replacing scalar with array works.
434+
*
435+
* @return void
436+
*/
437+
public function testReplaceScalarWithArray() {
438+
$this->Cookie->write('foo', 1);
439+
$this->Cookie->write('foo.bar', 2);
440+
441+
$data = $this->Cookie->read();
442+
$expected = array(
443+
'foo' => array(
444+
'bar' => 2
445+
)
446+
);
447+
$this->assertEquals($expected, $data);
448+
}
449+
432450
/**
433451
* testReadingCookieValue
434452
*

0 commit comments

Comments
 (0)