Mode.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. declare(strict_types = 1);
  3. namespace BaconQrCode\Common;
  4. use DASPRiD\Enum\AbstractEnum;
  5. /**
  6. * Enum representing various modes in which data can be encoded to bits.
  7. *
  8. * @method static self TERMINATOR()
  9. * @method static self NUMERIC()
  10. * @method static self ALPHANUMERIC()
  11. * @method static self STRUCTURED_APPEND()
  12. * @method static self BYTE()
  13. * @method static self ECI()
  14. * @method static self KANJI()
  15. * @method static self FNC1_FIRST_POSITION()
  16. * @method static self FNC1_SECOND_POSITION()
  17. * @method static self HANZI()
  18. */
  19. final class Mode extends AbstractEnum
  20. {
  21. protected const TERMINATOR = [[0, 0, 0], 0x00];
  22. protected const NUMERIC = [[10, 12, 14], 0x01];
  23. protected const ALPHANUMERIC = [[9, 11, 13], 0x02];
  24. protected const STRUCTURED_APPEND = [[0, 0, 0], 0x03];
  25. protected const BYTE = [[8, 16, 16], 0x04];
  26. protected const ECI = [[0, 0, 0], 0x07];
  27. protected const KANJI = [[8, 10, 12], 0x08];
  28. protected const FNC1_FIRST_POSITION = [[0, 0, 0], 0x05];
  29. protected const FNC1_SECOND_POSITION = [[0, 0, 0], 0x09];
  30. protected const HANZI = [[8, 10, 12], 0x0d];
  31. /**
  32. * @var int[]
  33. */
  34. private $characterCountBitsForVersions;
  35. /**
  36. * @var int
  37. */
  38. private $bits;
  39. protected function __construct(array $characterCountBitsForVersions, int $bits)
  40. {
  41. $this->characterCountBitsForVersions = $characterCountBitsForVersions;
  42. $this->bits = $bits;
  43. }
  44. /**
  45. * Returns the number of bits used in a specific QR code version.
  46. */
  47. public function getCharacterCountBits(Version $version) : int
  48. {
  49. $number = $version->getVersionNumber();
  50. if ($number <= 9) {
  51. $offset = 0;
  52. } elseif ($number <= 26) {
  53. $offset = 1;
  54. } else {
  55. $offset = 2;
  56. }
  57. return $this->characterCountBitsForVersions[$offset];
  58. }
  59. /**
  60. * Returns the four bits used to encode this mode.
  61. */
  62. public function getBits() : int
  63. {
  64. return $this->bits;
  65. }
  66. }