This repository has been archived by the owner on Jan 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Stdlib class for testing if a value is an assoc array
- Loading branch information
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Zend\Stdlib; | ||
|
||
/** | ||
* Simple class for testing whether a value is an associative array | ||
* | ||
* Declared abstract, as we have no need for instantiation. | ||
*/ | ||
abstract class IsAssocArray | ||
{ | ||
/** | ||
* Test whether a value is an associative array | ||
* | ||
* We have an associative array if at least one key is a string. | ||
* | ||
* @param mixed $value | ||
* @return bool | ||
*/ | ||
public static function test($value) | ||
{ | ||
return (is_array($value) | ||
&& count(array_filter(array_keys($value), 'is_string')) > 0 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace ZendTest\Stdlib; | ||
|
||
use PHPUnit_Framework_TestCase as TestCase, | ||
stdClass, | ||
Zend\Stdlib\IsAssocArray; | ||
|
||
class IsAssocArrayTest extends TestCase | ||
{ | ||
public static function validAssocArrays() | ||
{ | ||
return array( | ||
array(array( | ||
'foo' => 'bar', | ||
)), | ||
array(array( | ||
'bar', | ||
'foo' => 'bar', | ||
'baz', | ||
)), | ||
); | ||
} | ||
|
||
public static function invalidAssocArrays() | ||
{ | ||
return array( | ||
array(null), | ||
array(true), | ||
array(false), | ||
array(0), | ||
array(1), | ||
array(0.0), | ||
array(1.0), | ||
array('string'), | ||
array(array(0, 1, 2)), | ||
array(new stdClass), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider validAssocArrays | ||
*/ | ||
public function testValidAssocArraysReturnTrue($test) | ||
{ | ||
$this->assertTrue(IsAssocArray::test($test)); | ||
} | ||
|
||
/** | ||
* @dataProvider invalidAssocArrays | ||
*/ | ||
public function testInvalidAssocArraysReturnFalse($test) | ||
{ | ||
$this->assertFalse(IsAssocArray::test($test)); | ||
} | ||
} |