mirror of
https://github.com/chillerlan/php-qrcode.git
synced 2026-08-19 02:10:08 +00:00
:octocat: use a proper constant for data mode
This commit is contained in:
@@ -39,7 +39,7 @@ final class AlphaNum extends QRDataModeAbstract{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected static int $datamode = Mode::ALPHANUM;
|
||||
public const DATAMODE = Mode::ALPHANUM;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
@@ -73,7 +73,7 @@ final class AlphaNum extends QRDataModeAbstract{
|
||||
$len = $this->getCharCount();
|
||||
|
||||
$bitBuffer
|
||||
->put($this::$datamode, 4)
|
||||
->put(self::DATAMODE, 4)
|
||||
->put($len, $this::getLengthBits($versionNumber))
|
||||
;
|
||||
|
||||
|
||||
+2
-2
@@ -25,7 +25,7 @@ final class Byte extends QRDataModeAbstract{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected static int $datamode = Mode::BYTE;
|
||||
public const DATAMODE = Mode::BYTE;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
@@ -48,7 +48,7 @@ final class Byte extends QRDataModeAbstract{
|
||||
$len = $this->getCharCount();
|
||||
|
||||
$bitBuffer
|
||||
->put($this::$datamode, 4)
|
||||
->put(self::DATAMODE, 4)
|
||||
->put($len, $this::getLengthBits($versionNumber))
|
||||
;
|
||||
|
||||
|
||||
+2
-2
@@ -25,7 +25,7 @@ final class ECI extends QRDataModeAbstract{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected static int $datamode = Mode::ECI;
|
||||
public const DATAMODE = Mode::ECI;
|
||||
|
||||
/**
|
||||
* The current ECI encoding id
|
||||
@@ -67,7 +67,7 @@ final class ECI extends QRDataModeAbstract{
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function write(BitBuffer $bitBuffer, int $versionNumber):QRDataModeInterface{
|
||||
$bitBuffer->put($this::$datamode, 4);
|
||||
$bitBuffer->put(self::DATAMODE, 4);
|
||||
|
||||
if($this->encoding < 128){
|
||||
$bitBuffer->put($this->encoding, 8);
|
||||
|
||||
+15
-5
@@ -30,14 +30,24 @@ use function chr, implode, is_string, mb_convert_encoding, mb_detect_encoding,
|
||||
*/
|
||||
final class Hanzi extends QRDataModeAbstract{
|
||||
|
||||
// GB2312, GB18030
|
||||
public const ENCODING = 'GB18030';
|
||||
public const GB2312_SUBSET = 0b0001; // other subsets???
|
||||
/**
|
||||
* possible values: GB2312, GB18030
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public const ENCODING = 'GB18030';
|
||||
|
||||
/**
|
||||
* @todo: other subsets???
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public const GB2312_SUBSET = 0b0001;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected static int $datamode = Mode::HANZI;
|
||||
public const DATAMODE = Mode::HANZI;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
@@ -123,7 +133,7 @@ final class Hanzi extends QRDataModeAbstract{
|
||||
public function write(BitBuffer $bitBuffer, int $versionNumber):QRDataModeInterface{
|
||||
|
||||
$bitBuffer
|
||||
->put($this::$datamode, 4)
|
||||
->put(self::DATAMODE, 4)
|
||||
->put($this::GB2312_SUBSET, 4)
|
||||
->put($this->getCharCount(), $this::getLengthBits($versionNumber))
|
||||
;
|
||||
|
||||
+9
-4
@@ -28,14 +28,19 @@ use function chr, implode, is_string, mb_convert_encoding, mb_detect_encoding,
|
||||
*/
|
||||
final class Kanji extends QRDataModeAbstract{
|
||||
|
||||
// SJIS, SJIS-2004
|
||||
// SJIS-2004 may produce errors in PHP < 8
|
||||
/**
|
||||
* possible values: SJIS, SJIS-2004
|
||||
*
|
||||
* SJIS-2004 may produce errors in PHP < 8
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public const ENCODING = 'SJIS';
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected static int $datamode = Mode::KANJI;
|
||||
public const DATAMODE = Mode::KANJI;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
@@ -121,7 +126,7 @@ final class Kanji extends QRDataModeAbstract{
|
||||
public function write(BitBuffer $bitBuffer, int $versionNumber):QRDataModeInterface{
|
||||
|
||||
$bitBuffer
|
||||
->put($this::$datamode, 4)
|
||||
->put(self::DATAMODE, 4)
|
||||
->put($this->getCharCount(), $this::getLengthBits($versionNumber))
|
||||
;
|
||||
|
||||
|
||||
+2
-2
@@ -32,7 +32,7 @@ final class Number extends QRDataModeAbstract{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected static int $datamode = Mode::NUMBER;
|
||||
public const DATAMODE = Mode::NUMBER;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
@@ -66,7 +66,7 @@ final class Number extends QRDataModeAbstract{
|
||||
$len = $this->getCharCount();
|
||||
|
||||
$bitBuffer
|
||||
->put($this::$datamode, 4)
|
||||
->put(self::DATAMODE, 4)
|
||||
->put($len, $this::getLengthBits($versionNumber))
|
||||
;
|
||||
|
||||
|
||||
+1
-1
@@ -140,7 +140,7 @@ final class QRData{
|
||||
|
||||
foreach($this->dataSegments as $segment){
|
||||
// data length in bits of the current segment +4 bits for each mode descriptor
|
||||
$length += ($segment->getLengthInBits() + Mode::getLengthBitsForVersion($segment->getDataMode(), 1) + 4);
|
||||
$length += ($segment->getLengthInBits() + Mode::getLengthBitsForVersion($segment::DATAMODE, 1) + 4);
|
||||
|
||||
if(!$segment instanceof ECI){
|
||||
// mode length bits margin to the next breakpoint
|
||||
|
||||
@@ -17,11 +17,6 @@ use chillerlan\QRCode\Common\Mode;
|
||||
*/
|
||||
abstract class QRDataModeAbstract implements QRDataModeInterface{
|
||||
|
||||
/**
|
||||
* the current data mode: Num, Alphanum, Kanji, Hanzi, Byte
|
||||
*/
|
||||
protected static int $datamode;
|
||||
|
||||
/**
|
||||
* The data to write
|
||||
*/
|
||||
@@ -49,13 +44,6 @@ abstract class QRDataModeAbstract implements QRDataModeInterface{
|
||||
return strlen($this->data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getDataMode():int{
|
||||
return $this::$datamode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
@@ -67,7 +55,7 @@ abstract class QRDataModeAbstract implements QRDataModeInterface{
|
||||
* shortcut
|
||||
*/
|
||||
protected static function getLengthBits(int $versionNumber):int{
|
||||
return Mode::getLengthBitsForVersion(static::$datamode, $versionNumber);
|
||||
return Mode::getLengthBitsForVersion(static::DATAMODE, $versionNumber);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,9 +18,11 @@ use chillerlan\QRCode\Common\BitBuffer;
|
||||
interface QRDataModeInterface{
|
||||
|
||||
/**
|
||||
* returns the current data mode constant
|
||||
* the current data mode: Number, Alphanum, Kanji, Hanzi, Byte, ECI
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public function getDataMode():int;
|
||||
public const DATAMODE = -1;
|
||||
|
||||
/**
|
||||
* retruns the length in bits of the data string
|
||||
|
||||
@@ -132,7 +132,7 @@ abstract class DataInterfaceTestAbstract extends TestCase{
|
||||
// get the filled bitbuffer
|
||||
$bitBuffer = $this->QRData->getBitBuffer();
|
||||
// read the first 4 bits
|
||||
$this::assertSame($datamodeInterface->getDataMode(), $bitBuffer->read(4));
|
||||
$this::assertSame($datamodeInterface::DATAMODE, $bitBuffer->read(4));
|
||||
// decode the data
|
||||
/** @noinspection PhpUndefinedMethodInspection */
|
||||
$this::assertSame($this->testdata, $this->FQN::decodeSegment($bitBuffer, $options->version));
|
||||
|
||||
@@ -84,7 +84,7 @@ final class ECITest extends DataInterfaceTestAbstract{
|
||||
// get the filled bitbuffer
|
||||
$bitBuffer = $this->QRData->getBitBuffer();
|
||||
// read the first 4 bits
|
||||
$this::assertSame($segments[0]->getDataMode(), $bitBuffer->read(4));
|
||||
$this::assertSame($segments[0]::DATAMODE, $bitBuffer->read(4));
|
||||
// decode the data
|
||||
/** @noinspection PhpUndefinedMethodInspection */
|
||||
$this::assertSame($this->testdata, $this->FQN::decodeSegment($bitBuffer, $options->version));
|
||||
|
||||
Reference in New Issue
Block a user