diff --git a/src/Nerd/Common/Arrays.php b/src/Nerd/Common/Arrays.php index 8c350c4..c70d4a7 100644 --- a/src/Nerd/Common/Arrays.php +++ b/src/Nerd/Common/Arrays.php @@ -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); } @@ -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); } @@ -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; +} diff --git a/tests/ArraysTest.php b/tests/ArraysTest.php index 1af0fcf..e9bba6c 100644 --- a/tests/ArraysTest.php +++ b/tests/ArraysTest.php @@ -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 { @@ -27,9 +28,9 @@ 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() @@ -37,9 +38,9 @@ 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() @@ -58,9 +59,9 @@ 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() @@ -68,9 +69,16 @@ 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()); } }