Fixing JavascriptHelper::object() bug with numeric indexes, as reported on the mailing list

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4838 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2007-04-11 18:02:10 +00:00
parent 3a828d2338
commit 8df2d428ff
2 changed files with 14 additions and 8 deletions

View file

@ -349,21 +349,15 @@ class JavascriptHelper extends AppHelper {
}
$out = array();
$key = array();
$keys = array();
if (is_array($data)) {
$keys = array_keys($data);
}
$numeric = true;
if (!empty($keys)) {
foreach($keys as $key) {
if (!is_numeric($key)) {
$numeric = false;
break;
}
}
$numeric = (array_values($keys) === array_keys(array_values($keys)));
}
foreach($data as $key => $val) {

View file

@ -74,6 +74,18 @@ class JavascriptTest extends UnitTestCase {
$result = $this->js->object(array('default' => 0));
$expected = '{"default":0}';
$this->assertEqual($result, $expected);
$result = $this->js->object(array(
'2007' => array(
'Spring'=>array('1'=>array('id'=>'1','name'=>'Josh'), '2'=>array('id'=>'2','name'=>'Becky')),
'Fall' => array('1'=>array('id'=>'1','name'=>'Josh'), '2'=>array('id'=>'2','name'=>'Becky'))
), '2006' => array(
'Spring' => array('1'=>array('id'=>'1','name'=>'Josh'), '2'=>array('id'=>'2','name'=>'Becky')),
'Fall' => array('1' => array('id'=>'1','name'=>'Josh'), '2'=>array('id'=>'2','name'=>'Becky')
))
));
$expected = '{"2007":{"Spring":{"1":{"id":1, "name":"Josh"}, "2":{"id":2, "name":"Becky"}}, "Fall":{"1":{"id":1, "name":"Josh"}, "2":{"id":2, "name":"Becky"}}}, "2006":{"Spring":{"1":{"id":1, "name":"Josh"}, "2":{"id":2, "name":"Becky"}}, "Fall":{"1":{"id":1, "name":"Josh"}, "2":{"id":2, "name":"Becky"}}}}';
$this->assertEqual($result, $expected);
}
function tearDown() {