|
|
@@ -26,22 +26,26 @@ final class ECI extends QRDataModeAbstract{
|
|
|
public const DATAMODE = Mode::ECI;
|
|
|
|
|
|
/**
|
|
|
- * The current ECI encoding id
|
|
|
+ * The current ECI encoding id (default to UTF-8)
|
|
|
*/
|
|
|
- private int $encoding;
|
|
|
+ private int $encoding = ECICharset::ISO_IEC_10646_UTF_8;
|
|
|
|
|
|
/**
|
|
|
* @inheritDoc
|
|
|
* @throws \chillerlan\QRCode\Data\QRCodeDataException
|
|
|
* @noinspection PhpMissingParentConstructorInspection
|
|
|
*/
|
|
|
- public function __construct(int $encoding){
|
|
|
+ public function __construct(int|null $encoding = null){
|
|
|
|
|
|
- if($encoding < 0 || $encoding > 999999){
|
|
|
- throw new QRCodeDataException(sprintf('invalid encoding id: "%s"', $encoding));
|
|
|
+ if($encoding !== null){
|
|
|
+
|
|
|
+ if($encoding < 0 || $encoding > 999999){
|
|
|
+ throw new QRCodeDataException(sprintf('invalid encoding id: "%s"', $encoding));
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->encoding = $encoding;
|
|
|
}
|
|
|
|
|
|
- $this->encoding = $encoding;
|
|
|
}
|
|
|
|
|
|
public function getLengthInBits():int{
|
|
|
@@ -76,7 +80,7 @@ final class ECI extends QRDataModeAbstract{
|
|
|
$bitBuffer->put(($this->encoding | 0xC00000), 24);
|
|
|
}
|
|
|
else{
|
|
|
- throw new QRCodeDataException('invalid ECI ID');
|
|
|
+ throw new QRCodeDataException('invalid ECI ID'); // @codeCoverageIgnore
|
|
|
}
|
|
|
|
|
|
return $this;
|
|
|
@@ -87,7 +91,7 @@ final class ECI extends QRDataModeAbstract{
|
|
|
*
|
|
|
* @throws \chillerlan\QRCode\Data\QRCodeDataException
|
|
|
*/
|
|
|
- public static function parseValue(BitBuffer $bitBuffer):ECICharset{
|
|
|
+ public function parseValue(BitBuffer $bitBuffer):ECICharset{
|
|
|
$firstByte = $bitBuffer->read(8);
|
|
|
|
|
|
// just one byte
|
|
|
@@ -121,23 +125,25 @@ final class ECI extends QRDataModeAbstract{
|
|
|
*
|
|
|
* @throws \chillerlan\QRCode\Data\QRCodeDataException
|
|
|
*/
|
|
|
- public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):string{
|
|
|
- $eciCharset = self::parseValue($bitBuffer);
|
|
|
+ public function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):string{
|
|
|
+ $eciCharset = $this->parseValue($bitBuffer);
|
|
|
$nextMode = $bitBuffer->read(4);
|
|
|
$encoding = $eciCharset->getName();
|
|
|
|
|
|
// this is definitely weird, but there are QR Codes out in the wild
|
|
|
// that have ECI followed by numeric and alphanum segments
|
|
|
// @see https://github.com/chillerlan/php-qrcode/discussions/289
|
|
|
- $data = match($nextMode){
|
|
|
- Mode::NUMBER => Number::decodeSegment($bitBuffer, $versionNumber),
|
|
|
- Mode::ALPHANUM => AlphaNum::decodeSegment($bitBuffer, $versionNumber),
|
|
|
- Mode::BYTE => Byte::decodeSegment($bitBuffer, $versionNumber),
|
|
|
+ $dataMode = match($nextMode){
|
|
|
+ Mode::NUMBER => new Number,
|
|
|
+ Mode::ALPHANUM => new AlphaNum,
|
|
|
+ Mode::BYTE => new Byte,
|
|
|
default => throw new QRCodeDataException(
|
|
|
sprintf('ECI designator followed by invalid mode: "%04b"', $nextMode),
|
|
|
),
|
|
|
};
|
|
|
|
|
|
+ $data = $dataMode->decodeSegment($bitBuffer, $versionNumber);
|
|
|
+
|
|
|
if($encoding === null){
|
|
|
// The spec isn't clear on this mode; see
|
|
|
// section 6.4.5: it does not say which encoding to assuming
|