Skip to content

Commit

Permalink
Merge 3c59f54 into bd9114b
Browse files Browse the repository at this point in the history
  • Loading branch information
chadicus committed Apr 6, 2016
2 parents bd9114b + 3c59f54 commit 0c9b272
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
71 changes: 71 additions & 0 deletions src/Arrays.php
Expand Up @@ -10,6 +10,34 @@
*/
final class Arrays
{
/**
* Const for lower cased array keys.
*
* @const integer
*/
const CASE_LOWER = 1;

/**
* Const for upper cased array keys.
*
* @const integer
*/
const CASE_UPPER = 2;

/**
* Const for camel caps cased array keys.
*
* @const integer
*/
const CASE_CAMEL_CAPS = 4;

/**
* Const for underscored cased array keys.
*
* @const integer
*/
const CASE_UNDERSCORE = 8;

/**
* Simply returns an array value if the key exist or null if it does not.
*
Expand Down Expand Up @@ -456,4 +484,47 @@ final public static function getNested(array $array, $delimitedKey, $delimiter =

return $pointer;
}

/**
* Changes the case of all keys in an array. Numbered indices are left as is.
*
* @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 = [];
foreach ($input as $key => $value) {
$copy[preg_replace("/([a-z])([A-Z0-9])/", '$1_$2', $key)] = $value;
}

$input = $copy;
}

if ($case & self::CASE_CAMEL_CAPS) {
$copy = [];
foreach ($input as $key => $value) {
$key = implode(' ', array_filter(preg_split('/[^a-z0-9]/i', $key)));
$key = lcfirst(str_replace(' ', '', ucwords(strtolower($key))));
$copy[$key] = $value;
}

$input = $copy;
}

unset($copy); //gc

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
Expand Up @@ -815,4 +815,72 @@ public function getNestedPathNotFound()
$array = ['db' => ['host' => 'localhost', 'login' => [ 'username' => 'scott', 'password' => 'tiger']]];
$this->assertNull(A::getNested($array, 'db.notfound.username'));
}

/**
* 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 0c9b272

Please sign in to comment.