Making filtering of extracted arrays remember their key. This fixes attribute selectors followed by parent selectors returning seemingly random results. Fixes #502

This commit is contained in:
Mark Story 2010-03-27 16:23:46 -04:00
parent b559be5822
commit 35446a42a9
2 changed files with 75 additions and 1 deletions

View file

@ -486,7 +486,7 @@ class Set extends Object {
$length = count($matches); $length = count($matches);
foreach ($matches as $i => $match) { foreach ($matches as $i => $match) {
if (Set::matches(array($condition), $match['item'], $i + 1, $length)) { if (Set::matches(array($condition), $match['item'], $i + 1, $length)) {
$filtered[] = $match; $filtered[$i] = $match;
} }
} }
$matches = $filtered; $matches = $filtered;

View file

@ -974,6 +974,80 @@ class SetTest extends CakeTestCase {
$expected = array('Second'); $expected = array('Second');
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
} }
/**
* test parent selectors with extract
*
* @return void
*/
function testExtractParentSelector() {
$a = array(
'Model' => array(
'0' => array(
'id' => 18,
'SubModelsModel' => array(
'id' => 1,
'submodel_id' => 66,
'model_id' => 18,
'type' => 1
),
),
'1' => array(
'id' => 0,
'SubModelsModel' => array(
'id' => 2,
'submodel_id' => 66,
'model_id' => 0,
'type' => 1
),
),
'2' => array(
'id' => 17,
'SubModelsModel' => array(
'id' => 3,
'submodel_id' => 66,
'model_id' => 17,
'type' => 2
),
),
'3' => array(
'id' => 0,
'SubModelsModel' => array(
'id' => 4,
'submodel_id' => 66,
'model_id' => 0,
'type' => 2
)
)
)
);
$expected = array(
array(
'Model' => array(
'id' => 17,
'SubModelsModel' => array(
'id' => 3,
'submodel_id' => 66,
'model_id' => 17,
'type' => 2
),
)
),
array(
'Model' => array(
'id' => 0,
'SubModelsModel' => array(
'id' => 4,
'submodel_id' => 66,
'model_id' => 0,
'type' => 2
)
)
)
);
$r = Set::extract('/Model/SubModelsModel[type=2]/..', $a);
$this->assertEqual($r, $expected);
}
/** /**
* test that extract() still works when arrays don't contain a 0 index. * test that extract() still works when arrays don't contain a 0 index.
* *