Skip to content

Commit

Permalink
Add more methos wordformater (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Dec 17, 2023
1 parent 4e090cf commit bd4870e
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 16 deletions.
20 changes: 11 additions & 9 deletions src/TimeZone.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ public static function getAll(): array
$listsIdentifiers = DateTimeZone::listIdentifiers();

foreach ($listsIdentifiers as $timeZone) {
$name = str_replace('_', ' ', $timeZone);
$date = new DateTime('now', new DateTimeZone($timeZone));
$timeZones[] =
[
'timezone' => $timeZone,
'name' => "$name (UTC {$date->format('P')})",
'offset' => $date->getOffset(),
]
;
if (!empty($timeZone)) {
$name = str_replace('_', ' ', $timeZone);
$date = new DateTime('now', new DateTimeZone($timeZone));
$timeZones[] =
[
'timezone' => $timeZone,
'name' => "$name (UTC {$date->format('P')})",
'offset' => $date->getOffset(),
]
;
}
}

$offsets = array_column($timeZones, 'offset');
Expand Down
56 changes: 49 additions & 7 deletions src/WordFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use function explode;
use function implode;
use function preg_match;
use function preg_replace;
use function preg_split;
use function str_contains;
use function strtolower;
use function ucfirst;

Expand All @@ -21,17 +23,18 @@ final class WordFormatter
* Converts a string to words with capitalized first letters.
*
* This function takes a string as input and converts it into words with the first letter of each word capitalized.
* The input string can be in different formats, such as camelCase or snake_case, and the function will handle them properly.
* If the input string is in all uppercase, it will be treated as a single word and capitalized accordingly.
* The input string can be in different formats, such as camelCase or snake_case, and the function will handle them
* properly.
* If the input string is in uppercase, it will be treated as a single word and capitalized accordingly.
*
* @param string $string The input string to be converted.
* @param string $value The input string to be converted.
*
* @return string The string with words having capitalized first letters separated by spaces.
*/
public static function capitalizeToWords(string $string): string
public static function capitalizeToWords(string $value): string
{
if (preg_match('/^[A-Z][^_]*(_[A-Z][^_]*)*/', $string)) {
$strings = explode('_', $string);
if (preg_match('/^[A-Z][^_]*(_[A-Z][^_]*)*/', $value)) {
$strings = explode('_', $value);
$word = '';

foreach ($strings as $index => $string) {
Expand All @@ -45,12 +48,51 @@ public static function capitalizeToWords(string $string): string
}

$capitalizedWords = [];
$words = preg_split('/(?=[A-Z])|_/', $string);
$words = preg_split('/(?=[A-Z])|_/', $value);

foreach ($words as $word) {
$capitalizedWords[] = ucfirst($word);
}

return implode(' ', $capitalizedWords);
}

/**
* Converts a camelCase formatted string to snake_case.
*
* @param string $value The camelCase formatted string to convert.
*/
public static function camelCaseToSnakeCase(string $value): string
{
$snakeCase = preg_replace('/([A-Z])/', '_$1', $value);
$snakeCase = ltrim($snakeCase, '_'); // Eliminar guión bajo inicial si está presente
return strtolower($snakeCase);
}

/**
* Convert a snake_case formatted string to camelCase.
*
* @param string $snakeCaseString The snake_case formatted string to convert.
*
* @return string The converted camelCase string.
*/
public static function snakeCaseToCamelCase(string $snakeCaseString): string
{
if (str_contains($snakeCaseString, '_') === false) {
return $snakeCaseString;
}

$words = explode('_', $snakeCaseString);
$camelCase = '';

foreach ($words as $index => $word) {
if ($index === 0) {
$camelCase = $word;
} else {
$camelCase .= ucfirst($word);
}
}

return $camelCase;
}
}
22 changes: 22 additions & 0 deletions tests/WordFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,26 @@ public function testCapitalize(): void
$this->assertSame('Date Birth', WordFormatter::capitalizeToWords('dateBirth'));
$this->assertSame('Date Of Message', WordFormatter::capitalizeToWords('dateOfMessage'));
}

public function testCamelCaseToSnakeCase(): void
{
$this->assertSame('foo', WordFormatter::camelCaseToSnakeCase('foo'));
$this->assertSame('foo', WordFormatter::camelCaseToSnakeCase('Foo'));
$this->assertSame('foo_bar', WordFormatter::camelCaseToSnakeCase('fooBar'));
$this->assertSame('foo_bar', WordFormatter::camelCaseToSnakeCase('FooBar'));
$this->assertSame('created', WordFormatter::camelCaseToSnakeCase('created'));
$this->assertSame('created_at', WordFormatter::camelCaseToSnakeCase('createdAt'));
$this->assertSame('date_birth', WordFormatter::camelCaseToSnakeCase('dateBirth'));
$this->assertSame('date_of_message', WordFormatter::camelCaseToSnakeCase('dateOfMessage'));
}

public function testSnakeCaseToCamelCase(): void
{
$this->assertSame('foo', WordFormatter::snakeCaseToCamelCase('foo'));
$this->assertSame('fooBar', WordFormatter::snakeCaseToCamelCase('foo_bar'));
$this->assertSame('created', WordFormatter::snakeCaseToCamelCase('created'));
$this->assertSame('createdAt', WordFormatter::snakeCaseToCamelCase('created_at'));
$this->assertSame('dateBirth', WordFormatter::snakeCaseToCamelCase('date_birth'));
$this->assertSame('dateOfBirth', WordFormatter::snakeCaseToCamelCase('date_of_birth'));
}
}

0 comments on commit bd4870e

Please sign in to comment.