Skip to content

Commit

Permalink
Merge pull request traderinteractive#43 from chadicus/master
Browse files Browse the repository at this point in the history
unsetAll(), nullifyEmptyStrings

unsetAll is a convenience method that will remove all elements specified by the $keys array.

nullifyWhiteSpace will convert all strings containing only whitespace to null. Usefull for db inserts when you want to insert null and not an empty string.
  • Loading branch information
raybot committed Sep 30, 2014
2 parents 3eb8bd6 + 1577199 commit a4d4c5f
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Util/Arrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,4 +371,35 @@ 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 or strings that contain only whitespace to null in the given array
*
* @param array &$array The array containing empty strings
*
* @return void
*/
public static function nullifyEmptyStrings(array &$array)
{
foreach ($array as &$value) {
if (is_string($value) && trim($value) === '') {
$value = null;
}
}
}
}
79 changes: 79 additions & 0 deletions tests/Util/ArraysTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -707,4 +707,83 @@ 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 behavior of unsetAll() with empty array.
*
* @test
* @covers ::unsetAll
*/
public function unsetAll_emptyArray()
{
$array = [];
A::unsetAll($array, [0, 2]);
// array unchanged
$this->assertSame([], $array);
}

/**
* Verify behavior of unsetAll() with empty keys.
*
* @test
* @covers ::unsetAll
*/
public function unsetAll_emptyKeys()
{
$array = ['a', 'b', 'c'];
A::unsetAll($array, []);
// array unchanged
$this->assertSame(['a', 'b', 'c'], $array);
}

/**
* Verify behavior of unsetAll() with keys that don't exist
*
* @test
* @covers ::unsetAll
*/
public function unsetAll_keyNotFound()
{
$array = ['a', 'b', 'c'];
A::unsetAll($array, [3, 4]);
// array unchanged
$this->assertSame(['a', 'b', 'c'], $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);
}

/**
* Verify behavior of nullifyEmptyStrings() with empty input.
* @test
* @covers ::nullifyEmptyStrings
*/
public function nullifyEmptyStrings_emptyArray()
{
$array = [];
A::nullifyEmptyStrings($array);
$this->assertSame([], $array);
}
}

0 comments on commit a4d4c5f

Please sign in to comment.