Skip to content

Commit c1876cd

Browse files
committed
[String] Introduce underscore() method
1 parent 6c16390 commit c1876cd

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

src/Symfony/Component/String/AbstractString.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,8 @@ public function truncate(int $length, string $ellipsis = ''): self
636636
return $ellipsisLength ? $str->trimEnd()->append($ellipsis) : $str;
637637
}
638638

639+
abstract public function underscore(): self;
640+
639641
/**
640642
* @return static
641643
*/

src/Symfony/Component/String/AbstractUnicodeString.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,14 @@ public function trimStart(string $chars = " \t\n\r\0\x0B\x0C\u{A0}\u{FEFF}"): pa
396396
return $str;
397397
}
398398

399+
public function underscore(): parent
400+
{
401+
$str = $this->camel()->snake();
402+
$str->string = mb_strtolower(preg_replace('~(?<=[a-z])([0-9])~u', '_$1', $str->string), 'UTF-8');
403+
404+
return $str;
405+
}
406+
399407
public function upper(): parent
400408
{
401409
$str = clone $this;

src/Symfony/Component/String/ByteString.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,14 @@ public function trimStart(string $chars = " \t\n\r\0\x0B\x0C"): parent
439439
return $str;
440440
}
441441

442+
public function underscore(): parent
443+
{
444+
$str = $this->camel()->snake();
445+
$str->string = mb_strtolower(preg_replace('~(?<=[a-z])([0-9])~u', '_$1', $str->string), 'UTF-8');
446+
447+
return $str;
448+
}
449+
442450
public function upper(): parent
443451
{
444452
$str = clone $this;

src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,32 @@ public static function provideSnake()
930930
];
931931
}
932932

933+
/**
934+
* @dataProvider provideUnderscore
935+
*/
936+
public function testUnderscore(string $expectedString, string $origin): void
937+
{
938+
$instance = static::createFromString($origin)->underscore();
939+
940+
$this->assertEquals(static::createFromString($expectedString), $instance);
941+
}
942+
943+
public static function provideUnderscore(): array
944+
{
945+
return [
946+
['', ''],
947+
['symfony_is_great', 'symfonyIsGreat'],
948+
['symfony_44_is_great', 'symfony44IsGreat'],
949+
['symfony_is_great_5', 'symfonyIsGreat5'],
950+
['symfony_5_is_great', 'symfony5IsGreat'],
951+
['symfony_5is_great', 'symfony5isGreat'],
952+
['symfony_is_great', 'Symfony is great'],
953+
['symfony_is_a_great_framework', 'symfonyIsAGreatFramework'],
954+
['symfony_is_great', 'symfonyIsGREAT'],
955+
['symfony_is_really_great', 'symfonyIsREALLYGreat'],
956+
];
957+
}
958+
933959
/**
934960
* @dataProvider provideStartsWith
935961
*/

0 commit comments

Comments
 (0)