Mode.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Class Mode
  4. *
  5. * @created 19.11.2020
  6. * @author smiley <smiley@chillerlan.net>
  7. * @copyright 2020 smiley
  8. * @license MIT
  9. */
  10. namespace chillerlan\QRCode\Common;
  11. use chillerlan\QRCode\Data\{AlphaNum, Byte, Kanji, Number};
  12. use chillerlan\QRCode\QRCodeException;
  13. /**
  14. * ISO 18004:2006, 6.4.1, Tables 2 and 3
  15. */
  16. final class Mode{
  17. // ISO/IEC 18004:2000 Table 2
  18. /** @var int */
  19. public const DATA_TERMINATOR = 0b0000;
  20. /** @var int */
  21. public const DATA_NUMBER = 0b0001;
  22. /** @var int */
  23. public const DATA_ALPHANUM = 0b0010;
  24. /** @var int */
  25. public const DATA_BYTE = 0b0100;
  26. /** @var int */
  27. public const DATA_KANJI = 0b1000;
  28. /** @var int */
  29. public const DATA_STRCTURED_APPEND = 0b0011;
  30. /** @var int */
  31. public const DATA_FNC1_FIRST = 0b0101;
  32. /** @var int */
  33. public const DATA_FNC1_SECOND = 0b1001;
  34. /** @var int */
  35. public const DATA_ECI = 0b0111;
  36. /**
  37. * mode length bits for the version breakpoints 1-9, 10-26 and 27-40
  38. *
  39. * ISO/IEC 18004:2000 Table 3 - Number of bits in Character Count Indicator
  40. */
  41. public const LENGTH_BITS = [
  42. self::DATA_NUMBER => [10, 12, 14],
  43. self::DATA_ALPHANUM => [ 9, 11, 13],
  44. self::DATA_BYTE => [ 8, 16, 16],
  45. self::DATA_KANJI => [ 8, 10, 12],
  46. ];
  47. /**
  48. * Map of data mode => interface (detection order)
  49. *
  50. * @var string[]
  51. */
  52. public const DATA_INTERFACES = [
  53. self::DATA_NUMBER => Number::class,
  54. self::DATA_ALPHANUM => AlphaNum::class,
  55. self::DATA_KANJI => Kanji::class,
  56. self::DATA_BYTE => Byte::class,
  57. ];
  58. /**
  59. * References to the keys of the following table(s):
  60. *
  61. * @see \chillerlan\QRCode\Common\Version::MAX_LENGTH
  62. *
  63. * @var int[]
  64. */
  65. public const DATA_MODES = [
  66. self::DATA_NUMBER => 0,
  67. self::DATA_ALPHANUM => 1,
  68. self::DATA_BYTE => 2,
  69. self::DATA_KANJI => 3,
  70. ];
  71. /**
  72. * returns the length bits for the version breakpoints 1-9, 10-26 and 27-40
  73. *
  74. * @throws \chillerlan\QRCode\QRCodeException
  75. */
  76. public static function getLengthBitsForVersion(int $mode, int $version):int{
  77. if(!isset(self::LENGTH_BITS[$mode])){
  78. throw new QRCodeException('invalid mode given');
  79. }
  80. $minVersion = 0;
  81. foreach([9, 26, 40] as $key => $breakpoint){
  82. if($version > $minVersion && $version <= $breakpoint){
  83. return self::LENGTH_BITS[$mode][$key];
  84. }
  85. $minVersion = $breakpoint;
  86. }
  87. throw new QRCodeException(sprintf('invalid version number: %d', $version));
  88. }
  89. /**
  90. * returns the array of length bits for the given mode
  91. */
  92. public static function getLengthBitsForMode(int $mode):array{
  93. return self::LENGTH_BITS[$mode];
  94. }
  95. }