From 4b8c136d6fbf3f2cbc1f37c579970430b1f90cd2 Mon Sep 17 00:00:00 2001 From: m1 Date: Thu, 8 Oct 2015 17:47:15 +0100 Subject: [PATCH] Add isAssoc() and isNumeric() methods with tests In order to check whether array associative or numeric --- README.md | 16 +++++++++++++++ src/AbstractArray.php | 40 ++++++++++++++++++++++++++++++++++++ tests/AbstractArrayTest.php | 19 +++++++++-------- tests/ImmutableArrayTest.php | 20 ++++++++++++++++++ tests/MutableArrayTest.php | 20 ++++++++++++++++++ 5 files changed, 107 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 5dba619..4f77efa 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,8 @@ $a = $a->shuffle(); // override instance you operates on, because $a !== $a->shu * [getRandomValues](#getrandomvalues) * [getValues](#getvalues) * [indexOf](#indexof) + * [isAssoc](#isassoc) + * [isNumeric](#isnumeric) * [isEmpty](#isempty) * [key](#key) * [last](#last) @@ -494,6 +496,20 @@ $a = A::create(['a', 'b', 'c']); $a->indexOf('b'); // 1 ``` +### isAssoc + +``` php +$a = A::create(['key' => 'value']); +$a->isAssoc(); // true +``` + +### isNumeric + +``` php +$a = A::create(['a', 'b', 'c']); +$a->isNumeric(); // true +``` + ### isEmpty ``` php diff --git a/src/AbstractArray.php b/src/AbstractArray.php index 379f56b..c664085 100644 --- a/src/AbstractArray.php +++ b/src/AbstractArray.php @@ -356,6 +356,46 @@ public function isEmpty() return !$this->elements; } + /** + * Check whether array is associative or not + * + * @return bool Returns true if associative, false otherwise + */ + public function isAssoc() + { + if ($this->isEmpty()) { + return false; + } + + foreach ($this->getKeys() as $key) { + if (!is_string($key)) { + return false; + } + } + + return true; + } + + /** + * Check whether array is numeric or not + * + * @return bool Returns true if numeric, false otherwise + */ + public function isNumeric() + { + if ($this->isEmpty()) { + return false; + } + + foreach ($this->getKeys() as $key) { + if (!is_int($key)) { + return false; + } + } + + return true; + } + /** * Check if the given value exists in array * diff --git a/tests/AbstractArrayTest.php b/tests/AbstractArrayTest.php index 64cebbc..2121de2 100644 --- a/tests/AbstractArrayTest.php +++ b/tests/AbstractArrayTest.php @@ -10,34 +10,37 @@ abstract class AbstractArrayTest extends PHPUnit_Framework_TestCase public function simpleArrayProvider() { return [ - // empty array + 'empty_array' => [ [], + 'empty' ], - // indexed array + 'indexed_array' => [ [ 1 => 'one', 2 => 'two', 3 => 'three', - ] + ], + 'numeric' ], - // assoc array + 'assoc_array' => [ [ 'one' => 1, 'two' => 2, 'three' => 3, - ] + ], + 'assoc' ], - // mixed array + 'mixed_array' => [ [ 1 => 'one', 'two' => 2, 3 => 'three', - ] - + ], + 'mixed' ], ]; } diff --git a/tests/ImmutableArrayTest.php b/tests/ImmutableArrayTest.php index 2565715..40b5ba6 100644 --- a/tests/ImmutableArrayTest.php +++ b/tests/ImmutableArrayTest.php @@ -821,6 +821,26 @@ public function testIsEmpty(array $array) $this->assertTrue($isEmpty === $ma->isEmpty()); } + /** + * @dataProvider simpleArrayProvider + */ + public function testIsAssoc(array $array, $type = false) + { + $ma = new ImmutableArray($array); + + $this->assertTrue(($type === 'assoc') === $ma->isAssoc()); + } + + /** + * @dataProvider simpleArrayProvider + */ + public function testIsNumeric(array $array, $type = false) + { + $ma = new ImmutableArray($array); + + $this->assertTrue(($type === 'numeric') === $ma->isNumeric()); + } + /** * @dataProvider simpleArrayProvider */ diff --git a/tests/MutableArrayTest.php b/tests/MutableArrayTest.php index 61462f3..52d20bb 100644 --- a/tests/MutableArrayTest.php +++ b/tests/MutableArrayTest.php @@ -862,6 +862,26 @@ public function testIsEmpty(array $array) $this->assertTrue($isEmpty === $ma->isEmpty()); } + /** + * @dataProvider simpleArrayProvider + */ + public function testIsAssoc(array $array, $type = false) + { + $ma = new MutableArray($array); + + $this->assertTrue(($type === 'assoc') === $ma->isAssoc()); + } + + /** + * @dataProvider simpleArrayProvider + */ + public function testIsNumeric(array $array, $type = false) + { + $ma = new MutableArray($array); + + $this->assertTrue(($type === 'numeric') === $ma->isNumeric()); + } + /** * @dataProvider simpleArrayProvider */