mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-08-11 07:44:12 +02:00
Added isHexadecimal
This commit is contained in:
@@ -648,6 +648,18 @@ class StaticStringy
|
||||
return Stringy::create($str, $encoding)->isUpperCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string contains only hexadecimal chars, false otherwise.
|
||||
*
|
||||
* @param string $str String to check
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str contains only hexadecimal characters
|
||||
*/
|
||||
public static function isHexadecimal($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->isHexadecimal();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of occurrences of $substring in the given string.
|
||||
* An alias for mb_substr_count()
|
||||
|
@@ -974,6 +974,16 @@ class Stringy
|
||||
return $this->matchesPattern('^([[:alnum:]])*$');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string contains only hexadecimal chars, false otherwise.
|
||||
*
|
||||
* @return bool Whether or not $str contains only hexadecimal chars
|
||||
*/
|
||||
public function isHexadecimal()
|
||||
{
|
||||
return $this->matchesPattern('^([[:xdigit:]])*$');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string contains only whitespace chars, false otherwise.
|
||||
*
|
||||
|
@@ -798,6 +798,27 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForIsHexadecimal()
|
||||
{
|
||||
$testData = array(
|
||||
array(true, ''),
|
||||
array(true, 'abcdef'),
|
||||
array(true, 'ABCDEF'),
|
||||
array(true, '0123456789'),
|
||||
array(true, '0123456789AbCdEf'),
|
||||
array(false, '0123456789x'),
|
||||
array(false, 'ABCDEFx'),
|
||||
array(true, 'abcdef', 'UTF-8'),
|
||||
array(true, 'ABCDEF', 'UTF-8'),
|
||||
array(true, '0123456789', 'UTF-8'),
|
||||
array(true, '0123456789AbCdEf', 'UTF-8'),
|
||||
array(false, '0123456789x', 'UTF-8'),
|
||||
array(false, 'ABCDEFx', 'UTF-8'),
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForCount()
|
||||
{
|
||||
$testData = array(
|
||||
|
@@ -482,6 +482,16 @@ class StaticStringyTestCase extends CommonTest
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsHexadecimal
|
||||
*/
|
||||
public function testIsHexadecimal($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::isHexadecimal($str, $encoding);
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForCount
|
||||
*/
|
||||
|
@@ -562,6 +562,18 @@ class StringyTestCase extends CommonTest
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsHexadecimal
|
||||
*/
|
||||
public function testIsHexadecimal($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->isHexadecimal();
|
||||
$this->assertInternalType('boolean', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForCount
|
||||
*/
|
||||
|
Reference in New Issue
Block a user