1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class Util |
4
|
|
|
* |
5
|
|
|
* @filesource Util.php |
6
|
|
|
* @created 25.11.2015 |
7
|
|
|
* @package chillerlan\QRCode |
8
|
|
|
* @author Smiley <[email protected]> |
9
|
|
|
* @copyright 2015 Smiley |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace chillerlan\QRCode; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* |
17
|
|
|
*/ |
18
|
|
|
class Util{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param string $s |
22
|
|
|
* |
23
|
|
|
* @return bool |
24
|
|
|
*/ |
25
|
|
|
public static function isNumber($s){ |
26
|
|
|
|
27
|
|
|
$len = strlen($s); |
28
|
|
|
for($i = 0; $i < $len; $i++){ |
29
|
|
|
$c = ord($s[$i]); |
30
|
|
|
|
31
|
|
|
if(!(ord('0') <= $c && $c <= ord('9'))){ |
32
|
|
|
return false; |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return true; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $s |
41
|
|
|
* |
42
|
|
|
* @return bool |
43
|
|
|
*/ |
44
|
|
|
public static function isAlphaNum($s){ |
45
|
|
|
|
46
|
|
|
$len = strlen($s); |
47
|
|
|
for($i = 0; $i < $len; $i++){ |
48
|
|
|
$c = ord($s[$i]); |
49
|
|
|
|
50
|
|
|
if(!(ord('0') <= $c && $c <= ord('9')) && !(ord('A') <= $c && $c <= ord('Z')) && strpos(' $%*+-./:', $s[$i]) === false){ |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return true; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $s |
60
|
|
|
* |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
|
|
public static function isKanji($s){ |
64
|
|
|
|
65
|
|
|
if(empty($s)){ |
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$i = 0; |
70
|
|
|
$len = strlen($s); |
71
|
|
|
while($i + 1 < $len){ |
72
|
|
|
$c = ((0xff&ord($s[$i])) << 8)|(0xff&ord($s[$i + 1])); |
73
|
|
|
|
74
|
|
|
if(!($c >= 0x8140 && $c <= 0x9FFC) && !($c >= 0xE040 && $c <= 0xEBBF)){ |
75
|
|
|
return false; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$i += 2; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return !($i < $len); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param int $data |
86
|
|
|
* |
87
|
|
|
* @return int |
88
|
|
|
*/ |
89
|
|
View Code Duplication |
public static function getBCHTypeInfo($data){ |
|
|
|
|
90
|
|
|
$d = $data << 10; |
91
|
|
|
|
92
|
|
|
while(self::getBCHDigit($d) - self::getBCHDigit(QRConst::G15) >= 0){ |
93
|
|
|
$d ^= (QRConst::G15 << (self::getBCHDigit($d) - self::getBCHDigit(QRConst::G15))); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return (($data << 10)|$d)^QRConst::G15_MASK; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param int $data |
101
|
|
|
* |
102
|
|
|
* @return int |
103
|
|
|
*/ |
104
|
|
View Code Duplication |
public static function getBCHTypeNumber($data){ |
|
|
|
|
105
|
|
|
$d = $data << 12; |
106
|
|
|
|
107
|
|
|
while(self::getBCHDigit($d) - self::getBCHDigit(QRConst::G18) >= 0){ |
108
|
|
|
$d ^= (QRConst::G18 << (self::getBCHDigit($d) - self::getBCHDigit(QRConst::G18))); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return ($data << 12)|$d; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param int $data |
116
|
|
|
* |
117
|
|
|
* @return int |
118
|
|
|
*/ |
119
|
|
|
public static function getBCHDigit($data){ |
120
|
|
|
$digit = 0; |
121
|
|
|
|
122
|
|
|
while($data !== 0){ |
123
|
|
|
$digit++; |
124
|
|
|
$data >>= 1; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $digit; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param int $typeNumber |
132
|
|
|
* @param int $errorCorrectLevel |
133
|
|
|
* |
134
|
|
|
* @return array |
135
|
|
|
* @throws \chillerlan\QRCode\QRCodeException |
136
|
|
|
*/ |
137
|
|
|
public static function getRSBlocks($typeNumber, $errorCorrectLevel){ |
138
|
|
|
// PHP5 compat |
139
|
|
|
$RSBLOCK = QRConst::RSBLOCK; |
140
|
|
|
$BLOCK_TABLE = QRConst::BLOCK_TABLE; |
141
|
|
|
|
142
|
|
|
if(!isset($RSBLOCK[$errorCorrectLevel])){ |
143
|
|
|
throw new QRCodeException('$typeNumber: '.$typeNumber.' / $errorCorrectLevel: '.$errorCorrectLevel); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$rsBlock = $BLOCK_TABLE[($typeNumber - 1) * 4 + $RSBLOCK[$errorCorrectLevel]]; |
147
|
|
|
|
148
|
|
|
$list = []; |
149
|
|
|
$length = count($rsBlock) / 3; |
150
|
|
|
for($i = 0; $i < $length; $i++){ |
151
|
|
|
for($j = 0; $j < $rsBlock[$i * 3 + 0]; $j++){ |
152
|
|
|
$list[] = [$rsBlock[$i * 3 + 1], $rsBlock[$i * 3 + 2]]; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $list; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param int $typeNumber |
161
|
|
|
* @param int $mode |
162
|
|
|
* @param int $ecLevel |
163
|
|
|
* |
164
|
|
|
* @return int |
165
|
|
|
* @throws \chillerlan\QRCode\QRCodeException |
166
|
|
|
*/ |
167
|
|
|
public static function getMaxLength($typeNumber, $mode, $ecLevel){ |
168
|
|
|
$RSBLOCK = QRConst::RSBLOCK; |
169
|
|
|
$MAX_LENGTH = QRConst::MAX_LENGTH; |
170
|
|
|
$MODE = QRConst::MODE; |
171
|
|
|
|
172
|
|
|
if(!isset($RSBLOCK[$ecLevel])){ |
173
|
|
|
throw new QRCodeException('Invalid error correct level: '.$ecLevel); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
if(!isset($MODE[$mode])){ |
177
|
|
|
throw new QRCodeException('Invalid mode: '.$mode); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $MAX_LENGTH[$typeNumber - 1][$RSBLOCK[$ecLevel]][$MODE[$mode]]; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.