| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class Byte |
| 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 chr, ord; |
| 16 |
/** |
| 17 |
* 8-bit Byte mode, ISO-8859-1 or UTF-8 |
| 18 |
* |
| 19 |
* ISO/IEC 18004:2000 Section 8.3.4 |
| 20 |
* ISO/IEC 18004:2000 Section 8.4.4 |
| 21 |
*/ |
| 22 |
final class Byte extends QRDataModeAbstract |
| 23 |
{ |
| 24 |
/** |
| 25 |
* @inheritDoc |
| 26 |
*/ |
| 27 |
public const DATAMODE = Mode::BYTE; |
| 28 |
/** |
| 29 |
* @inheritDoc |
| 30 |
*/ |
| 31 |
public function getLengthInBits() : int |
| 32 |
{ |
| 33 |
return $this->getCharCount() * 8; |
| 34 |
} |
| 35 |
/** |
| 36 |
* @inheritDoc |
| 37 |
*/ |
| 38 |
public static function validateString(string $string) : bool |
| 39 |
{ |
| 40 |
return $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 |
while ($i < $len) { |
| 51 |
$bitBuffer->put(ord($this->data[$i]), 8); |
| 52 |
$i++; |
| 53 |
} |
| 54 |
return $this; |
| 55 |
} |
| 56 |
/** |
| 57 |
* @inheritDoc |
| 58 |
* |
| 59 |
* @throws \chillerlan\QRCode\Data\QRCodeDataException |
| 60 |
*/ |
| 61 |
public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber) : string |
| 62 |
{ |
| 63 |
$length = $bitBuffer->read(self::getLengthBits($versionNumber)); |
| 64 |
if ($bitBuffer->available() < 8 * $length) { |
| 65 |
throw new QRCodeDataException('not enough bits available'); |
| 66 |
// @codeCoverageIgnore |
| 67 |
} |
| 68 |
$readBytes = ''; |
| 69 |
for ($i = 0; $i < $length; $i++) { |
| 70 |
$readBytes .= chr($bitBuffer->read(8)); |
| 71 |
} |
| 72 |
return $readBytes; |
| 73 |
} |
| 74 |
} |
| 75 |
|