Skip to content

[String] Add AbstractString::pascal() method #58545

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
Jan 5, 2025
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 @@ -438,6 +438,11 @@ public function kebab(): static
return $this->snake()->replace('_', '-');
}

public function pascal(): static
{
return $this->camel()->title();
}

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

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

7.3
---

* Add the `AbstractString::pascal()` method
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* Add the `AbstractString::pascal()` method
* Add the `AbstractString::pascal()` method

Copy link
Member

Choose a reason for hiding this comment

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

Fixed


7.2
---

Expand Down
27 changes: 27 additions & 0 deletions src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,33 @@ public static function provideKebab(): array
];
}

/**
* @dataProvider providePascal
*/
public function testPascal(string $expectedString, string $origin)
{
$instance = static::createFromString($origin)->pascal();

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

public static function providePascal(): array
{
return [
['', ''],
['XY', 'x_y'],
['XuYo', 'xu_yo'],
['SymfonyIsGreat', 'symfony_is_great'],
['Symfony5IsGreat', 'symfony_5_is_great'],
['SymfonyIsGreat', 'Symfony is great'],
['SYMFONYISGREAT', 'SYMFONY_IS_GREAT'],
['SymfonyIsAGreatFramework', 'Symfony is a great framework'],
['SymfonyIsGREAT', '*Symfony* is GREAT!!'],
['SYMFONY', 'SYMFONY'],
];
}

/**
* @dataProvider provideStartsWith
*/
Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Component/String/Tests/AbstractUnicodeTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,17 @@ public static function provideCamel()
);
}

public static function providePascal(): array
{
return array_merge(
parent::providePascal(),
[
['SymfonyIstÄußerstCool', 'symfonyIstÄußerstCool'],
['SymfonyWithEmojis', 'Symfony with 😃 emojis'],
]
);
}

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