Passed
Push — main ( b62539...4e03cd )
by smiley
01:53
created

Kanji::getLength()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Class Kanji
4
 *
5
 * @created      25.11.2015
6
 * @author       Smiley <[email protected]>
7
 * @copyright    2015 Smiley
8
 * @license      MIT
9
 */
10
11
namespace chillerlan\QRCode\Data;
12
13
use chillerlan\QRCode\Common\{BitBuffer, Mode};
14
15
use function chr, implode, mb_convert_encoding, mb_detect_encoding, mb_internal_encoding, mb_strlen, ord, sprintf, strlen;
16
17
/**
18
 * Kanji mode: double-byte characters from the Shift JIS character set
19
 *
20
 * ISO/IEC 18004:2000 Section 8.3.5
21
 * ISO/IEC 18004:2000 Section 8.4.5
22
 */
23
final class Kanji extends QRDataModeAbstract{
24
25
	protected static int $datamode = Mode::DATA_KANJI;
26
27
	public function __construct(string $data){
28
		parent::__construct($data);
29
30
		/** @noinspection PhpFieldAssignmentTypeMismatchInspection */
31
		$this->data = mb_convert_encoding($this->data, 'SJIS', mb_detect_encoding($this->data));
32
	}
33
34
	/**
35
	 * @inheritdoc
36
	 */
37
	protected function getCharCount():int{
38
		return mb_strlen($this->data, 'SJIS');
39
	}
40
41
	/**
42
	 * @inheritdoc
43
	 */
44
	public function getLengthInBits():int{
45
		return $this->getCharCount() * 13;
46
	}
47
48
	/**
49
	 * checks if a string qualifies as Kanji
50
	 */
51
	public static function validateString(string $string):bool{
52
		$i   = 0;
53
		$len = strlen($string);
54
55
		while($i + 1 < $len){
56
			$c = ((0xff & ord($string[$i])) << 8) | (0xff & ord($string[$i + 1]));
57
58
			if(!($c >= 0x8140 && $c <= 0x9ffc) && !($c >= 0xe040 && $c <= 0xebbf)){
59
				return false;
60
			}
61
62
			$i += 2;
63
		}
64
65
		return $i >= $len;
66
	}
67
68
	/**
69
	 * @inheritdoc
70
	 *
71
	 * @throws \chillerlan\QRCode\Data\QRCodeDataException on an illegal character occurence
72
	 */
73
	public function write(BitBuffer $bitBuffer, int $versionNumber):void{
74
75
		$bitBuffer
76
			->put($this::$datamode, 4)
77
			->put($this->getCharCount(), Mode::getLengthBitsForVersion($this::$datamode, $versionNumber))
78
		;
79
80
		$len = strlen($this->data);
81
82
		for($i = 0; $i + 1 < $len; $i += 2){
83
			$c = ((0xff & ord($this->data[$i])) << 8) | (0xff & ord($this->data[$i + 1]));
84
85
			if($c >= 0x8140 && $c <= 0x9ffC){
86
				$c -= 0x8140;
87
			}
88
			elseif($c >= 0xe040 && $c <= 0xebbf){
89
				$c -= 0xc140;
90
			}
91
			else{
92
				throw new QRCodeDataException(sprintf('illegal char at %d [%d]', $i + 1, $c));
93
			}
94
95
			$bitBuffer->put(((($c >> 8) & 0xff) * 0xc0) + ($c & 0xff), 13);
96
		}
97
98
		if($i < $len){
99
			throw new QRCodeDataException(sprintf('illegal char at %d', $i + 1));
100
		}
101
102
	}
103
104
	/**
105
	 * @inheritdoc
106
	 *
107
	 * @throws \chillerlan\QRCode\Data\QRCodeDataException
108
	 */
109
	public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):string{
110
		$length = $bitBuffer->read(Mode::getLengthBitsForVersion(self::$datamode, $versionNumber));
111
112
		if($bitBuffer->available() < $length * 13){
113
			throw new QRCodeDataException('not enough bits available');
114
		}
115
116
		$buffer = [];
117
		$offset = 0;
118
119
		while($length > 0){
120
			// Each 13 bits encodes a 2-byte character
121
			$twoBytes          = $bitBuffer->read(13);
122
			$assembledTwoBytes = (($twoBytes / 0x0c0) << 8) | ($twoBytes % 0x0c0);
123
124
			$assembledTwoBytes += ($assembledTwoBytes < 0x01f00)
125
				? 0x08140  // In the 0x8140 to 0x9FFC range
126
				: 0x0c140; // In the 0xE040 to 0xEBBF range
127
128
			$buffer[$offset]     = chr(0xff & ($assembledTwoBytes >> 8));
129
			$buffer[$offset + 1] = chr(0xff & $assembledTwoBytes);
130
			$offset              += 2;
131
			$length--;
132
		}
133
134
		return mb_convert_encoding(implode($buffer), mb_internal_encoding(), 'SJIS');
0 ignored issues
show
Bug introduced by
It seems like mb_internal_encoding() can also be of type true; however, parameter $to_encoding of mb_convert_encoding() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

134
		return mb_convert_encoding(implode($buffer), /** @scrutinizer ignore-type */ mb_internal_encoding(), 'SJIS');
Loading history...
135
	}
136
137
}
138