BitArray.php
8 months ago
BitMatrix.php
8 months ago
BitUtils.php
8 months ago
CharacterSetEci.php
8 months ago
EcBlock.php
8 months ago
EcBlocks.php
8 months ago
ErrorCorrectionLevel.php
8 months ago
FormatInformation.php
8 months ago
Mode.php
8 months ago
ReedSolomonCodec.php
8 months ago
Version.php
8 months ago
Mode.php
80 lines
| 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 | * @var int[] |
| 37 | */ |
| 38 | private $characterCountBitsForVersions; |
| 39 | |
| 40 | /** |
| 41 | * @var int |
| 42 | */ |
| 43 | private $bits; |
| 44 | |
| 45 | /** |
| 46 | * @param int[] $characterCountBitsForVersions |
| 47 | */ |
| 48 | protected function __construct(array $characterCountBitsForVersions, int $bits) |
| 49 | { |
| 50 | $this->characterCountBitsForVersions = $characterCountBitsForVersions; |
| 51 | $this->bits = $bits; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Returns the number of bits used in a specific QR code version. |
| 56 | */ |
| 57 | public function getCharacterCountBits(Version $version) : int |
| 58 | { |
| 59 | $number = $version->getVersionNumber(); |
| 60 | |
| 61 | if ($number <= 9) { |
| 62 | $offset = 0; |
| 63 | } elseif ($number <= 26) { |
| 64 | $offset = 1; |
| 65 | } else { |
| 66 | $offset = 2; |
| 67 | } |
| 68 | |
| 69 | return $this->characterCountBitsForVersions[$offset]; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns the four bits used to encode this mode. |
| 74 | */ |
| 75 | public function getBits() : int |
| 76 | { |
| 77 | return $this->bits; |
| 78 | } |
| 79 | } |
| 80 |