Skip to content

Commit

Permalink
Add StringUtils new methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Louis Fortunier committed Apr 18, 2024
1 parent 67a4db5 commit ff065ef
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 0 deletions.
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
]
];
}
}

0 comments on commit ff065ef

Please sign in to comment.