Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
separator added for CaseCoverter
Browse files Browse the repository at this point in the history
  • Loading branch information
mkorkmaz committed Jul 13, 2017
1 parent 09d1c0d commit 2dcb093
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/CaseConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ class CaseConverter
* Returns PascalCase string
*
* @param string $source
* @param string $separator
* @return string
*
*/
public static function toPascalCase(string $source) : string
public static function toPascalCase(string $source, string $separator='_') : string
{
// If the string is snake case
$modified = str_replace('_', ' ', $source);
$modified = str_replace($separator, ' ', $source);
$lowercase = mb_strtolower($modified);
$uppercaseFirstLetters = mb_convert_case($lowercase, MB_CASE_TITLE);
return str_replace(' ', '', $uppercaseFirstLetters);
Expand All @@ -25,26 +26,28 @@ public static function toPascalCase(string $source) : string
* Returns camelCase string
*
* @param string $source
* @param string $separator
* @return string
*
*/
public static function toCamelCase(string $source) : string
public static function toCamelCase(string $source, string $separator='_') : string
{
return lcfirst(self::toPascalCase($source));
return lcfirst(self::toPascalCase($source, $separator));
}

/**
* Returns snake_case string
*
* @param string $source
* @param string $separator
* @return string
*
*/
public static function toSnakeCase(string $source) : string
public static function toSnakeCase(string $source, string $separator='_') : string
{
// If the string is pascal/camel case
$modified = str_replace(' ',' ',preg_replace('/[A-Z]+/', ' $0', $source));
$lowercase = mb_strtolower(trim($modified));
return str_replace(' ', '_', $lowercase);
return str_replace(' ', $separator, $lowercase);
}
}

0 comments on commit 2dcb093

Please sign in to comment.