Util.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * Class Util
  4. *
  5. * @filesource Util.php
  6. * @created 25.11.2015
  7. * @package chillerlan\QRCode
  8. * @author Smiley <smiley@chillerlan.net>
  9. * @copyright 2015 Smiley
  10. * @license MIT
  11. */
  12. namespace chillerlan\QRCode;
  13. /**
  14. *
  15. */
  16. class Util{
  17. /**
  18. * @param string $s
  19. *
  20. * @return bool
  21. */
  22. public static function isNumber($s){
  23. $len = strlen($s);
  24. for($i = 0; $i < $len; $i++){
  25. $c = ord($s[$i]);
  26. if(!(ord('0') <= $c && $c <= ord('9'))){
  27. return false;
  28. }
  29. }
  30. return true;
  31. }
  32. /**
  33. * @param string $s
  34. *
  35. * @return bool
  36. */
  37. public static function isAlphaNum($s){
  38. $len = strlen($s);
  39. for($i = 0; $i < $len; $i++){
  40. $c = ord($s[$i]);
  41. if(!(ord('0') <= $c && $c <= ord('9')) && !(ord('A') <= $c && $c <= ord('Z')) && strpos(' $%*+-./:', $s[$i]) === false){
  42. return false;
  43. }
  44. }
  45. return true;
  46. }
  47. /**
  48. * @param string $s
  49. *
  50. * @return bool
  51. */
  52. public static function isKanji($s){
  53. if(empty($s)){
  54. return false;
  55. }
  56. $i = 0;
  57. $len = strlen($s);
  58. while($i + 1 < $len){
  59. $c = ((0xff&ord($s[$i])) << 8)|(0xff&ord($s[$i + 1]));
  60. if(!($c >= 0x8140 && $c <= 0x9FFC) && !($c >= 0xE040 && $c <= 0xEBBF)){
  61. return false;
  62. }
  63. $i += 2;
  64. }
  65. return !($i < $len);
  66. }
  67. /**
  68. * @param int $data
  69. *
  70. * @return int
  71. */
  72. public static function getBCHTypeInfo($data){
  73. $d = $data << 10;
  74. while(self::getBCHDigit($d) - self::getBCHDigit(QRConst::G15) >= 0){
  75. $d ^= (QRConst::G15 << (self::getBCHDigit($d) - self::getBCHDigit(QRConst::G15)));
  76. }
  77. return (($data << 10)|$d)^QRConst::G15_MASK;
  78. }
  79. /**
  80. * @param int $data
  81. *
  82. * @return int
  83. */
  84. public static function getBCHTypeNumber($data){
  85. $d = $data << 12;
  86. while(self::getBCHDigit($d) - self::getBCHDigit(QRConst::G18) >= 0){
  87. $d ^= (QRConst::G18 << (self::getBCHDigit($d) - self::getBCHDigit(QRConst::G18)));
  88. }
  89. return ($data << 12)|$d;
  90. }
  91. /**
  92. * @param int $data
  93. *
  94. * @return int
  95. */
  96. public static function getBCHDigit($data){
  97. $digit = 0;
  98. while($data !== 0){
  99. $digit++;
  100. $data >>= 1;
  101. }
  102. return $digit;
  103. }
  104. /**
  105. * @param int $typeNumber
  106. * @param int $errorCorrectLevel
  107. *
  108. * @return array
  109. * @throws \chillerlan\QRCode\QRCodeException
  110. */
  111. public static function getRSBlocks($typeNumber, $errorCorrectLevel){
  112. // PHP5 compat
  113. $RSBLOCK = QRConst::RSBLOCK;
  114. $BLOCK_TABLE = QRConst::BLOCK_TABLE;
  115. if(!isset($RSBLOCK[$errorCorrectLevel])){
  116. throw new QRCodeException('$typeNumber: '.$typeNumber.' / $errorCorrectLevel: '.$errorCorrectLevel);
  117. }
  118. $rsBlock = $BLOCK_TABLE[($typeNumber - 1) * 4 + $RSBLOCK[$errorCorrectLevel]];
  119. $list = [];
  120. $length = count($rsBlock) / 3;
  121. for($i = 0; $i < $length; $i++){
  122. for($j = 0; $j < $rsBlock[$i * 3 + 0]; $j++){
  123. $list[] = [$rsBlock[$i * 3 + 1], $rsBlock[$i * 3 + 2]];
  124. }
  125. }
  126. return $list;
  127. }
  128. /**
  129. * @param int $typeNumber
  130. * @param int $mode
  131. * @param int $ecLevel
  132. *
  133. * @return int
  134. * @throws \chillerlan\QRCode\QRCodeException
  135. */
  136. public static function getMaxLength($typeNumber, $mode, $ecLevel){
  137. $RSBLOCK = QRConst::RSBLOCK;
  138. $MAX_LENGTH = QRConst::MAX_LENGTH;
  139. $MODE = QRConst::MODE;
  140. if(!isset($RSBLOCK[$ecLevel])){
  141. throw new QRCodeException('Invalid error correct level: '.$ecLevel);
  142. }
  143. if(!isset($MODE[$mode])){
  144. throw new QRCodeException('Invalid mode: '.$mode);
  145. }
  146. return $MAX_LENGTH[$typeNumber - 1][$RSBLOCK[$ecLevel]][$MODE[$mode]];
  147. }
  148. }