Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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...
commit 35446a42a92907e767442a433384472c40480763 1 parent b559be5
Mark Story markstory authored
Showing with 75 additions and 1 deletion.
  1. +1 −1  cake/libs/set.php
  2. +74 −0 cake/tests/cases/libs/set.test.php
2  cake/libs/set.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;
74 cake/tests/cases/libs/set.test.php
View
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.