Completed
Push — master ( 1a2d4b...99084f )
by smiley
03:12
created

Util::getBCHDigit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
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
		$len = strlen($s);
27
		$i = 0;
28
29
		while($i < $len){
30
			$c = ord($s[$i]);
31
32
			if(!(ord('0') <= $c && $c <= ord('9'))){
33
				return false;
34
			}
35
			
36
			$i++;
37
		}
38
39
		return true;
40
	}
41
42
	/**
43
	 * @param string $s
44
	 *
45
	 * @return bool
46
	 */
47
	public static function isAlphaNum($s){
48
		$len = strlen($s);
49
		$i = 0;
50
51
		while($i < $len){
52
			$c = ord($s[$i]);
53
54
			if(!(ord('0') <= $c && $c <= ord('9')) && !(ord('A') <= $c && $c <= ord('Z')) && strpos(' $%*+-./:', $s[$i]) === false){
55
				return false;
56
			}
57
58
			$i++;
59
		}
60
61
		return true;
62
	}
63
64
	/**
65
	 * @param string $s
66
	 *
67
	 * @return bool
68
	 */
69
	public static function isKanji($s){
70
71
		if(empty($s)){
72
			return false;
73
		}
74
75
		$i = 0;
76
		$len = strlen($s);
77
		while($i + 1 < $len){
78
			$c = ((0xff&ord($s[$i])) << 8)|(0xff&ord($s[$i + 1]));
79
80
			if(!($c >= 0x8140 && $c <= 0x9FFC) && !($c >= 0xE040 && $c <= 0xEBBF)){
81
				return false;
82
			}
83
84
			$i += 2;
85
		}
86
87
		return !($i < $len);
88
	}
89
90
	/**
91
	 * @param int $data
92
	 *
93
	 * @return int
94
	 */
95
	public static function getBCHTypeInfo($data){
96
		return (($data << 10)|self::getBCHT($data, 10, QRConst::G15))^QRConst::G15_MASK;
97
	}
98
99
	/**
100
	 * @param int $data
101
	 *
102
	 * @return int
103
	 */
104
	public static function getBCHTypeNumber($data){
105
		return ($data << 12)|self::getBCHT($data, 12, QRConst::G18);
106
	}
107
108
	/**
109
	 * @param int $data
110
	 * @param int $bits
111
	 * @param int $mask
112
	 *
113
	 * @return int
114
	 */
115
	protected static function getBCHT($data, $bits, $mask){
116
		$d = $data << $bits;
117
118
		while(self::getBCHDigit($d) - self::getBCHDigit($mask) >= 0){
119
			$d ^= ($mask << (self::getBCHDigit($d) - self::getBCHDigit($mask)));
120
		}
121
122
		return $d;
123
	}
124
125
	/**
126
	 * @param int $data
127
	 *
128
	 * @return int
129
	 */
130
	public static function getBCHDigit($data){
131
		$digit = 0;
132
133
		while($data !== 0){
134
			$digit++;
135
			$data >>= 1;
136
		}
137
138
		return $digit;
139
	}
140
141
	/**
142
	 * @param int $typeNumber
143
	 * @param int $errorCorrectLevel
144
	 *
145
	 * @return array
146
	 * @throws \chillerlan\QRCode\QRCodeException
147
	 */
148
	public static function getRSBlocks($typeNumber, $errorCorrectLevel){
149
150
		if(!array_key_exists($errorCorrectLevel, QRConst::RSBLOCK)){
151
			throw new QRCodeException('$typeNumber: '.$typeNumber.' / $errorCorrectLevel: '.$errorCorrectLevel);
152
		}
153
154
		$rsBlock = QRConst::BLOCK_TABLE[($typeNumber - 1) * 4 + QRConst::RSBLOCK[$errorCorrectLevel]];
155
		$list = [];
156
		$length = count($rsBlock) / 3;
157
		$i = $j = 0;
158
159
		while($i < $length){
160
			while($j < $rsBlock[$i * 3 + 0]){
161
				$list[] = [$rsBlock[$i * 3 + 1], $rsBlock[$i * 3 + 2]];
162
				$j++;
163
			}
164
			$i++;
165
		}
166
167
		return $list;
168
	}
169
170
	/**
171
	 * @param int $typeNumber
172
	 * @param int $mode
173
	 * @param int $ecLevel
174
	 *
175
	 * @return int
176
	 * @throws \chillerlan\QRCode\QRCodeException
177
	 */
178
	public static function getMaxLength($typeNumber, $mode, $ecLevel){
179
180
		if(!array_key_exists($ecLevel, QRConst::RSBLOCK)){
181
			throw new QRCodeException('Invalid error correct level: '.$ecLevel);
182
		}
183
184
		if(!array_key_exists($mode, QRConst::MODE)){
185
			throw new QRCodeException('Invalid mode: '.$mode);
186
		}
187
188
		return QRConst::MAX_LENGTH[$typeNumber - 1][QRConst::RSBLOCK[$ecLevel]][QRConst::MODE[$mode]];
189
	}
190
191
192
}
193