Просмотр исходного кода

:octocat: use a proper constant for data mode

smiley 2 лет назад
Родитель
Сommit
3e0a4655aa

+ 2 - 2
src/Data/AlphaNum.php

@@ -39,7 +39,7 @@ final class AlphaNum extends QRDataModeAbstract{
 	/**
 	/**
 	 * @inheritDoc
 	 * @inheritDoc
 	 */
 	 */
-	protected static int $datamode = Mode::ALPHANUM;
+	public const DATAMODE = Mode::ALPHANUM;
 
 
 	/**
 	/**
 	 * @inheritDoc
 	 * @inheritDoc
@@ -73,7 +73,7 @@ final class AlphaNum extends QRDataModeAbstract{
 		$len = $this->getCharCount();
 		$len = $this->getCharCount();
 
 
 		$bitBuffer
 		$bitBuffer
-			->put($this::$datamode, 4)
+			->put(self::DATAMODE, 4)
 			->put($len, $this::getLengthBits($versionNumber))
 			->put($len, $this::getLengthBits($versionNumber))
 		;
 		;
 
 

+ 2 - 2
src/Data/Byte.php

@@ -25,7 +25,7 @@ final class Byte extends QRDataModeAbstract{
 	/**
 	/**
 	 * @inheritDoc
 	 * @inheritDoc
 	 */
 	 */
-	protected static int $datamode = Mode::BYTE;
+	public const DATAMODE = Mode::BYTE;
 
 
 	/**
 	/**
 	 * @inheritDoc
 	 * @inheritDoc
@@ -48,7 +48,7 @@ final class Byte extends QRDataModeAbstract{
 		$len = $this->getCharCount();
 		$len = $this->getCharCount();
 
 
 		$bitBuffer
 		$bitBuffer
-			->put($this::$datamode, 4)
+			->put(self::DATAMODE, 4)
 			->put($len, $this::getLengthBits($versionNumber))
 			->put($len, $this::getLengthBits($versionNumber))
 		;
 		;
 
 

+ 2 - 2
src/Data/ECI.php

@@ -25,7 +25,7 @@ final class ECI extends QRDataModeAbstract{
 	/**
 	/**
 	 * @inheritDoc
 	 * @inheritDoc
 	 */
 	 */
-	protected static int $datamode = Mode::ECI;
+	public const DATAMODE = Mode::ECI;
 
 
 	/**
 	/**
 	 * The current ECI encoding id
 	 * The current ECI encoding id
@@ -67,7 +67,7 @@ final class ECI extends QRDataModeAbstract{
 	 * @inheritDoc
 	 * @inheritDoc
 	 */
 	 */
 	public function write(BitBuffer $bitBuffer, int $versionNumber):QRDataModeInterface{
 	public function write(BitBuffer $bitBuffer, int $versionNumber):QRDataModeInterface{
-		$bitBuffer->put($this::$datamode, 4);
+		$bitBuffer->put(self::DATAMODE, 4);
 
 
 		if($this->encoding < 128){
 		if($this->encoding < 128){
 			$bitBuffer->put($this->encoding, 8);
 			$bitBuffer->put($this->encoding, 8);

+ 15 - 5
src/Data/Hanzi.php

@@ -30,14 +30,24 @@ use function chr, implode, is_string, mb_convert_encoding, mb_detect_encoding,
  */
  */
 final class Hanzi extends QRDataModeAbstract{
 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
 	 * @inheritDoc
 	 */
 	 */
-	protected static int $datamode = Mode::HANZI;
+	public const DATAMODE = Mode::HANZI;
 
 
 	/**
 	/**
 	 * @inheritDoc
 	 * @inheritDoc
@@ -123,7 +133,7 @@ final class Hanzi extends QRDataModeAbstract{
 	public function write(BitBuffer $bitBuffer, int $versionNumber):QRDataModeInterface{
 	public function write(BitBuffer $bitBuffer, int $versionNumber):QRDataModeInterface{
 
 
 		$bitBuffer
 		$bitBuffer
-			->put($this::$datamode, 4)
+			->put(self::DATAMODE, 4)
 			->put($this::GB2312_SUBSET, 4)
 			->put($this::GB2312_SUBSET, 4)
 			->put($this->getCharCount(), $this::getLengthBits($versionNumber))
 			->put($this->getCharCount(), $this::getLengthBits($versionNumber))
 		;
 		;

+ 9 - 4
src/Data/Kanji.php

@@ -28,14 +28,19 @@ use function chr, implode, is_string, mb_convert_encoding, mb_detect_encoding,
  */
  */
 final class Kanji extends QRDataModeAbstract{
 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';
 	public const ENCODING = 'SJIS';
 
 
 	/**
 	/**
 	 * @inheritDoc
 	 * @inheritDoc
 	 */
 	 */
-	protected static int $datamode = Mode::KANJI;
+	public const DATAMODE = Mode::KANJI;
 
 
 	/**
 	/**
 	 * @inheritDoc
 	 * @inheritDoc
@@ -121,7 +126,7 @@ final class Kanji extends QRDataModeAbstract{
 	public function write(BitBuffer $bitBuffer, int $versionNumber):QRDataModeInterface{
 	public function write(BitBuffer $bitBuffer, int $versionNumber):QRDataModeInterface{
 
 
 		$bitBuffer
 		$bitBuffer
-			->put($this::$datamode, 4)
+			->put(self::DATAMODE, 4)
 			->put($this->getCharCount(), $this::getLengthBits($versionNumber))
 			->put($this->getCharCount(), $this::getLengthBits($versionNumber))
 		;
 		;
 
 

+ 2 - 2
src/Data/Number.php

@@ -32,7 +32,7 @@ final class Number extends QRDataModeAbstract{
 	/**
 	/**
 	 * @inheritDoc
 	 * @inheritDoc
 	 */
 	 */
-	protected static int $datamode = Mode::NUMBER;
+	public const DATAMODE = Mode::NUMBER;
 
 
 	/**
 	/**
 	 * @inheritDoc
 	 * @inheritDoc
@@ -66,7 +66,7 @@ final class Number extends QRDataModeAbstract{
 		$len = $this->getCharCount();
 		$len = $this->getCharCount();
 
 
 		$bitBuffer
 		$bitBuffer
-			->put($this::$datamode, 4)
+			->put(self::DATAMODE, 4)
 			->put($len, $this::getLengthBits($versionNumber))
 			->put($len, $this::getLengthBits($versionNumber))
 		;
 		;
 
 

+ 1 - 1
src/Data/QRData.php

@@ -140,7 +140,7 @@ final class QRData{
 
 
 		foreach($this->dataSegments as $segment){
 		foreach($this->dataSegments as $segment){
 			// data length in bits of the current segment +4 bits for each mode descriptor
 			// 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){
 			if(!$segment instanceof ECI){
 				// mode length bits margin to the next breakpoint
 				// mode length bits margin to the next breakpoint

+ 1 - 13
src/Data/QRDataModeAbstract.php

@@ -17,11 +17,6 @@ use chillerlan\QRCode\Common\Mode;
  */
  */
 abstract class QRDataModeAbstract implements QRDataModeInterface{
 abstract class QRDataModeAbstract implements QRDataModeInterface{
 
 
-	/**
-	 * the current data mode: Num, Alphanum, Kanji, Hanzi, Byte
-	 */
-	protected static int $datamode;
-
 	/**
 	/**
 	 * The data to write
 	 * The data to write
 	 */
 	 */
@@ -49,13 +44,6 @@ abstract class QRDataModeAbstract implements QRDataModeInterface{
 		return strlen($this->data);
 		return strlen($this->data);
 	}
 	}
 
 
-	/**
-	 * @inheritDoc
-	 */
-	public function getDataMode():int{
-		return $this::$datamode;
-	}
-
 	/**
 	/**
 	 * @inheritDoc
 	 * @inheritDoc
 	 */
 	 */
@@ -67,7 +55,7 @@ abstract class QRDataModeAbstract implements QRDataModeInterface{
 	 * shortcut
 	 * shortcut
 	 */
 	 */
 	protected static function getLengthBits(int $versionNumber):int{
 	protected static function getLengthBits(int $versionNumber):int{
-		return Mode::getLengthBitsForVersion(static::$datamode, $versionNumber);
+		return Mode::getLengthBitsForVersion(static::DATAMODE, $versionNumber);
 	}
 	}
 
 
 }
 }

+ 4 - 2
src/Data/QRDataModeInterface.php

@@ -18,9 +18,11 @@ use chillerlan\QRCode\Common\BitBuffer;
 interface QRDataModeInterface{
 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
 	 * retruns the length in bits of the data string

+ 1 - 1
tests/Data/DataInterfaceTestAbstract.php

@@ -132,7 +132,7 @@ abstract class DataInterfaceTestAbstract extends TestCase{
 		// get the filled bitbuffer
 		// get the filled bitbuffer
 		$bitBuffer = $this->QRData->getBitBuffer();
 		$bitBuffer = $this->QRData->getBitBuffer();
 		// read the first 4 bits
 		// read the first 4 bits
-		$this::assertSame($datamodeInterface->getDataMode(), $bitBuffer->read(4));
+		$this::assertSame($datamodeInterface::DATAMODE, $bitBuffer->read(4));
 		// decode the data
 		// decode the data
 		/** @noinspection PhpUndefinedMethodInspection */
 		/** @noinspection PhpUndefinedMethodInspection */
 		$this::assertSame($this->testdata, $this->FQN::decodeSegment($bitBuffer, $options->version));
 		$this::assertSame($this->testdata, $this->FQN::decodeSegment($bitBuffer, $options->version));

+ 1 - 1
tests/Data/ECITest.php

@@ -84,7 +84,7 @@ final class ECITest extends DataInterfaceTestAbstract{
 		// get the filled bitbuffer
 		// get the filled bitbuffer
 		$bitBuffer = $this->QRData->getBitBuffer();
 		$bitBuffer = $this->QRData->getBitBuffer();
 		// read the first 4 bits
 		// read the first 4 bits
-		$this::assertSame($segments[0]->getDataMode(), $bitBuffer->read(4));
+		$this::assertSame($segments[0]::DATAMODE, $bitBuffer->read(4));
 		// decode the data
 		// decode the data
 		/** @noinspection PhpUndefinedMethodInspection */
 		/** @noinspection PhpUndefinedMethodInspection */
 		$this::assertSame($this->testdata, $this->FQN::decodeSegment($bitBuffer, $options->version));
 		$this::assertSame($this->testdata, $this->FQN::decodeSegment($bitBuffer, $options->version));