Debian Codegraph @

src/Common/Mode.php

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