From 71824136a8eeaccff0671d39f2d15504cf42a196 Mon Sep 17 00:00:00 2001 From: chadicus Date: Sat, 13 Sep 2014 23:19:44 -0400 Subject: [PATCH 1/2] Add Util\Arrays::unsetAll --- src/Util/Arrays.php | 15 +++++++++++++++ tests/Util/ArraysTest.php | 13 +++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/Util/Arrays.php b/src/Util/Arrays.php index 1cdf94a..65cb5f9 100644 --- a/src/Util/Arrays.php +++ b/src/Util/Arrays.php @@ -371,4 +371,19 @@ public static function partition(array $input, $partitionCount, $preserveKeys = return $partitions; } + + /** + * Unsets all elements in the given $array specified by $keys + * + * @param array $array The array containing the elements to unset. + * @param array $keys Array of keys to unset. + * + * @return void + */ + public static function unsetAll(array &$array, array $keys) + { + foreach ($keys as $key) { + unset($array[$key]); + } + } } diff --git a/tests/Util/ArraysTest.php b/tests/Util/ArraysTest.php index 377100f..a0a2bcd 100644 --- a/tests/Util/ArraysTest.php +++ b/tests/Util/ArraysTest.php @@ -707,4 +707,17 @@ public function partition_nonBoolPreserveKeys() { A::partition(['a', 'b', 'c'], 3, 'not a bool'); } + + /** + * Verify basic behavior of unsetAll. + * + * @test + * @covers ::unsetAll + */ + public function unsetAll() + { + $array = ['a', 'b', 'c']; + A::unsetAll($array, [0, 2]); + $this->assertSame([1 => 'b'], $array); + } } From a7801c4fc21af55371c882cc6d9a6841642c7694 Mon Sep 17 00:00:00 2001 From: chadicus Date: Sat, 13 Sep 2014 23:23:12 -0400 Subject: [PATCH 2/2] Add Util\Arrays::nulifyEmptyStrings --- src/Util/Arrays.php | 23 ++++++++++++++++++++++- tests/Util/ArraysTest.php | 14 +++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/Util/Arrays.php b/src/Util/Arrays.php index 65cb5f9..60b8801 100644 --- a/src/Util/Arrays.php +++ b/src/Util/Arrays.php @@ -375,7 +375,7 @@ public static function partition(array $input, $partitionCount, $preserveKeys = /** * Unsets all elements in the given $array specified by $keys * - * @param array $array The array containing the elements to unset. + * @param array &$array The array containing the elements to unset. * @param array $keys Array of keys to unset. * * @return void @@ -386,4 +386,25 @@ public static function unsetAll(array &$array, array $keys) unset($array[$key]); } } + + /** + * Convert all empty strings to null in the given array + * + * @param array &$array The array containing empty strings + * + * @return void + */ + public static function nullifyEmptyStrings(array &$array) + { + $arrayCopy = $array; + foreach ($arrayCopy as $key => $value) { + if (!is_string($value)) { + continue; + } + + if (trim($value) === '') { + $array[$key] = null; + } + } + } } diff --git a/tests/Util/ArraysTest.php b/tests/Util/ArraysTest.php index a0a2bcd..914a36d 100644 --- a/tests/Util/ArraysTest.php +++ b/tests/Util/ArraysTest.php @@ -709,7 +709,7 @@ public function partition_nonBoolPreserveKeys() } /** - * Verify basic behavior of unsetAll. + * Verify basic behavior of unsetAll(). * * @test * @covers ::unsetAll @@ -720,4 +720,16 @@ public function unsetAll() A::unsetAll($array, [0, 2]); $this->assertSame([1 => 'b'], $array); } + + /** + * Verify basic behavior of nullifyEmptyStrings(). + * @test + * @covers ::nullifyEmptyStrings + */ + public function nullifyEmptyStrings() + { + $array = ['a' => '', 'b' => true, 'c' => "\n\t", 'd' => "\tstring with whitespace\n"]; + A::nullifyEmptyStrings($array); + $this->assertSame(['a' => null, 'b' => true, 'c' => null, 'd' => "\tstring with whitespace\n"], $array); + } }