From 6ead89f11059a5ba5f90d9741a2e4549c6fa3b30 Mon Sep 17 00:00:00 2001 From: Alexey Rogachev Date: Tue, 19 Sep 2023 20:30:58 +0600 Subject: [PATCH] Add `$strict` parameter to `StringHelper::toSnakeCase() method (#113) Co-authored-by: Sergei Predvoditelev --- CHANGELOG.md | 1 + src/Inflector.php | 5 +++-- tests/InflectorTest.php | 27 +++++++++++++++++++-------- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 61ae35b..3cd9f18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - New #104: Add methods `StringHelper::trim()`, `StringHelper::ltrim()`, `StringHelper::rtrim()` (@olegbaturin) - Enh #83: Make minor refactoring with Rector help (@vjik) - Enh #111: Minor refactoring (@Tigrov) +- Enh #92: Add `$strict` parameter to `Inflector::toSnakeCase()` method (@arogachev) ## 2.1.2 July 27, 2023 diff --git a/src/Inflector.php b/src/Inflector.php index e76dda1..521fc26 100644 --- a/src/Inflector.php +++ b/src/Inflector.php @@ -592,12 +592,13 @@ public function toCamelCase(string $input): string * so "who's online" will be converted to "who_s_online". * * @param string $input The word to convert. + * @param bool $strict Whether to insert a separator between two consecutive uppercase chars, defaults to true. * * @return string The "snake_cased" string. */ - public function toSnakeCase(string $input): string + public function toSnakeCase(string $input, bool $strict = true): string { - return $this->pascalCaseToId(preg_replace('/[^\pL\pN]+/u', '_', $input), '_', true); + return $this->pascalCaseToId(preg_replace('/[^\pL\pN]+/u', '_', $input), '_', $strict); } /** diff --git a/tests/InflectorTest.php b/tests/InflectorTest.php index 247dcce..d068126 100644 --- a/tests/InflectorTest.php +++ b/tests/InflectorTest.php @@ -162,17 +162,28 @@ public function testToCamelCase(): void $this->assertEquals('ひらがなHepimiz', $inflector->toCamelCase('ひらがな_hepimiz')); } - public function testToSnakeCase(): void + public function dataToSnakeCase(): array + { + return [ + [['input' => 'userName'], 'user_name'], + [['input' => 'travelSGuide'], 'travel_s_guide'], + [['input' => 'ひらがなHepimiz'], 'ひらがな_hepimiz'], + [['input' => 'Let\'s say "Hello, World!" yii 3 😂'], 'let_s_say_hello_world_yii_3'], + [['input' => 'HTML'], 'h_t_m_l'], + [['input' => 'createMyDTO'], 'create_my_d_t_o'], + [['input' => 'HTML', 'strict' => false], 'html'], + [['input' => 'createMyDTO', 'strict' => false], 'create_my_dto'], + ]; + } + + /** + * @dataProvider dataToSnakeCase + */ + public function testToSnakeCase(array $arguments, string $expectedOutput): void { $inflector = new Inflector(); - $this->assertEquals('user_name', $inflector->toSnakeCase('userName')); - $this->assertEquals('travel_s_guide', $inflector->toSnakeCase('travelSGuide')); - $this->assertEquals('ひらがな_hepimiz', $inflector->toSnakeCase('ひらがなHepimiz')); - $this->assertEquals( - 'let_s_say_hello_world_yii_3', - $inflector->toSnakeCase('Let\'s say "Hello, World!" yii 3 😂') - ); + $this->assertEquals($expectedOutput, $inflector->toSnakeCase(...$arguments)); } public function testToTable(): void