1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-08-07 13:56:31 +02:00

Add casetitle function

This commit is contained in:
Lucas Michot
2014-09-14 14:16:09 +02:00
parent 90de5cca00
commit 681463de87
4 changed files with 45 additions and 0 deletions

View File

@@ -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().

View File

@@ -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().

View File

@@ -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(

View File

@@ -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()
*/