| 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 |
|