@@ -26,22 +26,26 @@ final class ECI extends QRDataModeAbstract{
2626 public const DATAMODE = Mode::ECI ;
2727
2828 /**
29- * The current ECI encoding id
29+ * The current ECI encoding id (default to UTF-8)
3030 */
31- private int $ encoding ;
31+ private int $ encoding = ECICharset:: ISO_IEC_10646_UTF_8 ;
3232
3333 /**
3434 * @inheritDoc
3535 * @throws \chillerlan\QRCode\Data\QRCodeDataException
3636 * @noinspection PhpMissingParentConstructorInspection
3737 */
38- public function __construct (int $ encoding ){
38+ public function __construct (int | null $ encoding = null ){
3939
40- if ($ encoding < 0 || $ encoding > 999999 ){
41- throw new QRCodeDataException (sprintf ('invalid encoding id: "%s" ' , $ encoding ));
40+ if ($ encoding !== null ){
41+
42+ if ($ encoding < 0 || $ encoding > 999999 ){
43+ throw new QRCodeDataException (sprintf ('invalid encoding id: "%s" ' , $ encoding ));
44+ }
45+
46+ $ this ->encoding = $ encoding ;
4247 }
4348
44- $ this ->encoding = $ encoding ;
4549 }
4650
4751 public function getLengthInBits ():int {
@@ -76,7 +80,7 @@ public function write(BitBuffer $bitBuffer, int $versionNumber):static{
7680 $ bitBuffer ->put (($ this ->encoding | 0xC00000 ), 24 );
7781 }
7882 else {
79- throw new QRCodeDataException ('invalid ECI ID ' );
83+ throw new QRCodeDataException ('invalid ECI ID ' ); // @codeCoverageIgnore
8084 }
8185
8286 return $ this ;
@@ -87,7 +91,7 @@ public function write(BitBuffer $bitBuffer, int $versionNumber):static{
8791 *
8892 * @throws \chillerlan\QRCode\Data\QRCodeDataException
8993 */
90- public static function parseValue (BitBuffer $ bitBuffer ):ECICharset {
94+ public function parseValue (BitBuffer $ bitBuffer ):ECICharset {
9195 $ firstByte = $ bitBuffer ->read (8 );
9296
9397 // just one byte
@@ -121,23 +125,25 @@ public static function validateString(string $string):bool{
121125 *
122126 * @throws \chillerlan\QRCode\Data\QRCodeDataException
123127 */
124- public static function decodeSegment (BitBuffer $ bitBuffer , int $ versionNumber ):string {
125- $ eciCharset = self :: parseValue ($ bitBuffer );
128+ public function decodeSegment (BitBuffer $ bitBuffer , int $ versionNumber ):string {
129+ $ eciCharset = $ this -> parseValue ($ bitBuffer );
126130 $ nextMode = $ bitBuffer ->read (4 );
127131 $ encoding = $ eciCharset ->getName ();
128132
129133 // this is definitely weird, but there are QR Codes out in the wild
130134 // that have ECI followed by numeric and alphanum segments
131135 // @see https://github.com/chillerlan/php-qrcode/discussions/289
132- $ data = match ($ nextMode ){
133- Mode::NUMBER => Number:: decodeSegment ( $ bitBuffer , $ versionNumber ) ,
134- Mode::ALPHANUM => AlphaNum:: decodeSegment ( $ bitBuffer , $ versionNumber ) ,
135- Mode::BYTE => Byte:: decodeSegment ( $ bitBuffer , $ versionNumber ) ,
136+ $ dataMode = match ($ nextMode ){
137+ Mode::NUMBER => new Number ,
138+ Mode::ALPHANUM => new AlphaNum ,
139+ Mode::BYTE => new Byte ,
136140 default => throw new QRCodeDataException (
137141 sprintf ('ECI designator followed by invalid mode: "%04b" ' , $ nextMode ),
138142 ),
139143 };
140144
145+ $ data = $ dataMode ->decodeSegment ($ bitBuffer , $ versionNumber );
146+
141147 if ($ encoding === null ){
142148 // The spec isn't clear on this mode; see
143149 // section 6.4.5: it does not say which encoding to assuming
0 commit comments