Skip to content

Commit

Permalink
Merge pull request #736 from jails/pr1
Browse files Browse the repository at this point in the history
Fix `Set::extract()` close #196
  • Loading branch information
nateabele committed Dec 13, 2012
2 parents c008a4f + 719e278 commit 43e6143
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
4 changes: 2 additions & 2 deletions tests/cases/util/SetTest.php
Expand Up @@ -682,14 +682,14 @@ public function testExtractHabtm() {
$this->assertEqual($result[0]['Comment']['User']['name'], 'bob');
$this->assertEqual($result[1]['Comment']['User']['name'], 'tod');
$this->assertEqual($result[2]['Comment']['User']['name'], 'dan');
$this->assertEqual($result[3]['Comment']['User']['name'], 'dan');
$this->assertEqual($result[3]['Comment']['User']['name'], 'jim');

$result = Set::extract($habtm, '/Comment/User[name=/[a-z]+/]/..');
$this->assertEqual(count($result), 4);
$this->assertEqual($result[0]['Comment']['User']['name'], 'bob');
$this->assertEqual($result[1]['Comment']['User']['name'], 'tod');
$this->assertEqual($result[2]['Comment']['User']['name'], 'dan');
$this->assertEqual($result[3]['Comment']['User']['name'], 'dan');
$this->assertEqual($result[3]['Comment']['User']['name'], 'jim');

$result = Set::extract($habtm, '/Comment/User[name=/bob|dan/]/..');
$this->assertEqual(count($result), 2);
Expand Down
22 changes: 10 additions & 12 deletions util/Set.php
Expand Up @@ -296,16 +296,12 @@ public static function extract(array $data, $path = null, array $options = array
if (count($context['trace']) == 1) {
$context['trace'][] = $context['key'];
}
$parent = join('/', $context['trace']) . '/.';

array_pop($context['trace']);
$parent = join('/', $context['trace']);
$context['item'] = static::extract($data, $parent);
$context['key'] = array_pop($context['trace']);
if (isset($context['trace'][1]) && $context['trace'][1] > 0) {
$context['item'] = $context['item'][0];
} elseif (!empty($context['item'][$key])) {
$context['item'] = $context['item'][$key];
} else {
$context['item'] = array_shift($context['item']);
}
array_pop($context['trace']);
$context['item'] = array_shift($context['item']);
$matches[] = $context;
continue;
}
Expand Down Expand Up @@ -345,7 +341,7 @@ public static function extract(array $data, $path = null, array $options = array
array_unshift($tokens, $token);
}
} else {
$key = $token;
$ctext[] = $token;
}

$matches[] = array(
Expand Down Expand Up @@ -388,8 +384,10 @@ public static function extract(array $data, $path = null, array $options = array
$r = array();

foreach ($matches as $match) {
if ((!$options['flatten'] || is_array($match['item'])) && !is_int($match['key'])) {
$r[] = array($match['key'] => $match['item']);
$key = array_pop($match['trace']);
$condition = (!is_int($key) && $key !== null);
if ((!$options['flatten'] || is_array($match['item'])) && $condition) {
$r[] = array($key => $match['item']);
} else {
$r[] = $match['item'];
}
Expand Down

0 comments on commit 43e6143

Please sign in to comment.