Passed
Push — master ( 514bca...891b04 )
by smiley
02:57
created

Kanji   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 38
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
B write() 0 22 7
A getLength() 0 2 1
1
<?php
2
/**
3
 * Class Kanji
4
 *
5
 * @filesource   Kanji.php
6
 * @created      25.11.2015
7
 * @package      chillerlan\QRCode\Data
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2015 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\QRCode\Data;
14
15
use chillerlan\QRCode\QRCode;
16
17
use function mb_strlen, ord, sprintf, strlen;
18
19
/**
20
 * Kanji mode: double-byte characters from the Shift JIS character set
21
 */
22
class Kanji extends QRDataAbstract{
23
24
	protected int $datamode = QRCode::DATA_KANJI;
25
26
	protected array $lengthBits = [8, 10, 12];
27
28
	/**
29
	 * @inheritdoc
30
	 */
31
	protected function getLength(string $data):int{
32
		return mb_strlen($data, 'SJIS');
33
	}
34
35
	/**
36
	 * @inheritdoc
37
	 */
38
	protected function write(string $data):void{
39
		$len = strlen($data);
40
41
		for($i = 0; $i + 1 < $len; $i += 2){
42
			$c = ((0xff & ord($data[$i])) << 8) | (0xff & ord($data[$i + 1]));
43
44
			if(0x8140 <= $c && $c <= 0x9FFC){
45
				$c -= 0x8140;
46
			}
47
			elseif(0xE040 <= $c && $c <= 0xEBBF){
48
				$c -= 0xC140;
49
			}
50
			else{
51
				throw new QRCodeDataException(sprintf('illegal char at %d [%d]', $i + 1, $c));
52
			}
53
54
			$this->bitBuffer->put((($c >> 8) & 0xff) * 0xC0 + ($c & 0xff), 13);
55
56
		}
57
58
		if($i < $len){
59
			throw new QRCodeDataException(sprintf('illegal char at %d', $i + 1));
60
		}
61
62
	}
63
64
}
65