PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.20
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.20
1.10.20 1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 All 164 releases
woocommerce-pos / vendor_prefixed / chillerlan / php-qrcode / src / Data / Number.php

Number.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.20, at vendor_prefixed/chillerlan/php-qrcode/src/Data/Number.php

134 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class Number
5 *
6 * @created 26.11.2015
7 * @author Smiley <[email protected]>
8 * @copyright 2015 Smiley
9 * @license MIT
10 */
11 namespace WCPOS\Vendor\chillerlan\QRCode\Data;
12
13 use WCPOS\Vendor\chillerlan\QRCode\Common\BitBuffer;
14 use WCPOS\Vendor\chillerlan\QRCode\Common\Mode;
15 use function ceil, intdiv, substr, unpack;
16 /**
17 * Numeric mode: decimal digits 0 to 9
18 *
19 * ISO/IEC 18004:2000 Section 8.3.2
20 * ISO/IEC 18004:2000 Section 8.4.2
21 */
22 final class Number extends QRDataModeAbstract
23 {
24 /**
25 * @inheritDoc
26 */
27 public const DATAMODE = Mode::NUMBER;
28 /**
29 * @inheritDoc
30 */
31 public function getLengthInBits() : int
32 {
33 return (int) ceil($this->getCharCount() * (10 / 3));
34 }
35 /**
36 * @inheritDoc
37 */
38 public static function validateString(string $string) : bool
39 {
40 return (bool) \preg_match('/^\\d+$/', $string);
41 }
42 /**
43 * @inheritDoc
44 */
45 public function write(BitBuffer $bitBuffer, int $versionNumber) : QRDataModeInterface
46 {
47 $len = $this->getCharCount();
48 $bitBuffer->put(self::DATAMODE, 4)->put($len, $this::getLengthBits($versionNumber));
49 $i = 0;
50 // encode numeric triplets in 10 bits
51 while ($i + 2 < $len) {
52 $bitBuffer->put($this->parseInt(substr($this->data, $i, 3)), 10);
53 $i += 3;
54 }
55 if ($i < $len) {
56 // encode 2 remaining numbers in 7 bits
57 if ($len - $i === 2) {
58 $bitBuffer->put($this->parseInt(substr($this->data, $i, 2)), 7);
59 } elseif ($len - $i === 1) {
60 $bitBuffer->put($this->parseInt(substr($this->data, $i, 1)), 4);
61 }
62 }
63 return $this;
64 }
65 /**
66 * get the code for the given numeric string
67 *
68 * @throws \chillerlan\QRCode\Data\QRCodeDataException
69 */
70 private function parseInt(string $string) : int
71 {
72 $num = 0;
73 $ords = unpack('C*', $string);
74 if ($ords === \false) {
75 throw new QRCodeDataException('unpack() error');
76 }
77 foreach ($ords as $ord) {
78 $num = $num * 10 + $ord - 48;
79 }
80 return $num;
81 }
82 /**
83 * @inheritDoc
84 *
85 * @throws \chillerlan\QRCode\Data\QRCodeDataException
86 */
87 public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber) : string
88 {
89 $length = $bitBuffer->read(self::getLengthBits($versionNumber));
90 $result = '';
91 // Read three digits at a time
92 while ($length >= 3) {
93 // Each 10 bits encodes three digits
94 if ($bitBuffer->available() < 10) {
95 throw new QRCodeDataException('not enough bits available');
96 // @codeCoverageIgnore
97 }
98 $threeDigitsBits = $bitBuffer->read(10);
99 if ($threeDigitsBits >= 1000) {
100 throw new QRCodeDataException('error decoding numeric value');
101 }
102 $result .= intdiv($threeDigitsBits, 100);
103 $result .= intdiv($threeDigitsBits, 10) % 10;
104 $result .= $threeDigitsBits % 10;
105 $length -= 3;
106 }
107 if ($length === 2) {
108 // Two digits left over to read, encoded in 7 bits
109 if ($bitBuffer->available() < 7) {
110 throw new QRCodeDataException('not enough bits available');
111 // @codeCoverageIgnore
112 }
113 $twoDigitsBits = $bitBuffer->read(7);
114 if ($twoDigitsBits >= 100) {
115 throw new QRCodeDataException('error decoding numeric value');
116 }
117 $result .= intdiv($twoDigitsBits, 10);
118 $result .= $twoDigitsBits % 10;
119 } elseif ($length === 1) {
120 // One digit left over to read
121 if ($bitBuffer->available() < 4) {
122 throw new QRCodeDataException('not enough bits available');
123 // @codeCoverageIgnore
124 }
125 $digitBits = $bitBuffer->read(4);
126 if ($digitBits >= 10) {
127 throw new QRCodeDataException('error decoding numeric value');
128 }
129 $result .= $digitBits;
130 }
131 return $result;
132 }
133 }
134