Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions src/Nerd/Common/Arrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@

namespace Nerd\Common\Arrays;

const USE_VALUE = 0;
const USE_KEY = 1;
const USE_BOTH = 2;
const TEST_VALUE = 0;
const TEST_KEY = 1;
const TEST_BOTH = 2;

/**
* Test items in array using $callable and return true
* if at least one of tests successful.
* Tests whether some element in the array passes the test
* implemented by the provided function.
*
* @param $array
* @param $callable
* @param int $option
* @return bool
*/
function any($array, $callable, $option = USE_VALUE)
function any($array, $callable, $option = TEST_VALUE)
{
foreach ($array as $key => $value) {
switch ($option) {
case USE_KEY:
case TEST_KEY:
$test = $callable($key);
break;
case USE_BOTH:
case TEST_BOTH:
$test = $callable($value, $key);
break;
case USE_VALUE:
case TEST_VALUE:
default:
$test = $callable($value);
}
Expand All @@ -37,25 +37,25 @@ function any($array, $callable, $option = USE_VALUE)
}

/**
* Test items in array using $callable and return true
* if all tests successful.
* Tests whether all elements in the array pass the test
* implemented by the provided function.
*
* @param $array
* @param $callable
* @param int $option
* @return bool
*/
function all($array, $callable, $option = USE_VALUE)
function all($array, $callable, $option = TEST_VALUE)
{
foreach ($array as $key => $value) {
switch ($option) {
case USE_KEY:
case TEST_KEY:
$test = $callable($key);
break;
case USE_BOTH:
case TEST_BOTH:
$test = $callable($value, $key);
break;
case USE_VALUE:
case TEST_VALUE:
default:
$test = $callable($value);
}
Expand All @@ -65,3 +65,14 @@ function all($array, $callable, $option = USE_VALUE)
}
return true;
}

/**
* Create array from variable list of arguments.
*
* @param array ...$items
* @return array
*/
function arrayOf(...$items)
{
return $items;
}
38 changes: 23 additions & 15 deletions tests/ArraysTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace tests;

use function Nerd\Common\Arrays\arrayOf;
use PHPUnit\Framework\TestCase;

use function Nerd\Common\Arrays\all;
use function Nerd\Common\Arrays\any;

use const Nerd\Common\Arrays\USE_KEY;
use const Nerd\Common\Arrays\USE_BOTH;
use const Nerd\Common\Arrays\TEST_KEY;
use const Nerd\Common\Arrays\TEST_BOTH;

class ArraysTest extends TestCase
{
Expand All @@ -27,19 +28,19 @@ public function testAllKey()
$test = function ($key) {
return strlen($key) == 2;
};
$this->assertFalse(all(["aa" => 15, "abc" => 10], $test, USE_KEY));
$this->assertTrue(all(["aa" => 155, "ab" => 10], $test, USE_KEY));
$this->assertTrue(all([], $test, USE_KEY));
$this->assertFalse(all(["aa" => 15, "abc" => 10], $test, TEST_KEY));
$this->assertTrue(all(["aa" => 155, "ab" => 10], $test, TEST_KEY));
$this->assertTrue(all([], $test, TEST_KEY));
}

public function testAllBoth()
{
$test = function ($value, $key) {
return strlen($key) == 2 && $value > 0;
};
$this->assertFalse(all(["a" => -10, "b" => 15, "ab" => 20], $test, USE_BOTH));
$this->assertTrue(all(["ab" => 15, "cc" => 20], $test, USE_BOTH));
$this->assertTrue(all([], $test, USE_BOTH));
$this->assertFalse(all(["a" => -10, "b" => 15, "ab" => 20], $test, TEST_BOTH));
$this->assertTrue(all(["ab" => 15, "cc" => 20], $test, TEST_BOTH));
$this->assertTrue(all([], $test, TEST_BOTH));
}

public function testAnyValue()
Expand All @@ -58,19 +59,26 @@ public function testAnyKey()
$test = function ($key) {
return strlen($key) == 2;
};
$this->assertTrue(any(["aa" => 15, "abc" => 10], $test, USE_KEY));
$this->assertFalse(any(["aaa" => 155, "abc" => 10], $test, USE_KEY));
$this->assertFalse(any([], $test, USE_KEY));
$this->assertTrue(any(["aa" => 15, "abc" => 10], $test, TEST_KEY));
$this->assertFalse(any(["aaa" => 155, "abc" => 10], $test, TEST_KEY));
$this->assertFalse(any([], $test, TEST_KEY));
}

public function testAnyBoth()
{
$test = function ($value, $key) {
return strlen($key) == 2 && $value > 0;
};
$this->assertTrue(any(["a" => -10, "b" => 15, "ab" => 20], $test, USE_BOTH));
$this->assertTrue(any(["ab" => 15, "cc" => 20], $test, USE_BOTH));
$this->assertFalse(any([], $test, USE_BOTH));
$this->assertFalse(any(["a" => 15, "ab" => -15], $test, USE_BOTH));
$this->assertTrue(any(["a" => -10, "b" => 15, "ab" => 20], $test, TEST_BOTH));
$this->assertTrue(any(["ab" => 15, "cc" => 20], $test, TEST_BOTH));
$this->assertFalse(any([], $test, TEST_BOTH));
$this->assertFalse(any(["a" => 15, "ab" => -15], $test, TEST_BOTH));
}

public function testArrayOf()
{
$this->assertEquals([1, 2, 3], arrayOf(1, 2, 3));
$this->assertEquals([1], arrayOf(1));
$this->assertEquals([], arrayOf());
}
}