Browse Source

array_column() only works for actual arrays.

ADmad 10 years ago
parent
commit
b729195028
2 changed files with 14 additions and 4 deletions
  1. 0 3
      src/Utility/Hash.php
  2. 14 1
      tests/TestCase/Utility/HashTest.php

+ 0 - 3
src/Utility/Hash.php

@@ -138,9 +138,6 @@ class Hash
         }
         }
 
 
         if (strpos($path, '[') === false) {
         if (strpos($path, '[') === false) {
-            if (function_exists('array_column') && preg_match('|^\{n\}\.(\w+)$|', $path, $matches)) {
-                return array_column($data, $matches[1]);
-            }
             $tokens = explode('.', $path);
             $tokens = explode('.', $path);
         } else {
         } else {
             $tokens = Text::tokenize($path, '.', '[', ']');
             $tokens = Text::tokenize($path, '.', '[', ']');

+ 14 - 1
tests/TestCase/Utility/HashTest.php

@@ -964,7 +964,7 @@ class HashTest extends TestCase
 
 
         $data = new ArrayObject([
         $data = new ArrayObject([
             'User' => new ArrayObject([
             'User' => new ArrayObject([
-                0 => new ArrayObject([
+                0 => new Entity([
                     'id' => 4,
                     'id' => 4,
                     'name' => 'Neo'
                     'name' => 'Neo'
                 ]),
                 ]),
@@ -979,6 +979,19 @@ class HashTest extends TestCase
         ]);
         ]);
         $result = Hash::extract($data, 'User.{n}.name');
         $result = Hash::extract($data, 'User.{n}.name');
         $this->assertEquals($expected, $result);
         $this->assertEquals($expected, $result);
+
+        $data = [
+            0 => new Entity([
+                'id' => 4,
+                'name' => 'Neo'
+            ]),
+            'stringKey' => new ArrayObject([
+                'name' => 'Fail'
+            ])
+        ];
+        $result = Hash::extract($data, '{n}.name');
+        $expected = ['Neo'];
+        $this->assertEquals($expected, $result);
     }
     }
 
 
     /**
     /**