From 681463de8753defa37f00660b73ff83ec2d875b5 Mon Sep 17 00:00:00 2001 From: Lucas Michot Date: Sun, 14 Sep 2014 14:16:09 +0200 Subject: [PATCH] Add casetitle function --- src/StaticStringy.php | 12 ++++++++++++ src/Stringy.php | 12 ++++++++++++ tests/CommonTest.php | 11 +++++++++++ tests/StaticStringyTest.php | 10 ++++++++++ 4 files changed, 45 insertions(+) diff --git a/src/StaticStringy.php b/src/StaticStringy.php index 7a6c303..97671e6 100644 --- a/src/StaticStringy.php +++ b/src/StaticStringy.php @@ -323,6 +323,18 @@ class StaticStringy return (string) Stringy::create($str, $encoding)->toLowerCase(); } + /** + * Uppercase the first character of each word in a string. + * + * @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) + { + return (string) Stringy::create($str, $encoding)->toCaseTitle(); + } + /** * Converts all characters in the string to uppercase. An alias for PHP's * mb_strtoupper(). diff --git a/src/Stringy.php b/src/Stringy.php index 6a80463..bd14e96 100644 --- a/src/Stringy.php +++ b/src/Stringy.php @@ -676,6 +676,18 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess return self::create($str, $this->encoding); } + /** + * Uppercase the first character of each word in a string. + * + * @return Stringy Object with all characters of $str being title-cased + */ + public function toCaseTitle() + { + $str = mb_convert_case($this->str, MB_CASE_TITLE, $this->encoding); + + return self::create($str, $this->encoding); + } + /** * Converts all characters in the string to lowercase. An alias for PHP's * mb_strtolower(). diff --git a/tests/CommonTest.php b/tests/CommonTest.php index 7607bf0..bc59fb8 100644 --- a/tests/CommonTest.php +++ b/tests/CommonTest.php @@ -318,6 +318,17 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase ); } + public function toCaseTitleProvider() + { + return 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'), + ); + } + public function toUpperCaseProvider() { return array( diff --git a/tests/StaticStringyTest.php b/tests/StaticStringyTest.php index 676bff4..bf19108 100644 --- a/tests/StaticStringyTest.php +++ b/tests/StaticStringyTest.php @@ -244,6 +244,16 @@ class StaticStringyTestCase extends CommonTest $this->assertEquals($expected, $result); } + /** + * @dataProvider toCaseTitleProvider() + */ + public function testToCaseTitle($expected, $str, $encoding = null) + { + $result = S::toCaseTitle($str, $encoding); + $this->assertInternalType('string', $result); + $this->assertEquals($expected, $result); + } + /** * @dataProvider toUpperCaseProvider() */