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

Commit

Permalink
Added Stdlib class for testing if a value is an assoc array
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/IsAssocArray.php
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
);
}
}
56 changes: 56 additions & 0 deletions test/IsAssocArrayTest.php
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));
}
}

0 comments on commit 656dbe5

Please sign in to comment.