Skip to content

Commit

Permalink
Add data type check.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Aug 23, 2015
1 parent bf03f8e commit 0956782
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Utility/Hash.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand Down
12 changes: 12 additions & 0 deletions tests/TestCase/Utility/HashTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,18 @@ public function testGet()
$this->assertEquals(1, Hash::get(new ArrayObject($data), 'a.b.c.d'));
}

/**
* 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
*
Expand Down

0 comments on commit 0956782

Please sign in to comment.