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