From 5ccaf8f4eb337df398ef6d66fa518d90aea31e48 Mon Sep 17 00:00:00 2001 From: Roman Lakhtadyr Date: Sat, 15 Oct 2016 18:12:30 +0300 Subject: [PATCH 1/2] rename constants, fix comments --- src/Nerd/Common/Arrays.php | 30 +++++++++++++++--------------- tests/ArraysTest.php | 30 +++++++++++++++--------------- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/Nerd/Common/Arrays.php b/src/Nerd/Common/Arrays.php index 8c350c4..8800c17 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); } diff --git a/tests/ArraysTest.php b/tests/ArraysTest.php index 1af0fcf..2f353f4 100644 --- a/tests/ArraysTest.php +++ b/tests/ArraysTest.php @@ -7,8 +7,8 @@ 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 +27,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 +37,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 +58,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 +68,9 @@ 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)); } } From cecf30a6143b346220923bed8d4a25f9b63ff9d3 Mon Sep 17 00:00:00 2001 From: Roman Lakhtadyr Date: Sat, 15 Oct 2016 18:18:22 +0300 Subject: [PATCH 2/2] add arrayOf function --- src/Nerd/Common/Arrays.php | 11 +++++++++++ tests/ArraysTest.php | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/src/Nerd/Common/Arrays.php b/src/Nerd/Common/Arrays.php index 8800c17..c70d4a7 100644 --- a/src/Nerd/Common/Arrays.php +++ b/src/Nerd/Common/Arrays.php @@ -65,3 +65,14 @@ function all($array, $callable, $option = TEST_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 2f353f4..e9bba6c 100644 --- a/tests/ArraysTest.php +++ b/tests/ArraysTest.php @@ -2,6 +2,7 @@ namespace tests; +use function Nerd\Common\Arrays\arrayOf; use PHPUnit\Framework\TestCase; use function Nerd\Common\Arrays\all; @@ -73,4 +74,11 @@ public function testAnyBoth() $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()); + } }