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 support for diacritics in camel caps names #3532

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 9 additions & 31 deletions src/Util/Common.php
Expand Up @@ -346,49 +346,27 @@ public static function isCamelCaps(
if ($strict === false) {
// Can either start with a lowercase letter, or multiple uppercase
// in a row, representing an acronym.
$legalFirstChar .= '([A-Z]{2,}|[a-z])';
$legalFirstChar .= '([[:upper:]]{2,}|[[:lower:]])';
} else {
$legalFirstChar .= '[a-z]';
$legalFirstChar .= '[[:lower:]]';
}
} else {
$legalFirstChar = '[A-Z]';
$legalFirstChar = '[[:upper:]]';
}

if (preg_match("/^$legalFirstChar/", $string) === 0) {
if (preg_match("/^$legalFirstChar/u", $string) === 0) {
return false;
}

// Check that the name only contains legal characters.
$legalChars = 'a-zA-Z0-9';
if (preg_match("|[^$legalChars]|", substr($string, 1)) > 0) {
if (preg_match('/^[[:alnum:]]*$/u', substr($string, 1)) === 0) {
return false;
}

if ($strict === true) {
// Check that there are not two capital letters next to each other.
$length = strlen($string);
$lastCharWasCaps = $classFormat;

for ($i = 1; $i < $length; $i++) {
$ascii = ord($string[$i]);
if ($ascii >= 48 && $ascii <= 57) {
// The character is a number, so it cant be a capital.
$isCaps = false;
} else {
if (strtoupper($string[$i]) === $string[$i]) {
$isCaps = true;
} else {
$isCaps = false;
}
}

if ($isCaps === true && $lastCharWasCaps === true) {
return false;
}

$lastCharWasCaps = $isCaps;
}
}//end if
// Check that there are not two capital letters next to each other.
if ($strict === true && preg_match('/[[:upper:]]{2}/u', $string) === 1) {
return false;
}

return true;

Expand Down
10 changes: 10 additions & 0 deletions tests/Core/IsCamelCapsTest.php
Expand Up @@ -25,6 +25,8 @@ public function testValidNotClassFormatPublic()
{
$this->assertTrue(Common::isCamelCaps('thisIsCamelCaps', false, true, true));
$this->assertTrue(Common::isCamelCaps('thisISCamelCaps', false, true, false));
$this->assertTrue(Common::isCamelCaps('élément', false, true, false));
$this->assertTrue(Common::isCamelCaps('unÉlément', false, true, false));

}//end testValidNotClassFormatPublic()

Expand All @@ -39,6 +41,8 @@ public function testInvalidNotClassFormatPublic()
$this->assertFalse(Common::isCamelCaps('_thisIsCamelCaps', false, true, true));
$this->assertFalse(Common::isCamelCaps('thisISCamelCaps', false, true, true));
$this->assertFalse(Common::isCamelCaps('ThisIsCamelCaps', false, true, true));
$this->assertFalse(Common::isCamelCaps('Élément', false, true, true));
$this->assertFalse(Common::isCamelCaps('UnÉlément', false, true, true));

$this->assertFalse(Common::isCamelCaps('3thisIsCamelCaps', false, true, true));
$this->assertFalse(Common::isCamelCaps('*thisIsCamelCaps', false, true, true));
Expand All @@ -61,6 +65,8 @@ public function testValidNotClassFormatPrivate()
{
$this->assertTrue(Common::isCamelCaps('_thisIsCamelCaps', false, false, true));
$this->assertTrue(Common::isCamelCaps('_thisISCamelCaps', false, false, false));
$this->assertTrue(Common::isCamelCaps('_élément', false, false, false));
$this->assertTrue(Common::isCamelCaps('_unÉlément', false, false, false));
$this->assertTrue(Common::isCamelCaps('_i18N', false, false, true));
$this->assertTrue(Common::isCamelCaps('_i18n', false, false, true));

Expand All @@ -79,6 +85,7 @@ public function testInvalidNotClassFormatPrivate()
$this->assertFalse(Common::isCamelCaps('_ThisIsCamelCaps', false, false, true));
$this->assertFalse(Common::isCamelCaps('__thisIsCamelCaps', false, false, true));
$this->assertFalse(Common::isCamelCaps('__thisISCamelCaps', false, false, false));
$this->assertFalse(Common::isCamelCaps('_unÉLément', false, false, true));

$this->assertFalse(Common::isCamelCaps('3thisIsCamelCaps', false, false, true));
$this->assertFalse(Common::isCamelCaps('*thisIsCamelCaps', false, false, true));
Expand All @@ -98,6 +105,7 @@ public function testValidClassFormatPublic()
$this->assertTrue(Common::isCamelCaps('ThisIsCamelCaps', true, true, true));
$this->assertTrue(Common::isCamelCaps('ThisISCamelCaps', true, true, false));
$this->assertTrue(Common::isCamelCaps('This3IsCamelCaps', true, true, false));
$this->assertTrue(Common::isCamelCaps('Élément', true, true, true));

}//end testValidClassFormatPublic()

Expand All @@ -112,6 +120,7 @@ public function testInvalidClassFormat()
$this->assertFalse(Common::isCamelCaps('thisIsCamelCaps', true));
$this->assertFalse(Common::isCamelCaps('This-IsCamelCaps', true));
$this->assertFalse(Common::isCamelCaps('This_Is_Camel_Caps', true));
$this->assertFalse(Common::isCamelCaps('UnÉLément', true));

}//end testInvalidClassFormat()

Expand All @@ -128,6 +137,7 @@ public function testInvalidClassFormatPrivate()
{
$this->assertFalse(Common::isCamelCaps('_ThisIsCamelCaps', true, true));
$this->assertFalse(Common::isCamelCaps('_ThisIsCamelCaps', true, false));
$this->assertFalse(Common::isCamelCaps('_UnÉlément', true, false));

}//end testInvalidClassFormatPrivate()

Expand Down