This repository was archived by the owner on Jan 31, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +82
-0
lines changed Expand file tree Collapse file tree 2 files changed +82
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Zend \Stdlib ;
4+
5+ /**
6+ * Simple class for testing whether a value is an associative array
7+ *
8+ * Declared abstract, as we have no need for instantiation.
9+ */
10+ abstract class IsAssocArray
11+ {
12+ /**
13+ * Test whether a value is an associative array
14+ *
15+ * We have an associative array if at least one key is a string.
16+ *
17+ * @param mixed $value
18+ * @return bool
19+ */
20+ public static function test ($ value )
21+ {
22+ return (is_array ($ value )
23+ && count (array_filter (array_keys ($ value ), 'is_string ' )) > 0
24+ );
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace ZendTest \Stdlib ;
4+
5+ use PHPUnit_Framework_TestCase as TestCase ,
6+ stdClass ,
7+ Zend \Stdlib \IsAssocArray ;
8+
9+ class IsAssocArrayTest extends TestCase
10+ {
11+ public static function validAssocArrays ()
12+ {
13+ return array (
14+ array (array (
15+ 'foo ' => 'bar ' ,
16+ )),
17+ array (array (
18+ 'bar ' ,
19+ 'foo ' => 'bar ' ,
20+ 'baz ' ,
21+ )),
22+ );
23+ }
24+
25+ public static function invalidAssocArrays ()
26+ {
27+ return array (
28+ array (null ),
29+ array (true ),
30+ array (false ),
31+ array (0 ),
32+ array (1 ),
33+ array (0.0 ),
34+ array (1.0 ),
35+ array ('string ' ),
36+ array (array (0 , 1 , 2 )),
37+ array (new stdClass ),
38+ );
39+ }
40+
41+ /**
42+ * @dataProvider validAssocArrays
43+ */
44+ public function testValidAssocArraysReturnTrue ($ test )
45+ {
46+ $ this ->assertTrue (IsAssocArray::test ($ test ));
47+ }
48+
49+ /**
50+ * @dataProvider invalidAssocArrays
51+ */
52+ public function testInvalidAssocArraysReturnFalse ($ test )
53+ {
54+ $ this ->assertFalse (IsAssocArray::test ($ test ));
55+ }
56+ }
You can’t perform that action at this time.
0 commit comments