:octocat: added Number::CHAR_MAP and simplified QRCode::isNumber (...)

This commit is contained in:
codemasher
2018-03-13 18:20:17 +01:00
parent 23a7e851eb
commit c70413232b
2 changed files with 17 additions and 13 deletions
+14 -11
View File
@@ -273,16 +273,7 @@ class QRCode{
* @return bool
*/
public function isNumber(string $string):bool {
$len = strlen($string);
$map = str_split('0123456789');
for($i = 0; $i < $len; $i++){
if(!in_array($string[$i], $map, true)){
return false;
}
}
return true;
return $this->checkString($string, Number::CHAR_MAP);
}
/**
@@ -293,10 +284,22 @@ class QRCode{
* @return bool
*/
public function isAlphaNum(string $string):bool {
return $this->checkString($string, AlphaNum::CHAR_MAP);
}
/**
* checks is a given $string matches the characters of a given $charmap, returns false on the first invalid occurence.
*
* @param string $string
* @param array $charmap
*
* @return bool
*/
protected function checkString(string $string, array $charmap):bool{
$len = strlen($string);
for($i = 0; $i < $len; $i++){
if(!in_array($string[$i], AlphaNum::CHAR_MAP, true)){
if(!in_array($string[$i], $charmap, true)){
return false;
}
}