Debian Codegraph @

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

raw
1 <?php
2 /**
3 * Class Number
4 *
5 * @created 26.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 ceil, intdiv, preg_match, substr, unpack;
16
17 /**
18 * Numeric mode: decimal digits 0 to 9
19 *
20 * ISO/IEC 18004:2000 Section 8.3.2
21 * ISO/IEC 18004:2000 Section 8.4.2
22 */
23 final class Number extends QRDataModeAbstract{
24
25 public const DATAMODE = Mode::NUMBER;
26
27 public function getLengthInBits():int{
28 return (int)ceil($this->getCharCount() * (10 / 3));
29 }
30
31 public static function validateString(string $string):bool{
32 return (bool)preg_match('/^\d+$/', $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 // encode numeric triplets in 10 bits
46 while(($i + 2) < $len){
47 $bitBuffer->put($this->parseInt(substr($this->data, $i, 3)), 10);
48 $i += 3;
49 }
50
51 if($i < $len){
52
53 // encode 2 remaining numbers in 7 bits
54 if(($len - $i) === 2){
55 $bitBuffer->put($this->parseInt(substr($this->data, $i, 2)), 7);
56 }
57 // encode one remaining number in 4 bits
58 elseif(($len - $i) === 1){
59 $bitBuffer->put($this->parseInt(substr($this->data, $i, 1)), 4);
60 }
61
62 }
63
64 return $this;
65 }
66
67 /**
68 * get the code for the given numeric string
69 *
70 * @throws \chillerlan\QRCode\Data\QRCodeDataException
71 */
72 private function parseInt(string $string):int{
73 $num = 0;
74
75 $ords = unpack('C*', $string);
76
77 if($ords === false){
78 throw new QRCodeDataException('unpack() error');
79 }
80
81 foreach($ords as $ord){
82 $num = ($num * 10 + $ord - 48);
83 }
84
85 return $num;
86 }
87
88 /**
89 * @inheritDoc
90 *
91 * @throws \chillerlan\QRCode\Data\QRCodeDataException
92 */
93 public function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):string{
94 $length = $bitBuffer->read($this->getLengthBits($versionNumber));
95 $result = '';
96 // Read three digits at a time
97 while($length >= 3){
98 // Each 10 bits encodes three digits
99 if($bitBuffer->available() < 10){
100 throw new QRCodeDataException('not enough bits available'); // @codeCoverageIgnore
101 }
102
103 $threeDigitsBits = $bitBuffer->read(10);
104
105 if($threeDigitsBits >= 1000){
106 throw new QRCodeDataException('error decoding numeric value');
107 }
108
109 $result .= intdiv($threeDigitsBits, 100);
110 $result .= (intdiv($threeDigitsBits, 10) % 10);
111 $result .= ($threeDigitsBits % 10);
112
113 $length -= 3;
114 }
115
116 if($length === 2){
117 // Two digits left over to read, encoded in 7 bits
118 if($bitBuffer->available() < 7){
119 throw new QRCodeDataException('not enough bits available'); // @codeCoverageIgnore
120 }
121
122 $twoDigitsBits = $bitBuffer->read(7);
123
124 if($twoDigitsBits >= 100){
125 throw new QRCodeDataException('error decoding numeric value');
126 }
127
128 $result .= intdiv($twoDigitsBits, 10);
129 $result .= ($twoDigitsBits % 10);
130 }
131 elseif($length === 1){
132 // One digit left over to read
133 if($bitBuffer->available() < 4){
134 throw new QRCodeDataException('not enough bits available'); // @codeCoverageIgnore
135 }
136
137 $digitBits = $bitBuffer->read(4);
138
139 if($digitBits >= 10){
140 throw new QRCodeDataException('error decoding numeric value');
141 }
142
143 $result .= $digitBits;
144 }
145
146 return $result;
147 }
148
149 }