Skip to content

[String] Add support for kebab case #48781

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions src/Symfony/Component/String/AbstractString.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,8 @@ public function jsonSerialize(): string
return $this->string;
}

abstract public function kebab(): static;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AbstractString is not @internal, so this is a BC-break, I believe.


abstract public function length(): int;

abstract public function lower(): static;
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/String/AbstractUnicodeString.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,14 @@ public function join(array $strings, string $lastGlue = null): static
return $str;
}

public function kebab(): static
{
$str = $this->camel();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

camel()->lower()->replace('_', '-')

Is not enough? Just asking

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that's the case, maybe we don't need yet another method?

Copy link
Contributor Author

@seb-jean seb-jean Dec 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

camel()->lower()->replace('_', '-')

Is not enough? Just asking

Your example does not work.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, it's snake()->replace('_', '-'), I think.

$str->string = mb_strtolower(preg_replace(['/(\p{Lu}+)(\p{Lu}\p{Ll})/u', '/([\p{Ll}0-9])(\p{Lu})/u'], '\1-\2', $str->string), 'UTF-8');

return $str;
}

public function lower(): static
{
$str = clone $this;
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/String/ByteString.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,14 @@ public function join(array $strings, string $lastGlue = null): static
return $str;
}

public function kebab(): static
{
$str = $this->camel();
$str->string = strtolower(preg_replace(['/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'], '\1-\2', $str->string));

return $str;
}

public function length(): int
{
return \strlen($this->string);
Expand Down
28 changes: 28 additions & 0 deletions src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,34 @@ public static function provideSnake()
];
}

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

$this->assertEquals(static::createFromString($expectedString), $instance);
}

public static function provideKebab()
{
return [
['', ''],
['x-y', 'x_y'],
['x-y', 'X_Y'],
['xu-yo', 'xu_yo'],
['symfony-is-great', 'symfonyIsGreat'],
['symfony5-is-great', 'symfony5IsGreat'],
['symfony5is-great', 'symfony5isGreat'],
['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
10 changes: 10 additions & 0 deletions src/Symfony/Component/String/Tests/AbstractUnicodeTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,16 @@ public static function provideSnake()
);
}

public static function provideKebab()
{
return array_merge(
parent::provideKebab(),
[
['symfony-ist-äußerst-cool', 'symfonyIstÄußerstCool'],
]
);
}

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