AlphaNum.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Class AlphaNum
  4. *
  5. * @created 25.11.2015
  6. * @author Smiley <smiley@chillerlan.net>
  7. * @copyright 2015 Smiley
  8. * @license MIT
  9. */
  10. namespace chillerlan\QRCode\Data;
  11. use chillerlan\QRCode\Common\{BitBuffer, Mode};
  12. use function ceil, intdiv, str_split;
  13. /**
  14. * Alphanumeric mode: 0 to 9, A to Z, space, $ % * + - . / :
  15. *
  16. * ISO/IEC 18004:2000 Section 8.3.3
  17. * ISO/IEC 18004:2000 Section 8.4.3
  18. */
  19. final class AlphaNum extends QRDataModeAbstract{
  20. /**
  21. * ISO/IEC 18004:2000 Table 5
  22. *
  23. * @var int[]
  24. */
  25. private const CHAR_TO_ORD = [
  26. '0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7,
  27. '8' => 8, '9' => 9, 'A' => 10, 'B' => 11, 'C' => 12, 'D' => 13, 'E' => 14, 'F' => 15,
  28. 'G' => 16, 'H' => 17, 'I' => 18, 'J' => 19, 'K' => 20, 'L' => 21, 'M' => 22, 'N' => 23,
  29. 'O' => 24, 'P' => 25, 'Q' => 26, 'R' => 27, 'S' => 28, 'T' => 29, 'U' => 30, 'V' => 31,
  30. 'W' => 32, 'X' => 33, 'Y' => 34, 'Z' => 35, ' ' => 36, '$' => 37, '%' => 38, '*' => 39,
  31. '+' => 40, '-' => 41, '.' => 42, '/' => 43, ':' => 44,
  32. ];
  33. /**
  34. * @var string[]
  35. */
  36. private const ORD_TO_CHAR = [
  37. '0', '1', '2', '3', '4', '5', '6', '7',
  38. '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
  39. 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
  40. 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
  41. 'W', 'X', 'Y', 'Z', ' ', '$', '%', '*',
  42. '+', '-', '.', '/', ':',
  43. ];
  44. /**
  45. * @inheritDoc
  46. */
  47. public const DATAMODE = Mode::ALPHANUM;
  48. /**
  49. * @inheritDoc
  50. */
  51. public function getLengthInBits():int{
  52. return (int)ceil($this->getCharCount() * (11 / 2));
  53. }
  54. /**
  55. * @inheritDoc
  56. */
  57. public static function validateString(string $string):bool{
  58. if($string === ''){
  59. return false;
  60. }
  61. foreach(str_split($string) as $chr){
  62. if(!isset(self::CHAR_TO_ORD[$chr])){
  63. return false;
  64. }
  65. }
  66. return true;
  67. }
  68. /**
  69. * @inheritDoc
  70. */
  71. public function write(BitBuffer $bitBuffer, int $versionNumber):static{
  72. $len = $this->getCharCount();
  73. $bitBuffer
  74. ->put(self::DATAMODE, 4)
  75. ->put($len, $this::getLengthBits($versionNumber))
  76. ;
  77. // encode 2 characters in 11 bits
  78. for($i = 0; ($i + 1) < $len; $i += 2){
  79. $bitBuffer->put(
  80. (self::CHAR_TO_ORD[$this->data[$i]] * 45 + self::CHAR_TO_ORD[$this->data[($i + 1)]]),
  81. 11,
  82. );
  83. }
  84. // encode a remaining character in 6 bits
  85. if($i < $len){
  86. $bitBuffer->put(self::CHAR_TO_ORD[$this->data[$i]], 6);
  87. }
  88. return $this;
  89. }
  90. /**
  91. * @inheritDoc
  92. *
  93. * @throws \chillerlan\QRCode\Data\QRCodeDataException
  94. */
  95. public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):string{
  96. $length = $bitBuffer->read(self::getLengthBits($versionNumber));
  97. $result = '';
  98. // Read two characters at a time
  99. while($length > 1){
  100. if($bitBuffer->available() < 11){
  101. throw new QRCodeDataException('not enough bits available'); // @codeCoverageIgnore
  102. }
  103. $nextTwoCharsBits = $bitBuffer->read(11);
  104. $result .= self::ORD_TO_CHAR[intdiv($nextTwoCharsBits, 45)];
  105. $result .= self::ORD_TO_CHAR[($nextTwoCharsBits % 45)];
  106. $length -= 2;
  107. }
  108. if($length === 1){
  109. // special case: one character left
  110. if($bitBuffer->available() < 6){
  111. throw new QRCodeDataException('not enough bits available'); // @codeCoverageIgnore
  112. }
  113. $result .= self::ORD_TO_CHAR[$bitBuffer->read(6)];
  114. }
  115. return $result;
  116. }
  117. }