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 / AlphaNum.php

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

120 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class AlphaNum
5 *
6 * @created 25.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, preg_match, strpos;
16 /**
17 * Alphanumeric mode: 0 to 9, A to Z, space, $ % * + - . / :
18 *
19 * ISO/IEC 18004:2000 Section 8.3.3
20 * ISO/IEC 18004:2000 Section 8.4.3
21 */
22 final class AlphaNum extends QRDataModeAbstract
23 {
24 /**
25 * ISO/IEC 18004:2000 Table 5
26 *
27 * @var string
28 */
29 private const CHAR_MAP = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:';
30 /**
31 * @inheritDoc
32 */
33 public const DATAMODE = Mode::ALPHANUM;
34 /**
35 * @inheritDoc
36 */
37 public function getLengthInBits() : int
38 {
39 return (int) ceil($this->getCharCount() * (11 / 2));
40 }
41 /**
42 * @inheritDoc
43 */
44 public static function validateString(string $string) : bool
45 {
46 return (bool) preg_match('/^[A-Z\\d %$*+\\-.:\\/]+$/', $string);
47 }
48 /**
49 * @inheritDoc
50 */
51 public function write(BitBuffer $bitBuffer, int $versionNumber) : QRDataModeInterface
52 {
53 $len = $this->getCharCount();
54 $bitBuffer->put(self::DATAMODE, 4)->put($len, $this::getLengthBits($versionNumber));
55 // encode 2 characters in 11 bits
56 for ($i = 0; $i + 1 < $len; $i += 2) {
57 $bitBuffer->put($this->ord($this->data[$i]) * 45 + $this->ord($this->data[$i + 1]), 11);
58 }
59 // encode a remaining character in 6 bits
60 if ($i < $len) {
61 $bitBuffer->put($this->ord($this->data[$i]), 6);
62 }
63 return $this;
64 }
65 /**
66 * @inheritDoc
67 *
68 * @throws \chillerlan\QRCode\Data\QRCodeDataException
69 */
70 public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber) : string
71 {
72 $length = $bitBuffer->read(self::getLengthBits($versionNumber));
73 $result = '';
74 // Read two characters at a time
75 while ($length > 1) {
76 if ($bitBuffer->available() < 11) {
77 throw new QRCodeDataException('not enough bits available');
78 // @codeCoverageIgnore
79 }
80 $nextTwoCharsBits = $bitBuffer->read(11);
81 $result .= self::chr(intdiv($nextTwoCharsBits, 45));
82 $result .= self::chr($nextTwoCharsBits % 45);
83 $length -= 2;
84 }
85 if ($length === 1) {
86 // special case: one character left
87 if ($bitBuffer->available() < 6) {
88 throw new QRCodeDataException('not enough bits available');
89 // @codeCoverageIgnore
90 }
91 $result .= self::chr($bitBuffer->read(6));
92 }
93 return $result;
94 }
95 /**
96 * @throws \chillerlan\QRCode\Data\QRCodeDataException
97 */
98 private function ord(string $chr) : int
99 {
100 /** @phan-suppress-next-line PhanParamSuspiciousOrder */
101 $ord = strpos(self::CHAR_MAP, $chr);
102 if ($ord === \false) {
103 throw new QRCodeDataException('invalid character');
104 // @codeCoverageIgnore
105 }
106 return $ord;
107 }
108 /**
109 * @throws \chillerlan\QRCode\Data\QRCodeDataException
110 */
111 private static function chr(int $ord) : string
112 {
113 if ($ord < 0 || $ord > 44) {
114 throw new QRCodeDataException('invalid character code');
115 // @codeCoverageIgnore
116 }
117 return self::CHAR_MAP[$ord];
118 }
119 }
120