1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class Byte |
4
|
|
|
* |
5
|
|
|
* @filesource Byte.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\Common\{BitBuffer, Mode}; |
16
|
|
|
|
17
|
|
|
use function ord; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Byte mode, ISO-8859-1 or UTF-8 |
21
|
|
|
* |
22
|
|
|
* ISO/IEC 18004:2000 Section 8.3.4 |
23
|
|
|
* ISO/IEC 18004:2000 Section 8.4.4 |
24
|
|
|
*/ |
25
|
|
|
final class Byte extends QRDataModeAbstract{ |
26
|
|
|
|
27
|
|
|
protected static int $datamode = Mode::DATA_BYTE; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @inheritdoc |
31
|
|
|
*/ |
32
|
|
|
public function getLengthInBits():int{ |
33
|
|
|
return $this->getCharCount() * 8; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @inheritdoc |
38
|
|
|
*/ |
39
|
|
|
public static function validateString(string $string):bool{ |
40
|
|
|
return !empty($string); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @inheritdoc |
45
|
|
|
*/ |
46
|
|
|
public function write(BitBuffer $bitBuffer, int $versionNumber):void{ |
47
|
|
|
$len = $this->getCharCount(); |
48
|
|
|
|
49
|
|
|
$bitBuffer |
50
|
|
|
->put($this::$datamode, 4) |
51
|
|
|
->put($len, Mode::getLengthBitsForVersion($this::$datamode, $versionNumber)) |
52
|
|
|
; |
53
|
|
|
|
54
|
|
|
$i = 0; |
55
|
|
|
|
56
|
|
|
while($i < $len){ |
57
|
|
|
$bitBuffer->put(ord($this->data[$i]), 8); |
58
|
|
|
$i++; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @inheritdoc |
65
|
|
|
* |
66
|
|
|
* @throws \chillerlan\QRCode\Data\QRCodeDataException |
67
|
|
|
*/ |
68
|
|
|
public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):string{ |
69
|
|
|
$length = $bitBuffer->read(Mode::getLengthBitsForVersion(self::$datamode, $versionNumber)); |
70
|
|
|
|
71
|
|
|
if($bitBuffer->available() < 8 * $length){ |
72
|
|
|
throw new QRCodeDataException('not enough bits available'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$readBytes = ''; |
76
|
|
|
|
77
|
|
|
for($i = 0; $i < $length; $i++){ |
78
|
|
|
$readBytes .= \chr($bitBuffer->read(8)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $readBytes; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
} |
85
|
|
|
|