Mode.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. declare(strict_types=1);
  11. namespace chillerlan\QRCode\Common;
  12. use chillerlan\QRCode\Data\{AlphaNum, Byte, Hanzi, Kanji, Number};
  13. use chillerlan\QRCode\QRCodeException;
  14. /**
  15. * Data mode information - ISO 18004:2006, 6.4.1, Tables 2 and 3
  16. */
  17. final class Mode{
  18. // ISO/IEC 18004:2000 Table 2
  19. /** @var int */
  20. public const TERMINATOR = 0b0000;
  21. /** @var int */
  22. public const NUMBER = 0b0001;
  23. /** @var int */
  24. public const ALPHANUM = 0b0010;
  25. /** @var int */
  26. public const BYTE = 0b0100;
  27. /** @var int */
  28. public const KANJI = 0b1000;
  29. /** @var int */
  30. public const HANZI = 0b1101;
  31. /** @var int */
  32. public const STRCTURED_APPEND = 0b0011;
  33. /** @var int */
  34. public const FNC1_FIRST = 0b0101;
  35. /** @var int */
  36. public const FNC1_SECOND = 0b1001;
  37. /** @var int */
  38. public const ECI = 0b0111;
  39. /**
  40. * mode length bits for the version breakpoints 1-9, 10-26 and 27-40
  41. *
  42. * ISO/IEC 18004:2000 Table 3 - Number of bits in Character Count Indicator
  43. */
  44. public const LENGTH_BITS = [
  45. self::NUMBER => [10, 12, 14],
  46. self::ALPHANUM => [ 9, 11, 13],
  47. self::BYTE => [ 8, 16, 16],
  48. self::KANJI => [ 8, 10, 12],
  49. self::HANZI => [ 8, 10, 12],
  50. self::ECI => [ 0, 0, 0],
  51. ];
  52. /**
  53. * Map of data mode => interface (detection order)
  54. *
  55. * @var array<int, string>
  56. */
  57. public const INTERFACES = [
  58. self::NUMBER => Number::class,
  59. self::ALPHANUM => AlphaNum::class,
  60. self::KANJI => Kanji::class,
  61. self::HANZI => Hanzi::class,
  62. self::BYTE => Byte::class,
  63. ];
  64. /**
  65. * returns the length bits for the version breakpoints 1-9, 10-26 and 27-40
  66. *
  67. * @throws \chillerlan\QRCode\QRCodeException
  68. */
  69. public static function getLengthBitsForVersion(int $mode, int $version):int{
  70. if(!isset(self::LENGTH_BITS[$mode])){
  71. throw new QRCodeException('invalid mode given');
  72. }
  73. $minVersion = 0;
  74. foreach([9, 26, 40] as $key => $breakpoint){
  75. if($version > $minVersion && $version <= $breakpoint){
  76. return self::LENGTH_BITS[$mode][$key];
  77. }
  78. $minVersion = $breakpoint;
  79. }
  80. throw new QRCodeException(sprintf('invalid version number: %d', $version));
  81. }
  82. }