Skip to content

Commit

Permalink
Add single/blank support for char case rules
Browse files Browse the repository at this point in the history
  • Loading branch information
jakejohns committed Mar 4, 2016
1 parent a841aa0 commit 67ce515
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/Rule/AbstractCharCase.php
Expand Up @@ -90,10 +90,15 @@ protected function ucwords($str)
protected function ucfirst($str)
{
$len = $this->strlen($str);
$head = $this->substr($str, 0, 1);
$tail = $this->substr($str, 1, $len - 1);

return $this->strtoupper($head) . $tail;
if ($len == 0) {
return '';
}
if ($len > 1) {
$head = $this->substr($str, 0, 1);
$tail = $this->substr($str, 1, $len - 1);
return $this->strtoupper($head) . $tail;
}
return $this->strtoupper($str);
}

/**
Expand All @@ -109,10 +114,17 @@ protected function ucfirst($str)
protected function lcfirst($str)
{
$len = $this->strlen($str);
$head = $this->substr($str, 0, 1);
$tail = $this->substr($str, 1, $len - 1);

return $this->strtolower($head) . $tail;
if ($len == 0) {
// empty string
return '';
}
if ($len > 1) {
// more than a single character
$head = $this->substr($str, 0, 1);
$tail = $this->substr($str, 1, $len - 1);
return $this->strtolower($head) . $tail;
}
return $this->strtolower($str);
}

}
1 change: 1 addition & 0 deletions tests/Rule/Sanitize/LowercaseFirstTest.php
Expand Up @@ -7,6 +7,7 @@ public function providerTo()
{
return array(
array(array(), false, array()),
array('', true, ''),
array('A', true, 'a'),
array('AbCd', true, 'abCd'),
array('ABCDEF', true, 'aBCDEF'),
Expand Down
1 change: 1 addition & 0 deletions tests/Rule/Sanitize/LowercaseTest.php
Expand Up @@ -7,6 +7,7 @@ public function providerTo()
{
return array(
array(array(), false, array()),
array('', true, ''),
array('A', true, 'a'),
array('AbCd', true, 'abcd'),
array('ABCDEF', true, 'abcdef'),
Expand Down
1 change: 1 addition & 0 deletions tests/Rule/Sanitize/UppercaseFirstTest.php
Expand Up @@ -7,6 +7,7 @@ public function providerTo()
{
return array(
array(array(), false, array()),
array('', true, ''),
array('a', true, 'A'),
array('Ab cd', true, 'Ab cd'),
array('ABC DEF', true, 'ABC DEF'),
Expand Down
1 change: 1 addition & 0 deletions tests/Rule/Sanitize/UppercaseTest.php
Expand Up @@ -7,6 +7,7 @@ public function providerTo()
{
return array(
array(array(), false, array()),
array('', true, ''),
array('a', true, 'A'),
array('Ab cd', true, 'AB CD'),
array('ABC DEF', true, 'ABC DEF'),
Expand Down

0 comments on commit 67ce515

Please sign in to comment.