Skip to content
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

Add StringUtils new methods #23

Merged
merged 1 commit into from
Apr 22, 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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
CHANGELOG for 1.x
===================
## v1.2.3 - (2024-04-18)
### Added
- `StringUtils::upperAccentuatedCharacter` + tests
- `StringUtils::lowerAccentuatedCharacter` + tests
- `StringUtils::formatLastName` + tests
- `StringUtils::formatFirstName` + tests
- `StringUtils::formatSpaceBetween` + tests

## v1.2.2 - (2024-04-02)
### Added
- Add mapping comment on EntityTrait to be compatible with doctrine annotations type
Expand Down
59 changes: 59 additions & 0 deletions src/Utils/StringUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
*/
class StringUtils
{
public const LOWER_ACCENTUATED_CHARACTER = ['é', 'è', 'ê', 'ë', 'à', 'á', 'â', 'ï'];
public const UPPER_ACCENTUATED_CHARACTER = ['É', 'È', 'Ê', 'Ë', 'À', 'Á', 'Â', 'Ï'];

/**
* input : App\Entity\Folder\SomeEntityName
* output : folder_some_entity_name
Expand Down Expand Up @@ -127,4 +130,60 @@ public static function intToExcelColumn(int $n): string

return $result;
}

public static function upperAccentuatedCharacter(array|string|null $string): array|string
{
return str_replace(
self::LOWER_ACCENTUATED_CHARACTER,
self::UPPER_ACCENTUATED_CHARACTER,
$string
);
}

public static function lowerAccentuatedCharacter(array|string|null $string): array|string
{
return str_replace(
self::UPPER_ACCENTUATED_CHARACTER,
self::LOWER_ACCENTUATED_CHARACTER,
$string
);
}

public static function formatLastName(?string $string): string
{
if ($string === null) {
return '';
}

$toReturn = strtoupper($string);
// We use string into upperAccentuatedCharacter parameter, so the return will be string
return self::upperAccentuatedCharacter($toReturn); // @phpstan-ignore-line
}

public static function formatFirstName(?string $string): string
{
if ($string === null) {
return '';
}

if (in_array(substr($string, 0, 2), self::LOWER_ACCENTUATED_CHARACTER)) {
$firstLetter = self::upperAccentuatedCharacter(substr($string, 0, 2));
$endString = strtolower(substr(self::lowerAccentuatedCharacter($string), 2)); // @phpstan-ignore-line
$toReturn = $firstLetter . $endString; // @phpstan-ignore-line
} else {
$toReturn = ucfirst(strtolower(self::lowerAccentuatedCharacter($string))); // @phpstan-ignore-line
}

return $toReturn;
}

public static function formatSpaceBetween(?string $first, ?string $last): string
{
$space = '';
if (strlen($first) > 0 && strlen($last) > 0) {
$space = ' ';
}

return $first . $space . $last;
}
}
117 changes: 117 additions & 0 deletions tests/Utils/StringUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,121 @@ public function intToExcelColumnProvider(): array
'past double alphabetical' => ['BA', 53],
];
}

/**
* @dataProvider getLastNameProvider
* @param string|null $expected
* @param string|null $values
*/
public function testFormatLastName($expected, $values): void
{
$this->assertSame($expected, StringUtils::formatLastName($values));
}

/**
* @return array
*/
public function getLastNameProvider(): array
{
return [
'null' => [
'',
null
],
'simple_upper' => [
// expected
"DUCROT",
// values
"ducrot"
],
'accentuated_character' => [
"ÉÈÊËÀÁÂÏ",
"éèêëàáâï"
],
];
}

/**
* @dataProvider getFirstNameProvider
* @param string|null $expected
* @param string|null $values
*/
public function testFormatFirstName($expected, $values): void
{
$this->assertSame($expected, StringUtils::formatFirstName($values));
}

/**
* @return array
*/
public function getFirstNameProvider()
{
return [
'null' => [
// expected
'',
// values
null
],
'simple_ucfirst' => [
// expected
"Mathieu",
// values
"mathieu"
],
'ensure_ucfirst' => [
// expected
"Mathieu",
// values
"mathIEU"
],
'accentuated_character_first_only' => [
"Àmoré",
"àmorÉ"
],
'accentuated_character_end' => [
"Jérémy",
"jérémy"
],
'ensure_lower_end' => [
"Jérémy",
"jérÉMY"
],
];
}


/**
* @dataProvider formatSpaceBetweenProvider
*/
public function testFormatSpaceBetween(string $expected, ?string $first, ?string $last): void
{
$this->assertSame($expected, StringUtils::formatSpaceBetween($first, $last));
}

public function formatSpaceBetweenProvider(): array
{
return [
'simple' => [
'first last',
"first",
"last"
],
'first_null' => [
'last',
null,
"last"
],
'last_null' => [
'first',
'first',
null
],
'both_null' => [
'',
null,
null
]
];
}
}