Skip to content

Commit 6ead89f

Browse files
arogachevvjik
andauthored
Add $strict parameter to `StringHelper::toSnakeCase() method (#113)
Co-authored-by: Sergei Predvoditelev <sergei@predvoditelev.ru>
1 parent fee2b84 commit 6ead89f

3 files changed

Lines changed: 23 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- New #104: Add methods `StringHelper::trim()`, `StringHelper::ltrim()`, `StringHelper::rtrim()` (@olegbaturin)
1010
- Enh #83: Make minor refactoring with Rector help (@vjik)
1111
- Enh #111: Minor refactoring (@Tigrov)
12+
- Enh #92: Add `$strict` parameter to `Inflector::toSnakeCase()` method (@arogachev)
1213

1314
## 2.1.2 July 27, 2023
1415

src/Inflector.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,12 +592,13 @@ public function toCamelCase(string $input): string
592592
* so "who's online" will be converted to "who_s_online".
593593
*
594594
* @param string $input The word to convert.
595+
* @param bool $strict Whether to insert a separator between two consecutive uppercase chars, defaults to true.
595596
*
596597
* @return string The "snake_cased" string.
597598
*/
598-
public function toSnakeCase(string $input): string
599+
public function toSnakeCase(string $input, bool $strict = true): string
599600
{
600-
return $this->pascalCaseToId(preg_replace('/[^\pL\pN]+/u', '_', $input), '_', true);
601+
return $this->pascalCaseToId(preg_replace('/[^\pL\pN]+/u', '_', $input), '_', $strict);
601602
}
602603

603604
/**

tests/InflectorTest.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,17 +162,28 @@ public function testToCamelCase(): void
162162
$this->assertEquals('ひらがなHepimiz', $inflector->toCamelCase('ひらがな_hepimiz'));
163163
}
164164

165-
public function testToSnakeCase(): void
165+
public function dataToSnakeCase(): array
166+
{
167+
return [
168+
[['input' => 'userName'], 'user_name'],
169+
[['input' => 'travelSGuide'], 'travel_s_guide'],
170+
[['input' => 'ひらがなHepimiz'], 'ひらがな_hepimiz'],
171+
[['input' => 'Let\'s say "Hello, World!" yii 3 😂'], 'let_s_say_hello_world_yii_3'],
172+
[['input' => 'HTML'], 'h_t_m_l'],
173+
[['input' => 'createMyDTO'], 'create_my_d_t_o'],
174+
[['input' => 'HTML', 'strict' => false], 'html'],
175+
[['input' => 'createMyDTO', 'strict' => false], 'create_my_dto'],
176+
];
177+
}
178+
179+
/**
180+
* @dataProvider dataToSnakeCase
181+
*/
182+
public function testToSnakeCase(array $arguments, string $expectedOutput): void
166183
{
167184
$inflector = new Inflector();
168185

169-
$this->assertEquals('user_name', $inflector->toSnakeCase('userName'));
170-
$this->assertEquals('travel_s_guide', $inflector->toSnakeCase('travelSGuide'));
171-
$this->assertEquals('ひらがな_hepimiz', $inflector->toSnakeCase('ひらがなHepimiz'));
172-
$this->assertEquals(
173-
'let_s_say_hello_world_yii_3',
174-
$inflector->toSnakeCase('Let\'s say "Hello, World!" yii 3 😂')
175-
);
186+
$this->assertEquals($expectedOutput, $inflector->toSnakeCase(...$arguments));
176187
}
177188

178189
public function testToTable(): void

0 commit comments

Comments
 (0)