mirror of
https://github.com/chillerlan/php-qrcode.git
synced 2026-10-20 18:56:20 +00:00
:octocat: +data mode decodeSegment() (WIP)
This commit is contained in:
+46
-1
@@ -14,7 +14,7 @@ namespace chillerlan\QRCode\Data;
|
||||
|
||||
use chillerlan\QRCode\Common\{BitBuffer, Mode};
|
||||
|
||||
use function ceil, ord, sprintf, str_split;
|
||||
use function array_flip, ceil, ord, sprintf, str_split;
|
||||
|
||||
/**
|
||||
* Alphanumeric mode: 0 to 9, A to Z, space, $ % * + - . / :
|
||||
@@ -98,4 +98,49 @@ final class AlphaNum extends QRDataModeAbstract{
|
||||
return self::CHAR_MAP_ALPHANUM[$chr];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*
|
||||
* @throws \chillerlan\QRCode\Data\QRCodeDataException
|
||||
*/
|
||||
public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):string{
|
||||
$length = $bitBuffer->read(Mode::getLengthBitsForVersion(self::$datamode, $versionNumber));
|
||||
$charmap = array_flip(self::CHAR_MAP_ALPHANUM);
|
||||
|
||||
// @todo
|
||||
$toAlphaNumericChar = function(int $ord) use ($charmap):string{
|
||||
|
||||
if(isset($charmap[$ord])){
|
||||
return $charmap[$ord];
|
||||
}
|
||||
|
||||
throw new QRCodeDataException('invalid character value: '.$ord);
|
||||
};
|
||||
|
||||
$result = '';
|
||||
// Read two characters at a time
|
||||
while($length > 1){
|
||||
|
||||
if($bitBuffer->available() < 11){
|
||||
throw new QRCodeDataException('not enough bits available');
|
||||
}
|
||||
|
||||
$nextTwoCharsBits = $bitBuffer->read(11);
|
||||
$result .= $toAlphaNumericChar($nextTwoCharsBits / 45);
|
||||
$result .= $toAlphaNumericChar($nextTwoCharsBits % 45);
|
||||
$length -= 2;
|
||||
}
|
||||
|
||||
if($length === 1){
|
||||
// special case: one character left
|
||||
if($bitBuffer->available() < 6){
|
||||
throw new QRCodeDataException('not enough bits available');
|
||||
}
|
||||
|
||||
$result .= $toAlphaNumericChar($bitBuffer->read(6));
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user