From 88b8a8a6efbba1077b18ff5012156ad4f7fedbc7 Mon Sep 17 00:00:00 2001 From: "Daniel St. Jules" Date: Sat, 27 Jul 2013 23:56:07 -0400 Subject: [PATCH] Added isAlpha(), isAlphanumeric(), isBlank(), isLowerCase() and isUpperCase() --- README.md | 81 +++++++++++++++++++++---- src/Stringy/StaticStringy.php | 61 +++++++++++++++++++ src/Stringy/Stringy.php | 68 +++++++++++++++++++++ tests/Stringy/CommonTest.php | 91 +++++++++++++++++++++++++++++ tests/Stringy/StaticStringyTest.php | 45 ++++++++++++++ tests/Stringy/StringyTest.php | 45 ++++++++++++++ 6 files changed, 381 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index af70e75..f793e1d 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,11 @@ Note: The methods listed below are subject to change until we reach a 1.0.0 rele * [first](#first) * [humanize](#humanize) * [insert](#insert) + * [isAlpha](#isalpha) + * [isAlphanumeric](#isalphanumeric) + * [isBlank](#isblank) + * [isLowerCase](#islowercase) + * [isUpperCase](#isuppercase) * [last](#last) * [length](#length) * [longestCommonPrefix](#longestcommonprefix) @@ -276,6 +281,72 @@ S::create('fòô bà', 'UTF-8')->insert('ř', 6); S::insert('fòô bà', 'ř', 6, 'UTF-8'); // 'fòô bàř' ``` +##### isAlpha + +$stringy->isAlpha() + +S::isAlpha(string $str [, string $encoding ]) + +Returns true if $str contains only alphabetic chars, false otherwise. + +```php +S::create('丹尼爾', 'UTF-8')->isAlpha(); +S::isAlpha('丹尼爾', 'UTF-8'); // true +``` + +##### isAlphanumeric + +$stringy->isAlphanumeric() + +S::isAlphanumeric(string $str [, string $encoding ]) + +Returns true if $str contains only alphabetic and numeric chars, false +otherwise. + +```php +S::create('دانيال1', 'UTF-8')->isAlphanumeric(); +S::isAlphanumeric('دانيال1', 'UTF-8'); // true +``` + +##### isBlank + +$stringy->isBlank() + +S::isBlank(string $str [, string $encoding ]) + +Returns true if $str contains only whitespace chars, false otherwise. + +```php +S::create("\n\t \v\f")->isBlank(); +S::isBlank("\n\t \v\f"); // true +``` + +##### isLowerCase + +$stringy->isLowerCase() + +S::isLowerCase(string $str [, string $encoding ]) + +Returns true if $str contains only lower case chars, false otherwise. + +```php +S::create('fòô bàř', 'UTF-8')->isLowerCase(); +S::isLowerCase('fòô bàř', 'UTF-8'); // true +``` + +##### isUpperCase + +$stringy->isUpperCase() + +S::isUpperCase(string $str [, string $encoding ]) + +Returns true if $str contains only upper case chars, false otherwise. + +```php +S::create('FÒÔBÀŘ',, 'UTF-8')->isUpperCase(); +S::isUpperCase('FÒÔBÀŘ',, 'UTF-8'); // true +``` + ##### last $stringy->last(int $n) @@ -717,16 +788,6 @@ S::upperCaseFirst('σ test', 'UTF-8'); // 'Σ test' **toBoolean** -**isAlpha** - -**isAlphaNumeric** - -**isUpper** - -**isLower** - -**isBlank** - ## Tests [![Build Status](https://travis-ci.org/danielstjules/Stringy.png)](https://travis-ci.org/danielstjules/Stringy) diff --git a/src/Stringy/StaticStringy.php b/src/Stringy/StaticStringy.php index 7203167..de2cdaa 100644 --- a/src/Stringy/StaticStringy.php +++ b/src/Stringy/StaticStringy.php @@ -581,4 +581,65 @@ class StaticStringy { return Stringy::create($str, $encoding)->removeRight($substring)->str; } + + /** + * Returns true if $str contains only alphabetic chars, false otherwise. + * + * @param string $str String to check + * @param string $encoding The character encoding + * @return bool Whether or not $str contains only alphabetic chars + */ + public static function isAlpha($str, $encoding = null) + { + return Stringy::create($str, $encoding)->isAlpha(); + } + + /** + * Returns true if $str contains only alphabetic and numeric chars, false + * otherwise. + * + * @param string $str String to check + * @param string $encoding The character encoding + * @return bool Whether or not $str contains only alphanumeric chars + */ + public static function isAlphanumeric($str, $encoding = null) + { + return Stringy::create($str, $encoding)->isAlphanumeric(); + } + + /** + * Returns true if $str contains only whitespace chars, false otherwise. + * + * @param string $str String to check + * @param string $encoding The character encoding + * @return bool Whether or not $str contains only whitespace characters + */ + public static function isBlank($str, $encoding = null) + { + return Stringy::create($str, $encoding)->isBlank(); + } + + /** + * Returns true if $str contains only lower case chars, false otherwise. + * + * @param string $str String to check + * @param string $encoding The character encoding + * @return bool Whether or not $str contains only lower case characters + */ + public static function isLowerCase($str, $encoding = null) + { + return Stringy::create($str, $encoding)->isLowerCase(); + } + + /** + * Returns true if $str contains only upper case chars, false otherwise. + * + * @param string $str String to check + * @param string $encoding The character encoding + * @return bool Whether or not $str contains only upper case characters + */ + public static function isUpperCase($str, $encoding = null) + { + return Stringy::create($str, $encoding)->isUpperCase(); + } } diff --git a/src/Stringy/Stringy.php b/src/Stringy/Stringy.php index b4c6e34..93d46e3 100644 --- a/src/Stringy/Stringy.php +++ b/src/Stringy/Stringy.php @@ -884,4 +884,72 @@ class Stringy return $this; } + + /** + * Returns true if $str matches the supplied pattern, false otherwise. + * + * @param string Regex pattern to match against + * @return bool Whether or not $str matches the pattern + */ + private function matchesPattern($pattern) + { + $regexEncoding = mb_regex_encoding(); + mb_regex_encoding($this->encoding); + + $match = mb_ereg_match($pattern, $this->str); + mb_regex_encoding($regexEncoding); + + return $match; + } + + /** + * Returns true if $str contains only alphabetic chars, false otherwise. + * + * @return bool Whether or not $str contains only alphabetic chars + */ + public function isAlpha() + { + return $this->matchesPattern('^([[:alpha:]])*$'); + } + + /** + * Returns true if $str contains only alphabetic and numeric chars, false + * otherwise. + * + * @return bool Whether or not $str contains only alphanumeric chars + */ + public function isAlphanumeric() + { + return $this->matchesPattern('^([[:alnum:]])*$'); + } + + /** + * Returns true if $str contains only whitespace chars, false otherwise. + * + * @return bool Whether or not $str contains only whitespace characters + */ + public function isBlank() + { + return $this->matchesPattern('^([[:space:]])*$'); + } + + /** + * Returns true if $str contains only lower case chars, false otherwise. + * + * @return bool Whether or not $str contains only lower case characters + */ + public function isLowerCase() + { + return $this->matchesPattern('^([[:lower:]])*$'); + } + + /** + * Returns true if $str contains only lower case chars, false otherwise. + * + * @return bool Whether or not $str contains only lower case characters + */ + public function isUpperCase() + { + return $this->matchesPattern('^([[:upper:]])*$'); + } } diff --git a/tests/Stringy/CommonTest.php b/tests/Stringy/CommonTest.php index d3d36c7..08a0afd 100644 --- a/tests/Stringy/CommonTest.php +++ b/tests/Stringy/CommonTest.php @@ -705,6 +705,97 @@ class CommonTest extends PHPUnit_Framework_TestCase return $testData; } + public function stringsForIsAlpha() + { + $testData = array( + array(true, ''), + array(true, 'foobar'), + array(false, 'foo bar'), + array(false, 'foobar2'), + array(true, 'fòôbàř', 'UTF-8'), + array(false, 'fòô bàř', 'UTF-8'), + array(false, 'fòôbàř2', 'UTF-8'), + array(true, 'ҠѨњфгШ', 'UTF-8'), + array(false, 'ҠѨњ¨ˆфгШ', 'UTF-8'), + array(true, '丹尼爾', 'UTF-8') + ); + + return $testData; + } + + public function stringsForIsAlphanumeric() + { + $testData = array( + array(true, ''), + array(true, 'foobar1'), + array(false, 'foo bar'), + array(false, 'foobar2"'), + array(false, "\nfoobar\n"), + array(true, 'fòôbàř1', 'UTF-8'), + array(false, 'fòô bàř', 'UTF-8'), + array(false, 'fòôbàř2"', 'UTF-8'), + array(true, 'ҠѨњфгШ', 'UTF-8'), + array(false, 'ҠѨњ¨ˆфгШ', 'UTF-8'), + array(true, '丹尼爾111', 'UTF-8'), + array(true, 'دانيال1', 'UTF-8'), + array(false, 'دانيال1 ', 'UTF-8') + ); + + return $testData; + } + + public function stringsForIsBlank() + { + $testData = array( + array(true, ''), + array(true, ' '), + array(true, "\n\t "), + array(true, "\n\t \v\f"), + array(false, "\n\t a \v\f"), + array(false, "\n\t ' \v\f"), + array(false, "\n\t 2 \v\f"), + array(true, '', 'UTF-8'), + array(true, '   ', 'UTF-8'), // thin space + array(true, '  ', 'UTF-8'), // ideographic spaces + array(false, ' z', 'UTF-8'), + array(false, ' 1', 'UTF-8'), + ); + + return $testData; + } + + public function stringsForIsLowerCase() + { + $testData = array( + array(true, ''), + array(true, 'foobar'), + array(false, 'foo bar'), + array(false, 'Foobar'), + array(true, 'fòôbàř', 'UTF-8'), + array(false, 'fòôbàř2', 'UTF-8'), + array(false, 'fòô bàř', 'UTF-8'), + array(false, 'fòôbÀŘ', 'UTF-8'), + ); + + return $testData; + } + + public function stringsForIsUpperCase() + { + $testData = array( + array(true, ''), + array(true, 'FOOBAR'), + array(false, 'FOO BAR'), + array(false, 'fOOBAR'), + array(true, 'FÒÔBÀŘ', 'UTF-8'), + array(false, 'FÒÔBÀŘ2', 'UTF-8'), + array(false, 'FÒÔ BÀŘ', 'UTF-8'), + array(false, 'FÒÔBàř', 'UTF-8'), + ); + + return $testData; + } + // A test is required so as not to throw an error // This is a lot cleaner than using PHPUnit's mocks to spy public function test() { diff --git a/tests/Stringy/StaticStringyTest.php b/tests/Stringy/StaticStringyTest.php index 0113a48..8d4e053 100644 --- a/tests/Stringy/StaticStringyTest.php +++ b/tests/Stringy/StaticStringyTest.php @@ -390,4 +390,49 @@ class StaticStringyTestCase extends CommonTest $result = S::removeRight($str, $substring, $encoding); $this->assertEquals($expected, $result); } + + /** + * @dataProvider stringsForIsAlpha + */ + public function testIsAlpha($expected, $str, $encoding = null) + { + $result = S::isAlpha($str, $encoding); + $this->assertEquals($expected, $result); + } + + /** + * @dataProvider stringsForIsAlphanumeric + */ + public function testIsAlphanumeric($expected, $str, $encoding = null) + { + $result = S::isAlphanumeric($str, $encoding); + $this->assertEquals($expected, $result); + } + + /** + * @dataProvider stringsForIsBlank + */ + public function testIsBlank($expected, $str, $encoding = null) + { + $result = S::isBlank($str, $encoding); + $this->assertEquals($expected, $result); + } + + /** + * @dataProvider stringsForIsLowerCase + */ + public function testIsLowerCase($expected, $str, $encoding = null) + { + $result = S::isLowerCase($str, $encoding); + $this->assertEquals($expected, $result); + } + + /** + * @dataProvider stringsForIsUpperCase + */ + public function testIsUpperCase($expected, $str, $encoding = null) + { + $result = S::isUpperCase($str, $encoding); + $this->assertEquals($expected, $result); + } } diff --git a/tests/Stringy/StringyTest.php b/tests/Stringy/StringyTest.php index ea6ff1d..ba806f9 100644 --- a/tests/Stringy/StringyTest.php +++ b/tests/Stringy/StringyTest.php @@ -372,4 +372,49 @@ class StringyTestCase extends CommonTest $result = S::create($str, $encoding)->removeRight($substring); $this->assertEquals($expected, $result); } + + /** + * @dataProvider stringsForIsAlpha + */ + public function testIsAlpha($expected, $str, $encoding = null) + { + $result = S::create($str, $encoding)->isAlpha(); + $this->assertEquals($expected, $result); + } + + /** + * @dataProvider stringsForIsAlphanumeric + */ + public function testIsAlphanumeric($expected, $str, $encoding = null) + { + $result = S::create($str, $encoding)->isAlphanumeric(); + $this->assertEquals($expected, $result); + } + + /** + * @dataProvider stringsForIsBlank + */ + public function testIsBlank($expected, $str, $encoding = null) + { + $result = S::create($str, $encoding)->isBlank(); + $this->assertEquals($expected, $result); + } + + /** + * @dataProvider stringsForIsLowerCase + */ + public function testIsLowerCase($expected, $str, $encoding = null) + { + $result = S::create($str, $encoding)->isLowerCase(); + $this->assertEquals($expected, $result); + } + + /** + * @dataProvider stringsForIsUpperCase + */ + public function testIsUpperCase($expected, $str, $encoding = null) + { + $result = S::create($str, $encoding)->isUpperCase(); + $this->assertEquals($expected, $result); + } }