Skip to content

Commit

Permalink
Merge a7801c4 into 3eb8bd6
Browse files Browse the repository at this point in the history
  • Loading branch information
chadicus committed Sep 14, 2014
2 parents 3eb8bd6 + a7801c4 commit 2b08830
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Util/Arrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,4 +371,40 @@ 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]);
}
}

/**
* 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;
}
}
}
}
25 changes: 25 additions & 0 deletions tests/Util/ArraysTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -707,4 +707,29 @@ 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);
}

/**
* 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);
}
}

0 comments on commit 2b08830

Please sign in to comment.