Skip to content

Commit

Permalink
Add $strict parameter to `StringHelper::toSnakeCase() method (#113)
Browse files Browse the repository at this point in the history
Co-authored-by: Sergei Predvoditelev <sergei@predvoditelev.ru>
  • Loading branch information
arogachev and vjik authored Sep 19, 2023
1 parent fee2b84 commit 6ead89f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions src/Inflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
27 changes: 19 additions & 8 deletions tests/InflectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6ead89f

Please sign in to comment.