-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e23a187
commit 07630d5
Showing
3 changed files
with
262 additions
and
18 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
/** | ||
* This file is part of the NeedleProject\Common package. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespace NeedleProject\Common\Convertor; | ||
|
||
/** | ||
* Class CompoundWordConvertor | ||
* | ||
* @package NeedleProject\Common\Convertor | ||
* @author Adrian Tilita <adrian@tilita.ro> | ||
* @copyright 2018 Adrian Tilita | ||
* @license https://opensource.org/licenses/MIT MIT Licence | ||
*/ | ||
class CompoundWordConvertor | ||
{ | ||
/** | ||
* Convert any string to camelCase | ||
* @param string $word | ||
* @return string | ||
*/ | ||
public static function convertToCamelCase($word) | ||
{ | ||
return lcfirst(self::convertToPascalCase($word)); | ||
} | ||
|
||
/** | ||
* Convert any string to PascalCase | ||
* @param string $word | ||
* @return string | ||
*/ | ||
public static function convertToPascalCase($word) | ||
{ | ||
// separate into block for easier readability | ||
$word = str_replace('_', ' ', $word); | ||
$word = strtolower($word); | ||
$word = ucwords($word); | ||
return str_replace(' ', '', $word); | ||
} | ||
|
||
/** | ||
* Convert any string to underscore_string (snake_case) | ||
* @param string $word | ||
* @return string | ||
*/ | ||
public static function convertToSnakeCase($word) | ||
{ | ||
// append an _ to all capital letters. Ex: Abc will be converted to _Abc | ||
$word = preg_replace('/(?<!^)[A-Z]/', '_$0', $word); | ||
// replace spaces to underscore | ||
$word = preg_replace('/\s/','_', $word); | ||
// replace multiple spaces to one underscore | ||
$word = preg_replace('/\s\s+/','_', $word); | ||
// replace multiple underscores to one underscore | ||
$word = preg_replace('/([_]+)/','_', $word); | ||
return strtolower($word); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
namespace NeedleProject\Common\Convertor; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class CompoundWordConvertorTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider provideCamelCaseScenarios | ||
* @param $input | ||
* @param $expectedOutput | ||
*/ | ||
public function testConvertToCamelCase($input, $expectedOutput) | ||
{ | ||
$this->assertEquals( | ||
$expectedOutput, | ||
CompoundWordConvertor::convertToCamelCase($input) | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider providePascalCaseScenarios | ||
* @param $input | ||
* @param $expectedOutput | ||
*/ | ||
public function testConvertToPascalCase($input, $expectedOutput) | ||
{ | ||
$this->assertEquals( | ||
$expectedOutput, | ||
CompoundWordConvertor::convertToPascalCase($input) | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider provideSnakeCaseScenarios | ||
* @param $input | ||
* @param $expectedOutput | ||
*/ | ||
public function testConvertToSnakeCase($input, $expectedOutput) | ||
{ | ||
$this->assertEquals( | ||
$expectedOutput, | ||
CompoundWordConvertor::convertToSnakeCase($input) | ||
); | ||
} | ||
|
||
public function provideCamelCaseScenarios() | ||
{ | ||
return [ | ||
['foo_bar', 'fooBar'], | ||
['foo_a', 'fooA'], | ||
['FOOO', 'fooo'], | ||
['fOoOoOo BaRrRr', 'fooooooBarrrr'], | ||
['Hello World', 'helloWorld'] | ||
// @needs a fix: ['fooBar', 'fooBar'] | ||
]; | ||
} | ||
|
||
public function providePascalCaseScenarios() | ||
{ | ||
return [ | ||
['foo_bar', 'FooBar'], | ||
['foo_a', 'FooA'], | ||
['FOOO', 'Fooo'], | ||
['fOoOoOo BaRrRr', 'FooooooBarrrr'], | ||
['Hello World', 'HelloWorld'] | ||
// @needs a fix: ['FooBar', 'FooBar'] | ||
]; | ||
} | ||
|
||
public function provideSnakeCaseScenarios() | ||
{ | ||
return [ | ||
['FooBar', 'foo_bar'], | ||
['fooBarBaz', 'foo_bar_baz'], | ||
['foo_bar', 'foo_bar'], | ||
['Hello World', 'hello_world'] | ||
// @needs a fix: ['FOO BAR', 'foo_bar'], | ||
// @needs a fix: ['SimpleXML', 'simple_xml'] | ||
]; | ||
} | ||
} |