Skip to content

[String] Add the AbstractString::kebab() method #58385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/String/AbstractString.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,11 @@ abstract public function slice(int $start = 0, ?int $length = null): static;

abstract public function snake(): static;

public function kebab(): static
{
return $this->snake()->replace('_', '-');
}

abstract public function splice(string $replacement, int $start = 0, ?int $length = null): static;

/**
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/String/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

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

7.1
---
Expand Down
29 changes: 29 additions & 0 deletions src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,35 @@ public static function provideSnake()
];
}

/**
* @dataProvider provideKebab
*/
public function testKebab(string $expectedString, string $origin)
{
$instance = static::createFromString($origin)->kebab();

$this->assertEquals(static::createFromString($expectedString), $instance);
$this->assertNotSame($origin, $instance, 'Strings should be immutable');
}

public static function provideKebab(): array
{
return [
['', ''],
['x-y', 'x_y'],
['x-y', 'X_Y'],
['xu-yo', 'xu_yo'],
['symfony-is-great', 'symfonyIsGreat'],
['symfony123-is-great', 'symfony123IsGreat'],
['symfony123is-great', 'symfony123isGreat'],
['symfony-is-great', 'Symfony is great'],
['symfony-is-a-great-framework', 'symfonyIsAGreatFramework'],
['symfony-is-great', 'symfonyIsGREAT'],
['symfony-is-really-great', 'symfonyIsREALLYGreat'],
['symfony', 'SYMFONY'],
];
}

/**
* @dataProvider provideStartsWith
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,15 @@ public static function provideSnake()
);
}

public static function provideKebab(): array
{
return [
...parent::provideKebab(),
['symfony-ist-äußerst-cool', 'symfonyIstÄußerstCool'],
['symfony-with-emojis', 'Symfony with 😃 emojis'],
];
}

public static function provideEqualsTo()
{
return array_merge(
Expand Down
Loading