From 32297d93ddf0aea7c4e10fab53c85a2d4d2cb88a Mon Sep 17 00:00:00 2001 From: Molkobain Date: Tue, 13 Dec 2022 11:40:51 +0100 Subject: [PATCH] =?UTF-8?q?N=C2=B05655=20-=20Add=20utils::ToSnakeCase()=20?= =?UTF-8?q?method?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/utils.inc.php | 25 ++++++++++--- test/application/UtilsTest.php | 65 ++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 4 deletions(-) diff --git a/application/utils.inc.php b/application/utils.inc.php index 4331e8b92..22d5dd225 100644 --- a/application/utils.inc.php +++ b/application/utils.inc.php @@ -3045,18 +3045,35 @@ HTML; } /** - * Transform a snake_case $sInput into a CamelCase string - * * @param string $sInput * - * @return string + * @return string Camel case representation of $sInput (eg. "something_new" becomes "SomethingNew") * @since 2.7.0 */ - public static function ToCamelCase($sInput) + public static function ToCamelCase($sInput): string { return str_replace(' ', '', ucwords(strtr($sInput, '_-', ' '))); } + /** + * @param string $sInput + * + * @return string Snake case representation of $sInput (eg. "SomethingNew" becomes "something_new") + * @since 3.1.0 + * @link https://stackoverflow.com/a/19533226/2710325 + */ + public static function ToSnakeCase(string $sInput): string + { + // Remove special chars to join words + $sOutput = preg_replace('/(\W)/', '_', $sInput); + // Transform camel case words to snake case + $sOutput = preg_replace('/[A-Z]([A-Z](?![a-z]))*/', '_$0', $sOutput); + // Lowercase everything + $sOutput = mb_strtolower($sOutput); + // Trim outer underscores + return trim($sOutput, '_'); + } + /** * @param string $sInput * diff --git a/test/application/UtilsTest.php b/test/application/UtilsTest.php index d0e660cdf..dca63c485 100644 --- a/test/application/UtilsTest.php +++ b/test/application/UtilsTest.php @@ -469,6 +469,71 @@ class UtilsTest extends ItopTestCase ]; } + /** + * @dataProvider ToSnakeCaseProvider + * @covers utils::ToSnakeCase + * + * @param string $sInput + * @param string $sExpectedOutput + * + * @return void + */ + public function testToSnakeCase(string $sInput, string $sExpectedOutput) + { + $sTestedOutput = utils::ToSnakeCase($sInput); + $this->assertEquals($sExpectedOutput, $sTestedOutput, "Snake case transformation for '$sInput' doesn't match. Got '$sTestedOutput', expected '$sExpectedOutput'."); + } + + /** + * @since 3.1.0 + * @return \string[][] + */ + public function ToSnakeCaseProvider(): array + { + return [ + 'One word lowercase' => [ + 'hello', + 'hello', + ], + 'One word uppercase' => [ + 'HELLO', + 'hello', + ], + 'One word capitalize' => [ + 'Hello', + 'hello', + ], + 'Two words separated with space' => [ + 'hello world', + 'hello_world', + ], + 'Two words separated with underscore' => [ + 'hello_world', + 'hello_world', + ], + 'Two words separated with dash' => [ + 'hello-world', + 'hello_world', + ], + 'Two words separated with dot' => [ + 'hello.world', + 'hello_world', + ], + 'Two words camel cased' => [ + 'HelloWorld', + 'hello_world', + ], + 'Two words camel cased with acronym' => [ + 'HTMLWorld', + 'html_world', + ], + 'Three words separated with underscore and space' => [ + 'hello_there world', + 'hello_there_world', + ], + ]; + } + /** * @dataProvider ToAcronymProvider * @covers utils::ToAcronym