mirror of
https://github.com/chillerlan/php-qrcode.git
synced 2026-08-24 15:47:54 +00:00
:octocat: added Number::CHAR_MAP and simplified QRCode::isNumber (...)
This commit is contained in:
+3
-2
@@ -19,6 +19,8 @@ use chillerlan\QRCode\QRCode;
|
||||
*/
|
||||
class Number extends QRDataAbstract{
|
||||
|
||||
const CHAR_MAP = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
@@ -63,13 +65,12 @@ class Number extends QRDataAbstract{
|
||||
*/
|
||||
protected function parseInt(string $string):int {
|
||||
$num = 0;
|
||||
$map = str_split('0123456789');
|
||||
|
||||
$len = strlen($string);
|
||||
for($i = 0; $i < $len; $i++){
|
||||
$c = ord($string[$i]);
|
||||
|
||||
if(!in_array($string[$i], $map, true)){
|
||||
if(!in_array($string[$i], $this::CHAR_MAP, true)){
|
||||
throw new QRCodeDataException('illegal char: "'.$string[$i].'" ['.$c.']');
|
||||
}
|
||||
|
||||
|
||||
+14
-11
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user