Browse Source

support more than one matcher for Hash insert

Shiyanov Sergey 10 years ago
parent
commit
318da77fcb
2 changed files with 15 additions and 6 deletions
  1. 4 6
      src/Utility/Hash.php
  2. 11 0
      tests/TestCase/Utility/HashTest.php

+ 4 - 6
src/Utility/Hash.php

@@ -300,12 +300,10 @@ class Hash
 
         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);
                 }
             }
         }

+ 11 - 0
tests/TestCase/Utility/HashTest.php

@@ -1476,6 +1476,17 @@ class HashTest extends TestCase
             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);
     }
 
     /**