Skip to content

Commit 4ca1e5b

Browse files
Fix for enum naming for camelcased+underscore scenario (#194)
* Fix duplicate PHP enum name generation * Add back camelcase-ization to limit the changes. * Fix code comment
1 parent 545439f commit 4ca1e5b

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

Templates/PHP/Model/EnumType.php.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class <#=enumT.Name.ToUpperFirstChar()#> extends Enum
4242
<#
4343
} else {
4444
#>
45-
const <#= value.Name.ToUnderscore().ToUpper()#> = "<#= value.Name.ToCamelize()#>";
45+
const <#= value.Name.ToUnderscorePHPEnum().ToUpper()#> = "<#= value.Name.ToCamelize()#>";
4646
<# }
4747
count++;
4848
}

src/GraphODataTemplateWriter/Extensions/StringExtensions.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,27 @@ public static string ToUnderscore(this string input)
7272
return Inflector.Inflector.Underscore(input);
7373
}
7474

75+
/// <summary>
76+
/// Replaces underscores with double underscore, adds underscore between camel
77+
/// case, converts hyphens to underscore. It does the same as ToUnderscore but
78+
/// adds the double underscore capability.
79+
/// </summary>
80+
/// <param name="input"></param>
81+
/// <returns></returns>
82+
public static string ToUnderscorePHPEnum(this string input)
83+
{
84+
return Regex.Replace(
85+
Regex.Replace(
86+
Regex.Replace(
87+
Regex.Replace(input, @"([_\s])([A-Z])", "$1_$2"),
88+
@"([a-z\d])([A-Z])",
89+
"$1_$2"),
90+
@"([A-Z]+)([A-Z][a-z])", "$1_$2"),
91+
@"[-\s]",
92+
"_")
93+
.ToLower();
94+
}
95+
7596
public static string RemoveFromEnd(this string input, string suffix)
7697
{
7798
if (input.EndsWith(suffix))

0 commit comments

Comments
 (0)