mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-08-11 15:54:04 +02:00
Added isAlpha(), isAlphanumeric(), isBlank(), isLowerCase() and isUpperCase()
This commit is contained in:
81
README.md
81
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)
|
* [first](#first)
|
||||||
* [humanize](#humanize)
|
* [humanize](#humanize)
|
||||||
* [insert](#insert)
|
* [insert](#insert)
|
||||||
|
* [isAlpha](#isalpha)
|
||||||
|
* [isAlphanumeric](#isalphanumeric)
|
||||||
|
* [isBlank](#isblank)
|
||||||
|
* [isLowerCase](#islowercase)
|
||||||
|
* [isUpperCase](#isuppercase)
|
||||||
* [last](#last)
|
* [last](#last)
|
||||||
* [length](#length)
|
* [length](#length)
|
||||||
* [longestCommonPrefix](#longestcommonprefix)
|
* [longestCommonPrefix](#longestcommonprefix)
|
||||||
@@ -276,6 +281,72 @@ S::create('fòô bà', 'UTF-8')->insert('ř', 6);
|
|||||||
S::insert('fòô bà', 'ř', 6, 'UTF-8'); // 'fòô bàř'
|
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
|
##### last
|
||||||
|
|
||||||
$stringy->last(int $n)
|
$stringy->last(int $n)
|
||||||
@@ -717,16 +788,6 @@ S::upperCaseFirst('σ test', 'UTF-8'); // 'Σ test'
|
|||||||
|
|
||||||
**toBoolean**
|
**toBoolean**
|
||||||
|
|
||||||
**isAlpha**
|
|
||||||
|
|
||||||
**isAlphaNumeric**
|
|
||||||
|
|
||||||
**isUpper**
|
|
||||||
|
|
||||||
**isLower**
|
|
||||||
|
|
||||||
**isBlank**
|
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
[](https://travis-ci.org/danielstjules/Stringy)
|
[](https://travis-ci.org/danielstjules/Stringy)
|
||||||
|
@@ -581,4 +581,65 @@ class StaticStringy
|
|||||||
{
|
{
|
||||||
return Stringy::create($str, $encoding)->removeRight($substring)->str;
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -884,4 +884,72 @@ class Stringy
|
|||||||
|
|
||||||
return $this;
|
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:]])*$');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -705,6 +705,97 @@ class CommonTest extends PHPUnit_Framework_TestCase
|
|||||||
return $testData;
|
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
|
// A test is required so as not to throw an error
|
||||||
// This is a lot cleaner than using PHPUnit's mocks to spy
|
// This is a lot cleaner than using PHPUnit's mocks to spy
|
||||||
public function test() {
|
public function test() {
|
||||||
|
@@ -390,4 +390,49 @@ class StaticStringyTestCase extends CommonTest
|
|||||||
$result = S::removeRight($str, $substring, $encoding);
|
$result = S::removeRight($str, $substring, $encoding);
|
||||||
$this->assertEquals($expected, $result);
|
$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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -372,4 +372,49 @@ class StringyTestCase extends CommonTest
|
|||||||
$result = S::create($str, $encoding)->removeRight($substring);
|
$result = S::create($str, $encoding)->removeRight($substring);
|
||||||
$this->assertEquals($expected, $result);
|
$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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user