Passed
Push — main ( a7758f...de27a9 )
by smiley
02:24
created

Mode::getLengthBitsForMode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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