diff --git a/src/IsAssocArray.php b/src/IsAssocArray.php index 27e29a139..cb1716251 100644 --- a/src/IsAssocArray.php +++ b/src/IsAssocArray.php @@ -15,12 +15,17 @@ abstract class IsAssocArray * We have an associative array if at least one key is a string. * * @param mixed $value + * @param bool $allowEmpty * @return bool */ - public static function test($value) + public static function test($value, $allowEmpty = false) { - return (is_array($value) - && count(array_filter(array_keys($value), 'is_string')) > 0 - ); + if (is_array($value)) { + if ($allowEmpty && count($value) == 0) { + return true; + } + return count(array_filter(array_keys($value), 'is_string')) > 0; + } + return false; } } diff --git a/test/IsAssocArrayTest.php b/test/IsAssocArrayTest.php index 14fc59ce1..d834dc241 100644 --- a/test/IsAssocArrayTest.php +++ b/test/IsAssocArrayTest.php @@ -22,6 +22,13 @@ public static function validAssocArrays() ); } + public static function validAssocEmptyArrays() + { + $array = self::validAssocArrays(); + $array[] = array(array()); + return $array; + } + public static function invalidAssocArrays() { return array( @@ -46,6 +53,14 @@ public function testValidAssocArraysReturnTrue($test) $this->assertTrue(IsAssocArray::test($test)); } + /** + * @dataProvider validAssocEmptyArrays + */ + public function testValidAssocEmptyArraysReturnTrue($test) + { + $this->assertTrue(isAssocArray::test($test, true)); + } + /** * @dataProvider invalidAssocArrays */ @@ -53,4 +68,12 @@ public function testInvalidAssocArraysReturnFalse($test) { $this->assertFalse(IsAssocArray::test($test)); } + + /** + * @dataProvider invalidAssocArrays + */ + public function testInvalidAssocEmptyArraysReturnFalse($test) + { + $this->assertFalse(IsAssocArray::test($test, true)); + } }