mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-19 02:56:15 +00:00
Adding backwards compatible cookie reading back into CookieComponent.
Cookie values using the 1.x formatting will be read, and upon next write be converted to json encoded values. Fixes #1593
This commit is contained in:
parent
d4ff39206a
commit
63275626ee
2 changed files with 40 additions and 8 deletions
|
@ -467,12 +467,24 @@ class CookieComponent extends Component {
|
|||
|
||||
/**
|
||||
* Explode method to return array from string set in CookieComponent::_implode()
|
||||
* Maintains reading backwards compatibility with 1.x CookieComponent::_implode().
|
||||
*
|
||||
* @param string $string A string containing JSON encoded data, or a bare string.
|
||||
* @return array Map of key and values
|
||||
*/
|
||||
protected function _explode($string) {
|
||||
$ret = json_decode($string, true);
|
||||
return ($ret != null) ? $ret : $string;
|
||||
if ($string[0] === '{' || $string[0] === '[') {
|
||||
$ret = json_decode($string, true);
|
||||
return ($ret != null) ? $ret : $string;
|
||||
}
|
||||
$array = array();
|
||||
foreach (explode(',', $string) as $pair) {
|
||||
$key = explode('|', $pair);
|
||||
if (!isset($key[1])) {
|
||||
return $key[0];
|
||||
}
|
||||
$array[$key[0]] = $key[1];
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -471,6 +471,19 @@ class CookieComponentTest extends CakeTestCase {
|
|||
unset($_COOKIE['CakeTestCookie']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Reading legacy cookie values.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function testReadLegacyCookieValue() {
|
||||
$_COOKIE['CakeTestCookie'] = array(
|
||||
'Legacy' => array('value' => $this->_oldImplode(array(1, 2, 3)))
|
||||
);
|
||||
$result = $this->Cookie->read('Legacy.value');
|
||||
$expected = array(1, 2, 3);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* test that no error is issued for non array data.
|
||||
|
@ -484,6 +497,19 @@ class CookieComponentTest extends CakeTestCase {
|
|||
$this->assertNull($this->Cookie->read('value'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method for generating old style encoded cookie values.
|
||||
*
|
||||
* @return string.
|
||||
*/
|
||||
protected function _oldImplode(array $array) {
|
||||
$string = '';
|
||||
foreach ($array as $key => $value) {
|
||||
$string .= ',' . $key . '|' . $value;
|
||||
}
|
||||
return substr($string, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implode method to keep keys are multidimensional arrays
|
||||
*
|
||||
|
@ -492,12 +518,6 @@ class CookieComponentTest extends CakeTestCase {
|
|||
*/
|
||||
protected function _implode(array $array) {
|
||||
return json_encode($array);
|
||||
|
||||
$string = '';
|
||||
foreach ($array as $key => $value) {
|
||||
$string .= ',' . $key . '|' . $value;
|
||||
}
|
||||
return substr($string, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Reference in a new issue