Debian Codegraph @

vendor/chillerlan/php-qrcode/src/Data/Byte.php

raw
1 <?php
2 /**
3 * Class Byte
4 *
5 * @created 25.11.2015
6 * @author Smiley <[email protected]>
7 * @copyright 2015 Smiley
8 * @license MIT
9 */
10 declare(strict_types=1);
11
12 namespace chillerlan\QRCode\Data;
13
14 use chillerlan\QRCode\Common\{BitBuffer, Mode};
15 use function chr, ord;
16
17 /**
18 * 8-bit Byte mode, ISO-8859-1 or UTF-8
19 *
20 * ISO/IEC 18004:2000 Section 8.3.4
21 * ISO/IEC 18004:2000 Section 8.4.4
22 */
23 final class Byte extends QRDataModeAbstract{
24
25 public const DATAMODE = Mode::BYTE;
26
27 public function getLengthInBits():int{
28 return ($this->getCharCount() * 8);
29 }
30
31 public static function validateString(string $string):bool{
32 return $string !== '';
33 }
34
35 public function write(BitBuffer $bitBuffer, int $versionNumber):static{
36 $len = $this->getCharCount();
37
38 $bitBuffer
39 ->put(self::DATAMODE, 4)
40 ->put($len, $this->getLengthBits($versionNumber))
41 ;
42
43 $i = 0;
44
45 while($i < $len){
46 $bitBuffer->put(ord($this->data[$i]), 8);
47 $i++;
48 }
49
50 return $this;
51 }
52
53 /**
54 * @inheritDoc
55 *
56 * @throws \chillerlan\QRCode\Data\QRCodeDataException
57 */
58 public function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):string{
59 $length = $bitBuffer->read($this->getLengthBits($versionNumber));
60
61 if($bitBuffer->available() < (8 * $length)){
62 throw new QRCodeDataException('not enough bits available'); // @codeCoverageIgnore
63 }
64
65 $readBytes = '';
66
67 for($i = 0; $i < $length; $i++){
68 $readBytes .= chr($bitBuffer->read(8));
69 }
70
71 return $readBytes;
72 }
73
74 }