Skip to content

Commit

Permalink
Merge 4d216b0 into 1db9373
Browse files Browse the repository at this point in the history
  • Loading branch information
chadicus committed Jan 4, 2016
2 parents 1db9373 + 4d216b0 commit 1a9f2e7
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/Arrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
*/
final class Arrays
{
const CASE_LOWER = 1;
const CASE_UPPER = 2;
const CASE_CAMEL_CAPS = 4;
const CASE_UNDERSCORE = 8;

/**
* Simply returns an array value if the key exist or null if it does not.
*
Expand Down Expand Up @@ -411,4 +416,48 @@ public static function nullifyEmptyStrings(array &$array)
}
}
}

/**
* Changes the case of all keys in an array.
*
* @param array $input The array to work on.
* @param integer $case The case to which the keys should be set.
*
* @return array Returns an array with its keys case changed.
*/
public static function changeKeyCase(array $input, $case = self::CASE_LOWER)
{
if ($case & self::CASE_UNDERSCORE) {
$copy = [];
array_walk(
$input,
function ($value, $key) use (&$copy) {
$copy[preg_replace("/([a-z])([A-Z0-9])/", '$1_$2', $key)] = $value;
}
);
$input = $copy;
}

if ($case & self::CASE_CAMEL_CAPS) {
$copy = [];
array_walk(
$input,
function ($value, $key) use (&$copy) {
$key = lcfirst(str_replace(' ', '', ucwords(strtolower(str_replace('_', ' ', $key)))));
$copy[$key] = $value;
}
);
$input = $copy;
}

if ($case & self::CASE_UPPER) {
$input = array_change_key_case($input, \CASE_UPPER);
}

if ($case & self::CASE_LOWER) {
$input = array_change_key_case($input, \CASE_LOWER);
}

return $input;
}
}
68 changes: 68 additions & 0 deletions tests/ArraysTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -787,4 +787,72 @@ public function nullifyEmptyStringsEmptyArray()
A::nullifyEmptyStrings($array);
$this->assertSame([], $array);
}

/**
* Verify functionality of changeKeyCase().
*
* @test
* @covers ::changeKeyCase
* @dataProvider changeKeyCaseData
*
* @return void
*/
public function changeKeyCase($input, $case, $expected)
{
$this->assertSame($expected, A::changeKeyCase($input, $case));
}

/**
* Dataprovider for changeKeyCase test.
*
* @return array
*/
public function changeKeyCaseData()
{
$lowerUnderscore = [
'first_and_last_name' => 'John Doe',
'email_address' => 'john@example.com',
'age' => 35,
];

$upperUnderscore = [
'FIRST_AND_LAST_NAME' => 'John Doe',
'EMAIL_ADDRESS' => 'john@example.com',
'AGE' => 35,
];

$camelCaps = [
'firstAndLastName' => 'John Doe',
'emailAddress' => 'john@example.com',
'age' => 35,
];

$underscore = [
'first_And_Last_Name' => 'John Doe',
'email_Address' => 'john@example.com',
'age' => 35,
];

$lower = [
'firstandlastname' => 'John Doe',
'emailaddress' => 'john@example.com',
'age' => 35,
];

$upper = [
'FIRSTANDLASTNAME' => 'John Doe',
'EMAILADDRESS' => 'john@example.com',
'AGE' => 35,
];

return [
'upper to lower' => [$upper, A::CASE_LOWER, $lower],
'lower to upper' => [$lower, A::CASE_UPPER, $upper],
'underscore to camel' => [$lowerUnderscore, A::CASE_CAMEL_CAPS, $camelCaps],
'camel to underscore' => [$camelCaps, A::CASE_UNDERSCORE, $underscore],
'camel to upper underscore' => [$camelCaps, A::CASE_UNDERSCORE | A::CASE_UPPER, $upperUnderscore],
'camel to lower underscore' => [$camelCaps, A::CASE_UNDERSCORE | A::CASE_LOWER, $lowerUnderscore],
'lower underscore to upper camel' => [$lowerUnderscore, A::CASE_CAMEL_CAPS | A::CASE_UPPER, $upper],
];
}
}

0 comments on commit 1a9f2e7

Please sign in to comment.