1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-08-12 00:04:11 +02:00

Added toLowerCase() and toUpperCase()

This commit is contained in:
Daniel St. Jules
2013-09-14 11:22:52 -04:00
parent ea20aebc91
commit 61cd5f5f4d
6 changed files with 152 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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