Skip to content

Commit

Permalink
Add isAssoc() and isNumeric() methods with tests
Browse files Browse the repository at this point in the history
In order to check whether array associative or numeric
  • Loading branch information
m1 authored and bocharsky-bw committed Oct 9, 2015
1 parent f28992f commit 4b8c136
Showing 5 changed files with 107 additions and 8 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
40 changes: 40 additions & 0 deletions src/AbstractArray.php
Original file line number Diff line number Diff line change
@@ -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
*
19 changes: 11 additions & 8 deletions tests/AbstractArrayTest.php
Original file line number Diff line number Diff line change
@@ -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'
],
];
}
20 changes: 20 additions & 0 deletions tests/ImmutableArrayTest.php
Original file line number Diff line number Diff line change
@@ -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
*/
20 changes: 20 additions & 0 deletions tests/MutableArrayTest.php
Original file line number Diff line number Diff line change
@@ -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
*/

0 comments on commit 4b8c136

Please sign in to comment.