-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathVersion.php
More file actions
200 lines (176 loc) · 7.65 KB
/
Copy pathVersion.php
File metadata and controls
200 lines (176 loc) · 7.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
declare(strict_types=1);
namespace GlobusStudio\QRCode\Util;
use GlobusStudio\QRCode\Data\AbstractQRData;
use GlobusStudio\QRCode\ErrorCorrection\ErrorCorrectionLevel;
final class Version
{
private const MAX_LENGTH = [
[[41,25,17,10],[34,20,14,8],[27,16,11,7],[17,10,7,4]],
[[77,47,32,20],[63,38,26,16],[48,29,20,12],[34,20,14,8]],
[[127,77,53,32],[101,61,42,26],[77,47,32,20],[58,35,24,15]],
[[187,114,78,48],[149,90,62,38],[111,67,46,28],[82,50,34,21]],
[[255,154,106,65],[202,122,84,52],[144,87,60,37],[106,64,44,27]],
[[322,195,134,82],[255,154,106,65],[178,108,74,45],[139,84,58,36]],
[[370,224,154,95],[293,178,122,75],[207,125,86,53],[154,93,64,39]],
[[461,279,192,118],[365,221,152,93],[259,157,108,66],[202,122,84,52]],
[[552,335,230,141],[432,262,180,111],[312,189,130,80],[235,143,98,60]],
[[652,395,271,167],[513,311,213,131],[364,221,151,93],[288,174,119,74]],
];
private const PATTERN_POSITION = [
[],
[6, 18],
[6, 22],
[6, 26],
[6, 30],
[6, 34],
[6, 22, 38],
[6, 24, 42],
[6, 26, 46],
[6, 28, 50],
[6, 30, 54],
[6, 32, 58],
[6, 34, 62],
[6, 26, 46, 66],
[6, 26, 48, 70],
[6, 26, 50, 74],
[6, 30, 54, 78],
[6, 30, 56, 82],
[6, 30, 58, 86],
[6, 34, 62, 90],
[6, 28, 50, 72, 94],
[6, 26, 50, 74, 98],
[6, 30, 54, 78, 102],
[6, 28, 54, 80, 106],
[6, 32, 58, 84, 110],
[6, 30, 58, 86, 114],
[6, 34, 62, 90, 118],
[6, 26, 50, 74, 98, 122],
[6, 30, 54, 78, 102, 126],
[6, 26, 52, 78, 104, 130],
[6, 30, 56, 82, 108, 134],
[6, 34, 60, 86, 112, 138],
[6, 30, 58, 86, 114, 142],
[6, 34, 62, 90, 118, 146],
[6, 30, 54, 78, 102, 126, 150],
[6, 24, 50, 76, 102, 128, 154],
[6, 28, 54, 80, 106, 132, 158],
[6, 32, 58, 84, 110, 136, 162],
[6, 26, 54, 82, 110, 138, 166],
[6, 30, 58, 86, 114, 142, 170],
];
/**
* @return int[]
*/
public static function getPatternPosition(int $version): array
{
return self::PATTERN_POSITION[$version - 1];
}
public static function getMaxLength(int $version, int $mode, int $errorCorrectionLevel): int
{
$t = $version - 1;
if ($t < 0 || $t >= 10) {
return self::getMaxLengthExtended($version, $mode, $errorCorrectionLevel);
}
$e = self::ecLevelToIndex($errorCorrectionLevel);
$m = self::modeToIndex($mode);
return self::MAX_LENGTH[$t][$e][$m];
}
public static function getMinimumVersion(int $length, int $mode, int $errorCorrectionLevel): int
{
for ($version = 1; $version <= 40; $version++) {
if ($length <= self::getMaxLength($version, $mode, $errorCorrectionLevel)) {
return $version;
}
}
throw new \OverflowException('Data too long for any QR Code version');
}
private static function getMaxLengthExtended(int $version, int $mode, int $errorCorrectionLevel): int
{
if ($version < 1 || $version > 40) {
throw new \InvalidArgumentException("Invalid version: $version");
}
$e = self::ecLevelToIndex($errorCorrectionLevel);
$m = self::modeToIndex($mode);
$capacityTable = self::buildCapacityTable();
return $capacityTable[$version - 1][$e][$m];
}
/**
* @return int[][][]
*/
private static function buildCapacityTable(): array
{
/** @var int[][][]|null $table */
static $table = null;
if ($table !== null) {
return $table;
}
$table = self::MAX_LENGTH;
$extended = [
[[772,468,321,198],[604,366,251,155],[427,259,177,109],[331,200,137,85]],
[[883,535,367,226],[691,419,287,177],[489,296,203,125],[374,227,155,96]],
[[1022,619,425,262],[796,483,331,204],[580,352,241,149],[427,259,177,109]],
[[1101,667,458,282],[871,528,362,223],[621,376,258,159],[468,283,194,120]],
[[1250,758,520,320],[991,600,412,254],[703,426,292,180],[530,321,220,136]],
[[1408,854,586,361],[1082,656,450,277],[775,470,322,198],[602,365,250,154]],
[[1548,938,644,397],[1212,734,504,310],[876,531,364,224],[674,408,280,173]],
[[1725,1046,718,442],[1346,816,560,345],[948,574,394,243],[746,452,310,191]],
[[1903,1153,792,488],[1500,909,624,384],[1063,644,442,272],[813,493,338,208]],
[[2061,1249,858,528],[1600,970,666,410],[1159,702,482,297],[919,557,382,235]],
[[2232,1352,929,572],[1708,1035,711,438],[1224,742,509,314],[969,587,403,248]],
[[2409,1460,1003,618],[1872,1134,779,480],[1358,823,565,348],[1056,640,439,270]],
[[2620,1588,1091,672],[2059,1248,857,528],[1468,890,611,376],[1108,672,461,284]],
[[2812,1704,1171,721],[2188,1326,911,561],[1588,963,661,407],[1228,744,511,315]],
[[3057,1853,1273,784],[2395,1451,997,614],[1718,1041,715,440],[1286,779,535,330]],
[[3283,1990,1367,842],[2544,1542,1059,652],[1804,1094,751,462],[1425,864,593,365]],
[[3517,2132,1465,902],[2701,1637,1125,692],[1933,1172,805,496],[1501,910,625,385]],
[[3669,2223,1528,940],[2857,1732,1190,732],[2085,1263,868,534],[1581,958,658,405]],
[[3909,2369,1628,1002],[3035,1839,1264,778],[2181,1322,908,559],[1677,1016,698,430]],
[[4158,2520,1732,1066],[3289,1994,1370,843],[2358,1429,982,604],[1782,1080,742,457]],
[[4417,2677,1840,1132],[3486,2113,1452,894],[2473,1499,1030,634],[1897,1150,790,486]],
[[4686,2840,1952,1201],[3693,2238,1538,947],[2670,1618,1112,684],[2022,1226,842,518]],
[[4965,3009,2068,1273],[3909,2369,1628,1002],[2805,1700,1168,719],[2157,1307,898,553]],
[[5253,3183,2188,1347],[4134,2506,1722,1060],[2949,1787,1228,756],[2301,1394,958,590]],
[[5529,3351,2303,1417],[4343,2632,1809,1113],[3081,1867,1283,790],[2361,1431,983,605]],
[[5836,3537,2431,1496],[4588,2780,1911,1176],[3244,1966,1351,832],[2524,1530,1051,647]],
[[6153,3729,2563,1577],[4775,2894,1989,1224],[3417,2071,1423,876],[2625,1591,1093,673]],
[[6479,3927,2699,1661],[5039,3054,2099,1292],[3599,2181,1499,923],[2735,1658,1139,701]],
[[6743,4087,2809,1729],[5313,3220,2213,1362],[3791,2298,1579,972],[2927,1774,1219,750]],
[[7089,4296,2953,1817],[5596,3391,2331,1435],[3993,2420,1663,1024],[3057,1852,1273,784]],
];
foreach ($extended as $row) {
$table[] = $row;
}
return $table;
}
private static function ecLevelToIndex(int $level): int
{
switch ($level) {
case ErrorCorrectionLevel::L:
return 0;
case ErrorCorrectionLevel::M:
return 1;
case ErrorCorrectionLevel::Q:
return 2;
case ErrorCorrectionLevel::H:
return 3;
default:
throw new \InvalidArgumentException("Invalid error correction level: $level");
}
}
private static function modeToIndex(int $mode): int
{
switch ($mode) {
case AbstractQRData::MODE_NUMBER:
return 0;
case AbstractQRData::MODE_ALPHA_NUM:
return 1;
case AbstractQRData::MODE_8BIT_BYTE:
return 2;
case AbstractQRData::MODE_KANJI:
return 3;
default:
throw new \InvalidArgumentException("Invalid mode: $mode");
}
}
}