Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Backporting regression fix in Set::extract() for 1.3 to 1.2. Closes #…

  • Loading branch information...
commit f0af34b04c5f0332cbd9981a412fde747f8c25e6 1 parent b0f9894
ADmad ADmad authored
Showing with 38 additions and 10 deletions.
  1. +10 −8 cake/libs/set.php
  2. +28 −2 cake/tests/cases/libs/set.test.php
18 cake/libs/set.php
View
@@ -432,14 +432,9 @@ function extract($path, $data = null, $options = array()) {
'key' => $key,
'item' => array_keys($context['item']),
);
- } elseif (($key === $token || (ctype_digit($token) && $key == $token) || $token === '.')) {
- $context['trace'][] = $key;
- $matches[] = array(
- 'trace' => $context['trace'],
- 'key' => $key,
- 'item' => $context['item'],
- );
- } elseif (is_array($context['item']) && array_key_exists($token, $context['item'])) {
+ } elseif (is_array($context['item'])
+ && array_key_exists($token, $context['item'])
+ && !(strval($key) === strval($token) && count($tokens) == 1 && $tokens[0] === '.')) {
$items = $context['item'][$token];
if (!is_array($items)) {
$items = array($items);
@@ -478,6 +473,13 @@ function extract($path, $data = null, $options = array()) {
'item' => $item,
);
}
+ } elseif ($key === $token || (ctype_digit($token) && $key == $token) || $token === '.') {
+ $context['trace'][] = $key;
+ $matches[] = array(
+ 'trace' => $context['trace'],
+ 'key' => $key,
+ 'item' => $context['item'],
+ );
}
}
if ($conditions) {
30 cake/tests/cases/libs/set.test.php
View
@@ -902,7 +902,7 @@ function testExtract() {
$result = Set::extract('/ParentNode/name', $hasMany);
$expected = array('Second');
$this->assertEqual($result, $expected);
-
+
$data = array(
array(
'Category' => array(
@@ -936,6 +936,32 @@ function testExtract() {
);
$result = Set::extract('/Category[id=1]/..', $data);
$this->assertEqual($result, $expected);
+
+ $data = array(
+ array(
+ 'ChildNode' => array('id' => 1),
+ array('name' => 'Item 1')
+ ),
+ array(
+ 'ChildNode' => array('id' => 2),
+ array('name' => 'Item 2')
+ ),
+ );
+
+ $expected = array(
+ 'Item 1',
+ 'Item 2'
+ );
+ $result = Set::extract('/0/name', $data);
+ $this->assertEqual($result, $expected);
+
+ $data = array(
+ array('A1', 'B1'),
+ array('A2', 'B2')
+ );
+ $expected = array('A1', 'A2');
+ $result = Set::extract('/0', $data);
+ $this->assertEqual($result, $expected);
}
/**
* test parent selectors with extract
@@ -1141,7 +1167,7 @@ function testExtractWithNonZeroArrays() {
$expected = array(1, 2, 3);
$r = Set::extract('/User/id', $nonZero);
$this->assertEqual($r, $expected);
-
+
$expected = array(
array('User' => array('id' => 1, 'name' => 'John')),
array('User' => array('id' => 2, 'name' => 'Bob')),
Please sign in to comment.
Something went wrong with that request. Please try again.