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