ソースを参照

Port fix for #6224 to master.

Fix issues with maxDimensions() to 3.0 as well.
Mark Story 11 年 前
コミット
4c29d7de06
2 ファイル変更14 行追加2 行削除
  1. 6 2
      src/Utility/Hash.php
  2. 8 0
      tests/TestCase/Utility/HashTest.php

+ 6 - 2
src/Utility/Hash.php

@@ -798,10 +798,14 @@ class Hash
         $depth = [];
         if (is_array($data) && reset($data) !== false) {
             foreach ($data as $value) {
-                $depth[] = static::dimensions((array)$value) + 1;
+                if (is_array($value)) {
+                    $depth[] = static::dimensions($value) + 1;
+                } else {
+                    $depth[] = 1;
+                }
             }
         }
-        return max($depth);
+        return empty($depth) ? 0 : max($depth);
     }
 
     /**

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

@@ -282,6 +282,14 @@ class HashTest extends TestCase
      */
     public function testMaxDimensions()
     {
+        $data = [];
+        $result = Hash::maxDimensions($data);
+        $this->assertEquals(0, $result);
+
+        $data = ['a', 'b'];
+        $result = Hash::maxDimensions($data);
+        $this->assertEquals(1, $result);
+
         $data = ['1' => '1.1', '2', '3' => ['3.1' => '3.1.1']];
         $result = Hash::maxDimensions($data);
         $this->assertEquals($result, 2);