diff --git a/README.md b/README.md index a21a603..f10f142 100644 --- a/README.md +++ b/README.md @@ -52,8 +52,10 @@ A PHP library with a variety of string manipulation functions with multibyte sup * [tidy](#tidy) * [titleize](#titleize) * [toAscii](#toascii) + * [toLowerCase](#tolowercase) * [toSpaces](#tospaces) * [toTabs](#totabs) + * [toUpperCase](#touppercase) * [trim](#trim) * [truncate](#truncate) * [underscored](#underscored) @@ -744,6 +746,20 @@ S::create('fòô bàř')->toAscii(); S::toAscii('fòô bàř'); // 'foo bar' ``` +#### toLowerCase + +$stringy->toLowerCase() + +S::toLowerCase(string $str [, string $encoding ]) + +Converts all characters in the string to lowercase. An alias for PHP's +mb_strtolower(). + +```php +S::create('FÒÔ BÀŘ', 'UTF-8')->toLowerCase(); +S::toLowerCase('FÒÔ BÀŘ', 'UTF-8'); // 'fòô bàř' +``` + #### toSpaces $stringy->toSpaces([ tabLength = 4 ]) @@ -773,6 +789,20 @@ S::create(' fòô bàř')->toTabs(); S::toTabs(' fòô bàř'); // ' fòô bàř' ``` +#### toUpperCase + +$stringy->toUpperCase() + +S::toUpperCase(string $str [, string $encoding ]) + +Converts all characters in the string to uppercase. An alias for PHP's +mb_strtoupper(). + +```php +S::create('fòô bàř', 'UTF-8')->toUpperCase(); +S::toUpperCase('fòô bàř', 'UTF-8'); // 'FÒÔ BÀŘ' +``` + #### trim $stringy->trim() diff --git a/src/Stringy/StaticStringy.php b/src/Stringy/StaticStringy.php index 968533a..d147053 100644 --- a/src/Stringy/StaticStringy.php +++ b/src/Stringy/StaticStringy.php @@ -296,6 +296,32 @@ class StaticStringy return Stringy::create($str)->toTabs($tabLength)->str; } + /** + * Converts all characters in the string to lowercase. An alias for PHP's + * mb_strtolower(). + * + * @param string $str String to convert case + * @param string $encoding The character encoding + * @return string The lowercase string + */ + public function toLowerCase($str, $encoding = null) + { + return Stringy::create($str, $encoding)->toLowerCase()->str; + } + + /** + * Converts all characters in the string to uppercase. An alias for PHP's + * mb_strtoupper(). + * + * @param string $str String to convert case + * @param string $encoding The character encoding + * @return string The uppercase string + */ + public function toUpperCase($str, $encoding = null) + { + return Stringy::create($str, $encoding)->toUpperCase()->str; + } + /** * Converts the string into an URL slug. This includes replacing non-ASCII * characters with their closest ASCII equivalents, removing non-alphanumeric diff --git a/src/Stringy/Stringy.php b/src/Stringy/Stringy.php index 5a25dfd..23ec6be 100644 --- a/src/Stringy/Stringy.php +++ b/src/Stringy/Stringy.php @@ -548,6 +548,32 @@ class Stringy return $stringy; } + /** + * Converts all characters in the string to lowercase. An alias for PHP's + * mb_strtolower(). + * + * @return Stringy Object with all characters of $str being lowercase + */ + public function toLowerCase() + { + $str = mb_strtolower($this->str, $this->encoding); + + return self::create($str, $this->encoding); + } + + /** + * Converts all characters in the string to uppercase. An alias for PHP's + * mb_strtoupper(). + * + * @return Stringy Object with all characters of $str being uppercase + */ + public function toUpperCase() + { + $str = mb_strtoupper($this->str, $this->encoding); + + return self::create($str, $this->encoding); + } + /** * Converts the string into an URL slug. This includes replacing non-ASCII * characters with their closest ASCII equivalents, removing non-alphanumeric diff --git a/tests/Stringy/CommonTest.php b/tests/Stringy/CommonTest.php index 5a73daf..f89dfbb 100644 --- a/tests/Stringy/CommonTest.php +++ b/tests/Stringy/CommonTest.php @@ -338,6 +338,32 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase return $testData; } + public function stringsForToLowerCase() + { + $testData = array( + array('foo bar', 'FOO BAR'), + array(' foo_bar ', ' FOO_bar '), + array('fòô bàř', 'FÒÔ BÀŘ', 'UTF-8'), + array(' fòô_bàř ', ' FÒÔ_bàř ', 'UTF-8'), + array('αυτοκίνητο', 'ΑΥΤΟΚΊΝΗΤΟ', 'UTF-8'), + ); + + return $testData; + } + + public function stringsForToUpperCase() + { + $testData = array( + array('FOO BAR', 'foo bar'), + array(' FOO_BAR ', ' FOO_bar '), + array('FÒÔ BÀŘ', 'fòô bàř', 'UTF-8'), + array(' FÒÔ_BÀŘ ', ' FÒÔ_bàř ', 'UTF-8'), + array('ΑΥΤΟΚΊΝΗΤΟ', 'αυτοκίνητο', 'UTF-8'), + ); + + return $testData; + } + public function stringsForSlugify() { $testData = array( diff --git a/tests/Stringy/StaticStringyTest.php b/tests/Stringy/StaticStringyTest.php index 5dd89df..17b9bd7 100644 --- a/tests/Stringy/StaticStringyTest.php +++ b/tests/Stringy/StaticStringyTest.php @@ -222,6 +222,26 @@ class StaticStringyTestCase extends CommonTest $this->assertEquals($expected, $result); } + /** + * @dataProvider stringsForToLowerCase + */ + public function testToLowerCase($expected, $str, $encoding = null) + { + $result = S::toLowerCase($str, $encoding); + $this->assertInternalType('string', $result); + $this->assertEquals($expected, $result); + } + + /** + * @dataProvider stringsForToUpperCase + */ + public function testToUpperCase($expected, $str, $encoding = null) + { + $result = S::toUpperCase($str, $encoding); + $this->assertInternalType('string', $result); + $this->assertEquals($expected, $result); + } + /** * @dataProvider stringsForSlugify */ diff --git a/tests/Stringy/StringyTest.php b/tests/Stringy/StringyTest.php index b9704d2..5534b7a 100644 --- a/tests/Stringy/StringyTest.php +++ b/tests/Stringy/StringyTest.php @@ -285,6 +285,30 @@ class StringyTestCase extends CommonTest $this->assertEquals($str, $stringy); } + /** + * @dataProvider stringsForToLowerCase + */ + public function testToLowerCase($expected, $str, $encoding = null) + { + $stringy = S::create($str, $encoding); + $result = $stringy->toLowerCase(); + $this->assertInstanceOf('Stringy\Stringy', $result); + $this->assertEquals($expected, $result); + $this->assertEquals($str, $stringy); + } + + /** + * @dataProvider stringsForToUpperCase + */ + public function testToUpperCase($expected, $str, $encoding = null) + { + $stringy = S::create($str, $encoding); + $result = $stringy->toUpperCase(); + $this->assertInstanceOf('Stringy\Stringy', $result); + $this->assertEquals($expected, $result); + $this->assertEquals($str, $stringy); + } + /** * @dataProvider stringsForSlugify */