-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathKanjiData.php
More file actions
68 lines (51 loc) · 1.52 KB
/
Copy pathKanjiData.php
File metadata and controls
68 lines (51 loc) · 1.52 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
<?php
declare(strict_types=1);
namespace GlobusStudio\QRCode\Data;
use GlobusStudio\QRCode\Encoder\BitBuffer;
final class KanjiData extends AbstractQRData
{
public function __construct(string $data)
{
if (!self::isValid($data)) {
throw new \InvalidArgumentException('Data is not valid Shift-JIS Kanji');
}
parent::__construct(self::MODE_KANJI, $data);
}
public static function isValid(string $data): bool
{
$len = strlen($data);
if ($len === 0 || $len % 2 !== 0) {
return false;
}
$i = 0;
while ($i + 1 < $len) {
$c = ((0xFF & ord($data[$i])) << 8) | (0xFF & ord($data[$i + 1]));
if (!(0x8140 <= $c && $c <= 0x9FFC) && !(0xE040 <= $c && $c <= 0xEBBF)) {
return false;
}
$i += 2;
}
return true;
}
public function getLength(): int
{
return (int) (strlen($this->data) / 2);
}
public function write(BitBuffer $buffer): void
{
$data = $this->getData();
$len = strlen($data);
$i = 0;
while ($i + 1 < $len) {
$c = ((0xFF & ord($data[$i])) << 8) | (0xFF & ord($data[$i + 1]));
if (0x8140 <= $c && $c <= 0x9FFC) {
$c -= 0x8140;
} elseif (0xE040 <= $c && $c <= 0xEBBF) {
$c -= 0xC140;
}
$c = (($c >> 8) & 0xFF) * 0xC0 + ($c & 0xFF);
$buffer->put($c, 13);
$i += 2;
}
}
}