Permalink
Browse files

Making filtering of extracted arrays remember their key. This fixes a…

…ttribute selectors followed by parent selectors returning seemingly random results. Fixes #502
  • Loading branch information...
1 parent b559be5 commit 35446a42a92907e767442a433384472c40480763 @markstory markstory committed Mar 27, 2010
Showing with 75 additions and 1 deletion.
  1. +1 −1 cake/libs/set.php
  2. +74 −0 cake/tests/cases/libs/set.test.php
View
@@ -486,7 +486,7 @@ function extract($path, $data = null, $options = array()) {
$length = count($matches);
foreach ($matches as $i => $match) {
if (Set::matches(array($condition), $match['item'], $i + 1, $length)) {
- $filtered[] = $match;
+ $filtered[$i] = $match;
}
}
$matches = $filtered;
@@ -975,6 +975,80 @@ function testExtract() {
$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.
*
* @return void

0 comments on commit 35446a4

Please sign in to comment.