PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.20
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.20
1.10.20 1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 All 164 releases
woocommerce-pos / vendor_prefixed / chillerlan / php-qrcode / src / Common / EccLevel.php

EccLevel.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.20, at vendor_prefixed/chillerlan/php-qrcode/src/Common/EccLevel.php

248 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class EccLevel
5 *
6 * @created 19.11.2020
7 * @author smiley <[email protected]>
8 * @copyright 2020 smiley
9 * @license MIT
10 */
11 namespace WCPOS\Vendor\chillerlan\QRCode\Common;
12
13 use WCPOS\Vendor\chillerlan\QRCode\QRCodeException;
14 use function array_column;
15 /**
16 * This class encapsulates the four error correction levels defined by the QR code standard.
17 */
18 final class EccLevel
19 {
20 // ISO/IEC 18004:2000 Tables 12, 25
21 /** @var int */
22 public const L = 0b1;
23 // 7%.
24 /** @var int */
25 public const M = 0b0;
26 // 15%.
27 /** @var int */
28 public const Q = 0b11;
29 // 25%.
30 /** @var int */
31 public const H = 0b10;
32 // 30%.
33 /**
34 * ISO/IEC 18004:2000 Tables 7-11 - Number of symbol characters and input data capacity for versions 1 to 40
35 *
36 * @var int[][]
37 */
38 private const MAX_BITS = [
39 // [ L, M, Q, H] // v => modules
40 [0, 0, 0, 0],
41 // 0 => will be ignored, index starts at 1
42 [152, 128, 104, 72],
43 // 1 => 21
44 [272, 224, 176, 128],
45 // 2 => 25
46 [440, 352, 272, 208],
47 // 3 => 29
48 [640, 512, 384, 288],
49 // 4 => 33
50 [864, 688, 496, 368],
51 // 5 => 37
52 [1088, 864, 608, 480],
53 // 6 => 41
54 [1248, 992, 704, 528],
55 // 7 => 45
56 [1552, 1232, 880, 688],
57 // 8 => 49
58 [1856, 1456, 1056, 800],
59 // 9 => 53
60 [2192, 1728, 1232, 976],
61 // 10 => 57
62 [2592, 2032, 1440, 1120],
63 // 11 => 61
64 [2960, 2320, 1648, 1264],
65 // 12 => 65
66 [3424, 2672, 1952, 1440],
67 // 13 => 69 NICE!
68 [3688, 2920, 2088, 1576],
69 // 14 => 73
70 [4184, 3320, 2360, 1784],
71 // 15 => 77
72 [4712, 3624, 2600, 2024],
73 // 16 => 81
74 [5176, 4056, 2936, 2264],
75 // 17 => 85
76 [5768, 4504, 3176, 2504],
77 // 18 => 89
78 [6360, 5016, 3560, 2728],
79 // 19 => 93
80 [6888, 5352, 3880, 3080],
81 // 20 => 97
82 [7456, 5712, 4096, 3248],
83 // 21 => 101
84 [8048, 6256, 4544, 3536],
85 // 22 => 105
86 [8752, 6880, 4912, 3712],
87 // 23 => 109
88 [9392, 7312, 5312, 4112],
89 // 24 => 113
90 [10208, 8000, 5744, 4304],
91 // 25 => 117
92 [10960, 8496, 6032, 4768],
93 // 26 => 121
94 [11744, 9024, 6464, 5024],
95 // 27 => 125
96 [12248, 9544, 6968, 5288],
97 // 28 => 129
98 [13048, 10136, 7288, 5608],
99 // 29 => 133
100 [13880, 10984, 7880, 5960],
101 // 30 => 137
102 [14744, 11640, 8264, 6344],
103 // 31 => 141
104 [15640, 12328, 8920, 6760],
105 // 32 => 145
106 [16568, 13048, 9368, 7208],
107 // 33 => 149
108 [17528, 13800, 9848, 7688],
109 // 34 => 153
110 [18448, 14496, 10288, 7888],
111 // 35 => 157
112 [19472, 15312, 10832, 8432],
113 // 36 => 161
114 [20528, 15936, 11408, 8768],
115 // 37 => 165
116 [21616, 16816, 12016, 9136],
117 // 38 => 169
118 [22496, 17728, 12656, 9776],
119 // 39 => 173
120 [23648, 18672, 13328, 10208],
121 ];
122 /**
123 * ISO/IEC 18004:2000 Section 8.9 - Format Information
124 *
125 * ECC level -> mask pattern
126 *
127 * @var int[][]
128 */
129 private const FORMAT_PATTERN = [[
130 // L
131 0b111011111000100,
132 0b111001011110011,
133 0b111110110101010,
134 0b111100010011101,
135 0b110011000101111,
136 0b110001100011000,
137 0b110110001000001,
138 0b110100101110110,
139 ], [
140 // M
141 0b101010000010010,
142 0b101000100100101,
143 0b101111001111100,
144 0b101101101001011,
145 0b100010111111001,
146 0b100000011001110,
147 0b100111110010111,
148 0b100101010100000,
149 ], [
150 // Q
151 0b11010101011111,
152 0b11000001101000,
153 0b11111100110001,
154 0b11101000000110,
155 0b10010010110100,
156 0b10000110000011,
157 0b10111011011010,
158 0b10101111101101,
159 ], [
160 // H
161 0b1011010001001,
162 0b1001110111110,
163 0b1110011100111,
164 0b1100111010000,
165 0b11101100010,
166 0b1001010101,
167 0b110100001100,
168 0b100000111011,
169 ]];
170 /**
171 * The current ECC level value
172 *
173 * L: 0b01
174 * M: 0b00
175 * Q: 0b11
176 * H: 0b10
177 */
178 private int $eccLevel;
179 /**
180 * @param int $eccLevel containing the two bits encoding a QR Code's error correction level
181 *
182 * @todo: accept string values (PHP8+)
183 * @see https://github.com/chillerlan/php-qrcode/discussions/160
184 *
185 * @throws \chillerlan\QRCode\QRCodeException
186 */
187 public function __construct(int $eccLevel)
188 {
189 if ((0b11 & $eccLevel) !== $eccLevel) {
190 throw new QRCodeException('invalid ECC level');
191 }
192 $this->eccLevel = $eccLevel;
193 }
194 /**
195 * returns the string representation of the current ECC level
196 */
197 public function __toString() : string
198 {
199 return [self::L => 'L', self::M => 'M', self::Q => 'Q', self::H => 'H'][$this->eccLevel];
200 }
201 /**
202 * returns the current ECC level
203 */
204 public function getLevel() : int
205 {
206 return $this->eccLevel;
207 }
208 /**
209 * returns the ordinal value of the current ECC level
210 *
211 * references to the keys of the following tables:
212 *
213 * @see \chillerlan\QRCode\Common\EccLevel::MAX_BITS
214 * @see \chillerlan\QRCode\Common\EccLevel::FORMAT_PATTERN
215 * @see \chillerlan\QRCode\Common\Version::RSBLOCKS
216 */
217 public function getOrdinal() : int
218 {
219 return [self::L => 0, self::M => 1, self::Q => 2, self::H => 3][$this->eccLevel];
220 }
221 /**
222 * returns the format pattern for the given $eccLevel and $maskPattern
223 */
224 public function getformatPattern(MaskPattern $maskPattern) : int
225 {
226 return self::FORMAT_PATTERN[$this->getOrdinal()][$maskPattern->getPattern()];
227 }
228 /**
229 * returns an array with the max bit lengths for version 1-40 and the current ECC level
230 *
231 * @return int[]
232 */
233 public function getMaxBits() : array
234 {
235 $col = array_column(self::MAX_BITS, $this->getOrdinal());
236 unset($col[0]);
237 // remove the inavlid index 0
238 return $col;
239 }
240 /**
241 * Returns the maximum bit length for the given version and current ECC level
242 */
243 public function getMaxBitsForVersion(Version $version) : int
244 {
245 return self::MAX_BITS[$version->getVersionNumber()][$this->getOrdinal()];
246 }
247 }
248