Last active
August 29, 2015 13:59
-
-
Save rr-/10522533 to your computer and use it in GitHub Desktop.
Convert text between popular capitalization standards
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class TextCaseConverter | |
{ | |
const SNAKE_CASE = 1; //snake_case | |
const SPINAL_CASE = 2; //spinal-case | |
const TRAIN_CASE = 3; //Train-Case | |
const UPPER_CAMEL_CASE = 5; //CamelCase | |
const LOWER_CAMEL_CASE = 6; //camelCase | |
const CAMEL_CASE = self::UPPER_CAMEL_CASE; //CamelCase | |
const BLANK_CASE = 7; //blank case | |
public static function convert($text, $from, $to) | |
{ | |
switch ($from) | |
{ | |
case self::LOWER_CAMEL_CASE: | |
case self::UPPER_CAMEL_CASE: | |
$trans = preg_split('/(^[^A-Z]+|[A-Z][^A-Z]+)/', $text, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); | |
break; | |
case self::SNAKE_CASE: | |
$trans = explode('_', $text); | |
break; | |
case self::TRAIN_CASE: | |
case self::SPINAL_CASE: | |
$trans = explode('-', $text); | |
break; | |
case self::BLANK_CASE: | |
$trans = preg_split('/\s+/', $text); | |
break; | |
default: | |
throw new Exception('Unknown conversion source: ' . $from); | |
} | |
$trans = array_map('trim', $trans); | |
$trans = array_filter($trans); | |
switch ($to) | |
{ | |
case self::SNAKE_CASE: | |
return join('_', array_map('strtolower', $trans)); | |
case self::SPINAL_CASE: | |
return join('-', array_map('strtolower', $trans)); | |
case self::TRAIN_CASE: | |
return join('-', array_map('ucfirst', array_map('strtolower', $trans))); | |
case self::UPPER_CAMEL_CASE: | |
return join('', array_map('ucfirst', array_map('strtolower', $trans))); | |
case self::LOWER_CAMEL_CASE: | |
return lcfirst(join('', array_map('ucfirst', array_map('strtolower', $trans)))); | |
case self::BLANK_CASE: | |
return join(' ', array_map('strtolower', $trans)); | |
default: | |
throw new Exception('Unknown conversion target: ' . $to); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include_once 'TextCaseConverter.php'; | |
$tests = | |
[ | |
['TestTest', 'test test', TextCaseConverter::BLANK_CASE, TextCaseConverter::CAMEL_CASE], | |
['TestTest', 'test test', TextCaseConverter::BLANK_CASE, TextCaseConverter::UPPER_CAMEL_CASE], | |
['testTest', 'test test', TextCaseConverter::BLANK_CASE, TextCaseConverter::LOWER_CAMEL_CASE], | |
['test_test', 'test test', TextCaseConverter::BLANK_CASE, TextCaseConverter::SNAKE_CASE], | |
['test-test', 'test test', TextCaseConverter::BLANK_CASE, TextCaseConverter::SPINAL_CASE], | |
['Test-Test', 'test test', TextCaseConverter::BLANK_CASE, TextCaseConverter::TRAIN_CASE], | |
['test test', 'test test', TextCaseConverter::BLANK_CASE, TextCaseConverter::BLANK_CASE], | |
['TestTest', 'TEST TEST', TextCaseConverter::BLANK_CASE, TextCaseConverter::CAMEL_CASE], | |
['TestTest', 'TEST TEST', TextCaseConverter::BLANK_CASE, TextCaseConverter::UPPER_CAMEL_CASE], | |
['testTest', 'TEST TEST', TextCaseConverter::BLANK_CASE, TextCaseConverter::LOWER_CAMEL_CASE], | |
['test_test', 'TEST TEST', TextCaseConverter::BLANK_CASE, TextCaseConverter::SNAKE_CASE], | |
['test-test', 'TEST TEST', TextCaseConverter::BLANK_CASE, TextCaseConverter::SPINAL_CASE], | |
['Test-Test', 'TEST TEST', TextCaseConverter::BLANK_CASE, TextCaseConverter::TRAIN_CASE], | |
['test test', 'TEST TEST', TextCaseConverter::BLANK_CASE, TextCaseConverter::BLANK_CASE], | |
['test test test', 'TestTESTTest', TextCaseConverter::CAMEL_CASE, TextCaseConverter::BLANK_CASE], | |
['test test test', 'testTESTTest', TextCaseConverter::CAMEL_CASE, TextCaseConverter::BLANK_CASE], | |
['test test', 'test_test', TextCaseConverter::SNAKE_CASE, TextCaseConverter::BLANK_CASE], | |
['test test', 'test-test', TextCaseConverter::SPINAL_CASE, TextCaseConverter::BLANK_CASE], | |
['test test', 'test-test', TextCaseConverter::TRAIN_CASE, TextCaseConverter::BLANK_CASE], | |
['test test', 'test test', TextCaseConverter::BLANK_CASE, TextCaseConverter::BLANK_CASE], | |
['test test', "test\t\ttest", TextCaseConverter::BLANK_CASE, TextCaseConverter::BLANK_CASE], | |
['test', '__test__', TextCaseConverter::SNAKE_CASE, TextCaseConverter::BLANK_CASE], | |
['test', '--test--', TextCaseConverter::SPINAL_CASE, TextCaseConverter::BLANK_CASE], | |
['test', '--test--', TextCaseConverter::TRAIN_CASE, TextCaseConverter::BLANK_CASE], | |
['test', " test \t", TextCaseConverter::BLANK_CASE, TextCaseConverter::BLANK_CASE], | |
['not really camel case', 'Not Really CamelCase', TextCaseConverter::CAMEL_CASE, TextCaseConverter::BLANK_CASE], | |
]; | |
foreach ($tests as $testCase) | |
{ | |
list ($expectedOutput, $input, $from, $to) = $testCase; | |
$output = TextCaseConverter::convert($input, $from, $to); | |
if ($output != $expectedOutput) | |
{ | |
echo 'convert(' . $input . ', ' . $from . ', ' . $to . ') == "' . $output . '" != "' . $expectedOutput . '"' . PHP_EOL; | |
exit(1); | |
} | |
} | |
echo 'ok' . PHP_EOL; | |
exit(0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment