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

Added isHexadecimal

This commit is contained in:
Lucas
2013-08-22 15:40:43 +02:00
parent f1abc38c17
commit a1d9787309
5 changed files with 65 additions and 0 deletions

View File

@@ -648,6 +648,18 @@ class StaticStringy
return Stringy::create($str, $encoding)->isUpperCase(); 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. * Returns the number of occurrences of $substring in the given string.
* An alias for mb_substr_count() * An alias for mb_substr_count()

View File

@@ -974,6 +974,16 @@ class Stringy
return $this->matchesPattern('^([[:alnum:]])*$'); 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. * Returns true if the string contains only whitespace chars, false otherwise.
* *

View File

@@ -798,6 +798,27 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
return $testData; 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() public function stringsForCount()
{ {
$testData = array( $testData = array(

View File

@@ -482,6 +482,16 @@ class StaticStringyTestCase extends CommonTest
$this->assertEquals($expected, $result); $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 * @dataProvider stringsForCount
*/ */

View File

@@ -562,6 +562,18 @@ class StringyTestCase extends CommonTest
$this->assertEquals($str, $stringy); $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 * @dataProvider stringsForCount
*/ */