Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/zend-view-empty-array' of https://github.com/mwi…
Browse files Browse the repository at this point in the history
…llbanks/zf2 into hotfix/empty-assoc-array
  • Loading branch information
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/IsAssocArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
23 changes: 23 additions & 0 deletions test/IsAssocArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -46,11 +53,27 @@ public function testValidAssocArraysReturnTrue($test)
$this->assertTrue(IsAssocArray::test($test));
}

/**
* @dataProvider validAssocEmptyArrays
*/
public function testValidAssocEmptyArraysReturnTrue($test)
{
$this->assertTrue(isAssocArray::test($test, true));
}

/**
* @dataProvider invalidAssocArrays
*/
public function testInvalidAssocArraysReturnFalse($test)
{
$this->assertFalse(IsAssocArray::test($test));
}

/**
* @dataProvider invalidAssocArrays
*/
public function testInvalidAssocEmptyArraysReturnFalse($test)
{
$this->assertFalse(IsAssocArray::test($test, true));
}
}

0 comments on commit 8e514a8

Please sign in to comment.