diff --git a/README.md b/README.md index d215d60..ec993c0 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,7 @@ Tested and compatible with PHP 5.3+ and HHVM. Inspired by underscore.string.js. * [titleize](#titleize) * [toAscii](#toascii) * [toLowerCase](#tolowercase) + * [toTitleCase](#toTitleCase) * [toSpaces](#tospaces) * [toTabs](#totabs) * [toUpperCase](#touppercase) @@ -852,6 +853,19 @@ S::create('FÒÔ BÀŘ', 'UTF-8')->toLowerCase(); S::toLowerCase('FÒÔ BÀŘ', 'UTF-8'); // 'fòô bàř' ``` +#### toTitleCase + +$stringy->toTitleCase() + +S::toTitleCase(string $str [, string $encoding ]) + +Converts the first character of each word in the string to uppercase. + +```php +S::create('fòô bàř', 'UTF-8')->toTitleCase(); +S::toTitleCase('fòô bàř', 'UTF-8'); // 'Fòô Bàř' +``` + #### toSpaces $stringy->toSpaces([ tabLength = 4 ]) diff --git a/src/StaticStringy.php b/src/StaticStringy.php index 97671e6..5a8c683 100644 --- a/src/StaticStringy.php +++ b/src/StaticStringy.php @@ -324,15 +324,15 @@ class StaticStringy } /** - * Uppercase the first character of each word in a string. + * Converts the first character of each word in the string to uppercase. * * @param string $str String to convert case * @param string $encoding The character encoding * @return string The title-cased string */ - public static function toCaseTitle($str, $encoding = null) + public static function toTitleCase($str, $encoding = null) { - return (string) Stringy::create($str, $encoding)->toCaseTitle(); + return (string) Stringy::create($str, $encoding)->toTitleCase(); } /** diff --git a/src/Stringy.php b/src/Stringy.php index bd14e96..50efb5c 100644 --- a/src/Stringy.php +++ b/src/Stringy.php @@ -677,11 +677,11 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess } /** - * Uppercase the first character of each word in a string. + * Converts the first character of each word in the string to uppercase. * * @return Stringy Object with all characters of $str being title-cased */ - public function toCaseTitle() + public function toTitleCase() { $str = mb_convert_case($this->str, MB_CASE_TITLE, $this->encoding); diff --git a/tests/CommonTest.php b/tests/CommonTest.php index bc59fb8..c4207ee 100644 --- a/tests/CommonTest.php +++ b/tests/CommonTest.php @@ -318,10 +318,10 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase ); } - public function toCaseTitleProvider() + public function toTitleCaseProvider() { return array( - array( 'Foo Bar', 'foo bar'), + array('Foo Bar', 'foo bar'), array(' Foo_Bar ', ' foo_bar '), array('Fòô Bàř', 'fòô bàř', 'UTF-8'), array(' Fòô_Bàř ', ' fòô_bàř ', 'UTF-8'), diff --git a/tests/StaticStringyTest.php b/tests/StaticStringyTest.php index bf19108..6b43a6a 100644 --- a/tests/StaticStringyTest.php +++ b/tests/StaticStringyTest.php @@ -245,11 +245,11 @@ class StaticStringyTestCase extends CommonTest } /** - * @dataProvider toCaseTitleProvider() + * @dataProvider toTitleCaseProvider() */ - public function testToCaseTitle($expected, $str, $encoding = null) + public function testToTitleCase($expected, $str, $encoding = null) { - $result = S::toCaseTitle($str, $encoding); + $result = S::toTitleCase($str, $encoding); $this->assertInternalType('string', $result); $this->assertEquals($expected, $result); } diff --git a/tests/StringyTest.php b/tests/StringyTest.php index 771bf85..17860ab 100644 --- a/tests/StringyTest.php +++ b/tests/StringyTest.php @@ -432,6 +432,18 @@ class StringyTestCase extends CommonTest $this->assertEquals($str, $stringy); } + /** + * @dataProvider toTitleCaseProvider() + */ + public function testToTitleCase($expected, $str, $encoding = null) + { + $stringy = S::create($str, $encoding); + $result = $stringy->toTitleCase(); + $this->assertInstanceOf('Stringy\Stringy', $result); + $this->assertEquals($expected, $result); + $this->assertEquals($str, $stringy); + } + /** * @dataProvider toUpperCaseProvider() */