Skip to content

Commit b781762

Browse files
committed
Fixing regression in Set::extract(). Refs #1895, #1816, #1692.
1 parent 7983b41 commit b781762

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

lib/Cake/Test/Case/Utility/SetTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,32 @@ public function testExtract() {
991991
);
992992
$result = Set::extract('/Category[id=1]/..', $data);
993993
$this->assertEqual($expected, $result);
994+
995+
$data = array(
996+
array(
997+
'ChildNode' => array('id' => 1),
998+
array('name' => 'Item 1')
999+
),
1000+
array(
1001+
'ChildNode' => array('id' => 2),
1002+
array('name' => 'Item 2')
1003+
),
1004+
);
1005+
1006+
$expected = array(
1007+
'Item 1',
1008+
'Item 2'
1009+
);
1010+
$result = Set::extract('/0/name', $data);
1011+
$this->assertEqual($expected, $result);
1012+
1013+
$data = array(
1014+
array('A1', 'B1'),
1015+
array('A2', 'B2')
1016+
);
1017+
$expected = array('A1', 'A2');
1018+
$result = Set::extract('/0', $data);
1019+
$this->assertEqual($expected, $result);
9941020
}
9951021

9961022
/**

lib/Cake/Utility/Set.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -400,14 +400,9 @@ public static function extract($path, $data = null, $options = array()) {
400400
'key' => $key,
401401
'item' => array_keys($context['item']),
402402
);
403-
} elseif (($key === $token || (ctype_digit($token) && $key == $token) || $token === '.')) {
404-
$context['trace'][] = $key;
405-
$matches[] = array(
406-
'trace' => $context['trace'],
407-
'key' => $key,
408-
'item' => $context['item'],
409-
);
410-
} elseif (is_array($context['item']) && array_key_exists($token, $context['item'])) {
403+
} elseif (is_array($context['item'])
404+
&& array_key_exists($token, $context['item'])
405+
&& !(strval($key) === strval($token) && count($tokens) == 1 && $tokens[0] === '.')) {
411406
$items = $context['item'][$token];
412407
if (!is_array($items)) {
413408
$items = array($items);
@@ -446,6 +441,13 @@ public static function extract($path, $data = null, $options = array()) {
446441
'item' => $item,
447442
);
448443
}
444+
} elseif ($key === $token || (ctype_digit($token) && $key == $token) || $token === '.') {
445+
$context['trace'][] = $key;
446+
$matches[] = array(
447+
'trace' => $context['trace'],
448+
'key' => $key,
449+
'item' => $context['item'],
450+
);
449451
}
450452
}
451453
if ($conditions) {

0 commit comments

Comments
 (0)