diff --git a/src/Stringy/StaticStringy.php b/src/Stringy/StaticStringy.php index bb90c77..ab75aef 100644 --- a/src/Stringy/StaticStringy.php +++ b/src/Stringy/StaticStringy.php @@ -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() diff --git a/src/Stringy/Stringy.php b/src/Stringy/Stringy.php index 578c305..256d07a 100644 --- a/src/Stringy/Stringy.php +++ b/src/Stringy/Stringy.php @@ -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. * diff --git a/tests/Stringy/CommonTest.php b/tests/Stringy/CommonTest.php index 07afcbd..f300a08 100644 --- a/tests/Stringy/CommonTest.php +++ b/tests/Stringy/CommonTest.php @@ -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( diff --git a/tests/Stringy/StaticStringyTest.php b/tests/Stringy/StaticStringyTest.php index a329497..7ceae44 100644 --- a/tests/Stringy/StaticStringyTest.php +++ b/tests/Stringy/StaticStringyTest.php @@ -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 */ diff --git a/tests/Stringy/StringyTest.php b/tests/Stringy/StringyTest.php index 2df7b61..d6ab75b 100644 --- a/tests/Stringy/StringyTest.php +++ b/tests/Stringy/StringyTest.php @@ -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 */