Browse Source

Add data type check.

ADmad 10 years ago
parent
commit
0956782d80
2 changed files with 20 additions and 1 deletions
  1. 8 1
      src/Utility/Hash.php
  2. 12 0
      tests/TestCase/Utility/HashTest.php

+ 8 - 1
src/Utility/Hash.php

@@ -36,7 +36,8 @@ class Hash
      * Does not support the full dot notation feature set,
      * but is faster for simple read operations.
      *
-     * @param array|\ArrayAccess $data Array of data to operate on.
+     * @param array|\ArrayAccess $data Array of data or object implementing
+     *   \ArrayAccess interface to operate on.
      * @param string|array $path The path being searched for. Either a dot
      *   separated string, or an array of path segments.
      * @param mixed $default The return value when the path does not exist
@@ -46,6 +47,12 @@ class Hash
      */
     public static function get($data, $path, $default = null)
     {
+        if (!(is_array($data) || $data instanceof ArrayAccess)) {
+            throw new InvalidArgumentException(
+                'Invalid data type, must an array or \ArrayAccess instance.'
+            );
+        }
+
         if (empty($data) || $path === null || $path === '') {
             return $default;
         }

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

@@ -262,6 +262,18 @@ class HashTest extends TestCase
     }
 
     /**
+     * Test get() for invalid $data type
+     *
+     * @expectedException \InvalidArgumentException
+     * @expectedExceptionMessage Invalid data type, must an array or \ArrayAccess instance.
+     * @return void
+     */
+    public function testGetInvalidData()
+    {
+        Hash::get('string', 'path');
+    }
+
+    /**
      * Test get() with an invalid path
      *
      * @expectedException \InvalidArgumentException