Passed
Push — main ( a9949d...738294 )
by smiley
01:55
created

FormatInformation::getMaskPattern()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
1
<?php
2
/**
3
 * Class FormatInformation
4
 *
5
 * @created      24.01.2021
6
 * @author       ZXing Authors
7
 * @author       Smiley <[email protected]>
8
 * @copyright    2021 Smiley
9
 * @license      Apache-2.0
10
 */
11
12
namespace chillerlan\QRCode\Common;
13
14
/**
15
 * Encapsulates a QR Code's format information, including the data mask used and error correction level.
16
 *
17
 * @author Sean Owen
18
 * @see    \chillerlan\QRCode\Common\EccLevel
19
 */
20
final class FormatInformation{
21
22
	public const FORMAT_INFO_MASK_QR = 0x5412;
23
24
	/**
25
	 * See ISO 18004:2006, Annex C, Table C.1
26
	 *
27
	 * [data bits, sequence after masking]
28
	 */
29
	public const DECODE_LOOKUP = [
30
		[0x00, 0x5412],
31
		[0x01, 0x5125],
32
		[0x02, 0x5E7C],
33
		[0x03, 0x5B4B],
34
		[0x04, 0x45F9],
35
		[0x05, 0x40CE],
36
		[0x06, 0x4F97],
37
		[0x07, 0x4AA0],
38
		[0x08, 0x77C4],
39
		[0x09, 0x72F3],
40
		[0x0A, 0x7DAA],
41
		[0x0B, 0x789D],
42
		[0x0C, 0x662F],
43
		[0x0D, 0x6318],
44
		[0x0E, 0x6C41],
45
		[0x0F, 0x6976],
46
		[0x10, 0x1689],
47
		[0x11, 0x13BE],
48
		[0x12, 0x1CE7],
49
		[0x13, 0x19D0],
50
		[0x14, 0x0762],
51
		[0x15, 0x0255],
52
		[0x16, 0x0D0C],
53
		[0x17, 0x083B],
54
		[0x18, 0x355F],
55
		[0x19, 0x3068],
56
		[0x1A, 0x3F31],
57
		[0x1B, 0x3A06],
58
		[0x1C, 0x24B4],
59
		[0x1D, 0x2183],
60
		[0x1E, 0x2EDA],
61
		[0x1F, 0x2BED],
62
	];
63
64
	/**
65
	 * The current ECC level value
66
	 *
67
	 * L: 0b01
68
	 * M: 0b00
69
	 * Q: 0b11
70
	 * H: 0b10
71
	 */
72
	private int $errorCorrectionLevel;
73
74
	/**
75
	 * The current mask pattern (0-7)
76
	 */
77
	private int $maskPattern;
78
79
	/**
80
	 * Receives the format information from a parsed QR Code, detects ECC level and mask pattern
81
	 */
82
	public function __construct(int $formatInfo){
83
		$this->errorCorrectionLevel = ($formatInfo >> 3) & 0x03; // Bits 3,4
84
		$this->maskPattern          = ($formatInfo & 0x07); // Bottom 3 bits
85
	}
86
87
	/**
88
	 * Returns and EccLevel instance ith the detected ECC level set
89
	 */
90
	public function getErrorCorrectionLevel():EccLevel{
91
		return new EccLevel($this->errorCorrectionLevel);
92
	}
93
94
	/**
95
	 * Returns a MaskPattern instance with the detected mask pattern set
96
	 */
97
	public function getMaskPattern():MaskPattern{
98
		return new MaskPattern($this->maskPattern);
99
	}
100
101
}
102
103