Skip to content

Commit 8133473

Browse files
[String] Add the AbstractString::kebab() method
1 parent 7e9bd70 commit 8133473

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

AbstractString.php

+5
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,11 @@ abstract public function slice(int $start = 0, ?int $length = null): static;
433433

434434
abstract public function snake(): static;
435435

436+
public function kebab(): static
437+
{
438+
return $this->snake()->replace('_', '-');
439+
}
440+
436441
abstract public function splice(string $replacement, int $start = 0, ?int $length = null): static;
437442

438443
/**

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add `TruncateMode` enum to handle more truncate methods
8+
* Add the `AbstractString::kebab()` method
89

910
7.1
1011
---

Tests/AbstractAsciiTestCase.php

+29
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,35 @@ public static function provideSnake()
10891089
];
10901090
}
10911091

1092+
/**
1093+
* @dataProvider provideKebab
1094+
*/
1095+
public function testKebab(string $expectedString, string $origin)
1096+
{
1097+
$instance = static::createFromString($origin)->kebab();
1098+
1099+
$this->assertEquals(static::createFromString($expectedString), $instance);
1100+
$this->assertNotSame($origin, $instance, 'Strings should be immutable');
1101+
}
1102+
1103+
public static function provideKebab(): array
1104+
{
1105+
return [
1106+
['', ''],
1107+
['x-y', 'x_y'],
1108+
['x-y', 'X_Y'],
1109+
['xu-yo', 'xu_yo'],
1110+
['symfony-is-great', 'symfonyIsGreat'],
1111+
['symfony123-is-great', 'symfony123IsGreat'],
1112+
['symfony123is-great', 'symfony123isGreat'],
1113+
['symfony-is-great', 'Symfony is great'],
1114+
['symfony-is-a-great-framework', 'symfonyIsAGreatFramework'],
1115+
['symfony-is-great', 'symfonyIsGREAT'],
1116+
['symfony-is-really-great', 'symfonyIsREALLYGreat'],
1117+
['symfony', 'SYMFONY'],
1118+
];
1119+
}
1120+
10921121
/**
10931122
* @dataProvider provideStartsWith
10941123
*/

Tests/AbstractUnicodeTestCase.php

+9
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,15 @@ public static function provideSnake()
665665
);
666666
}
667667

668+
public static function provideKebab(): array
669+
{
670+
return [
671+
...parent::provideKebab(),
672+
['symfony-ist-äußerst-cool', 'symfonyIstÄußerstCool'],
673+
['symfony-with-emojis', 'Symfony with 😃 emojis'],
674+
];
675+
}
676+
668677
public static function provideEqualsTo()
669678
{
670679
return array_merge(

0 commit comments

Comments
 (0)