Skip to content

Commit

Permalink
support more than one matcher for Hash insert
Browse files Browse the repository at this point in the history
  • Loading branch information
Shiyanov Sergey committed Jul 21, 2015
1 parent 7af3fa1 commit 318da77
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/Utility/Hash.php
Expand Up @@ -300,12 +300,10 @@ public static function insert(array $data, $path, $values = null)

foreach ($data as $k => $v) {
if (static::_matchToken($k, $token)) {
if ($conditions && static::_matches($v, $conditions)) {
$data[$k] = array_merge($v, $values);
continue;
}
if (!$conditions) {
$data[$k] = static::insert($v, $nextPath, $values);
if (!$conditions || static::_matches($v, $conditions)) {
$data[$k] = $nextPath
? static::insert($v, $nextPath, $values)
: array_merge($v, (array) $values);
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions tests/TestCase/Utility/HashTest.php
Expand Up @@ -1476,6 +1476,17 @@ public function testInsertMulti()
4 => ['Item' => ['id' => 5, 'title' => 'fifth']],
];
$this->assertEquals($expected, $result);

$data[3]['testable'] = true;
$result = Hash::insert($data, '{n}[testable].Item[id=/\b2|\b4/].test', 2);
$expected = [
0 => ['Item' => ['id' => 1, 'title' => 'first']],
1 => ['Item' => ['id' => 2, 'title' => 'second']],
2 => ['Item' => ['id' => 3, 'title' => 'third']],
3 => ['Item' => ['id' => 4, 'title' => 'fourth', 'test' => 2], 'testable' => true],
4 => ['Item' => ['id' => 5, 'title' => 'fifth']],
];
$this->assertEquals($expected, $result);
}

/**
Expand Down

0 comments on commit 318da77

Please sign in to comment.