Skip to content

Commit 3e0a465

Browse files
committed
:octocat: use a proper constant for data mode
1 parent f13300c commit 3e0a465

File tree

11 files changed

+40
-35
lines changed

11 files changed

+40
-35
lines changed

src/Data/AlphaNum.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ final class AlphaNum extends QRDataModeAbstract{
3939
/**
4040
* @inheritDoc
4141
*/
42-
protected static int $datamode = Mode::ALPHANUM;
42+
public const DATAMODE = Mode::ALPHANUM;
4343

4444
/**
4545
* @inheritDoc
@@ -73,7 +73,7 @@ public function write(BitBuffer $bitBuffer, int $versionNumber):QRDataModeInterf
7373
$len = $this->getCharCount();
7474

7575
$bitBuffer
76-
->put($this::$datamode, 4)
76+
->put(self::DATAMODE, 4)
7777
->put($len, $this::getLengthBits($versionNumber))
7878
;
7979

src/Data/Byte.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ final class Byte extends QRDataModeAbstract{
2525
/**
2626
* @inheritDoc
2727
*/
28-
protected static int $datamode = Mode::BYTE;
28+
public const DATAMODE = Mode::BYTE;
2929

3030
/**
3131
* @inheritDoc
@@ -48,7 +48,7 @@ public function write(BitBuffer $bitBuffer, int $versionNumber):QRDataModeInterf
4848
$len = $this->getCharCount();
4949

5050
$bitBuffer
51-
->put($this::$datamode, 4)
51+
->put(self::DATAMODE, 4)
5252
->put($len, $this::getLengthBits($versionNumber))
5353
;
5454

src/Data/ECI.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ final class ECI extends QRDataModeAbstract{
2525
/**
2626
* @inheritDoc
2727
*/
28-
protected static int $datamode = Mode::ECI;
28+
public const DATAMODE = Mode::ECI;
2929

3030
/**
3131
* The current ECI encoding id
@@ -67,7 +67,7 @@ public function getLengthInBits():int{
6767
* @inheritDoc
6868
*/
6969
public function write(BitBuffer $bitBuffer, int $versionNumber):QRDataModeInterface{
70-
$bitBuffer->put($this::$datamode, 4);
70+
$bitBuffer->put(self::DATAMODE, 4);
7171

7272
if($this->encoding < 128){
7373
$bitBuffer->put($this->encoding, 8);

src/Data/Hanzi.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,24 @@
3030
*/
3131
final class Hanzi extends QRDataModeAbstract{
3232

33-
// GB2312, GB18030
34-
public const ENCODING = 'GB18030';
35-
public const GB2312_SUBSET = 0b0001; // other subsets???
33+
/**
34+
* possible values: GB2312, GB18030
35+
*
36+
* @var string
37+
*/
38+
public const ENCODING = 'GB18030';
39+
40+
/**
41+
* @todo: other subsets???
42+
*
43+
* @var int
44+
*/
45+
public const GB2312_SUBSET = 0b0001;
3646

3747
/**
3848
* @inheritDoc
3949
*/
40-
protected static int $datamode = Mode::HANZI;
50+
public const DATAMODE = Mode::HANZI;
4151

4252
/**
4353
* @inheritDoc
@@ -123,7 +133,7 @@ public static function validateString(string $string):bool{
123133
public function write(BitBuffer $bitBuffer, int $versionNumber):QRDataModeInterface{
124134

125135
$bitBuffer
126-
->put($this::$datamode, 4)
136+
->put(self::DATAMODE, 4)
127137
->put($this::GB2312_SUBSET, 4)
128138
->put($this->getCharCount(), $this::getLengthBits($versionNumber))
129139
;

src/Data/Kanji.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,19 @@
2828
*/
2929
final class Kanji extends QRDataModeAbstract{
3030

31-
// SJIS, SJIS-2004
32-
// SJIS-2004 may produce errors in PHP < 8
31+
/**
32+
* possible values: SJIS, SJIS-2004
33+
*
34+
* SJIS-2004 may produce errors in PHP < 8
35+
*
36+
* @var string
37+
*/
3338
public const ENCODING = 'SJIS';
3439

3540
/**
3641
* @inheritDoc
3742
*/
38-
protected static int $datamode = Mode::KANJI;
43+
public const DATAMODE = Mode::KANJI;
3944

4045
/**
4146
* @inheritDoc
@@ -121,7 +126,7 @@ public static function validateString(string $string):bool{
121126
public function write(BitBuffer $bitBuffer, int $versionNumber):QRDataModeInterface{
122127

123128
$bitBuffer
124-
->put($this::$datamode, 4)
129+
->put(self::DATAMODE, 4)
125130
->put($this->getCharCount(), $this::getLengthBits($versionNumber))
126131
;
127132

src/Data/Number.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ final class Number extends QRDataModeAbstract{
3232
/**
3333
* @inheritDoc
3434
*/
35-
protected static int $datamode = Mode::NUMBER;
35+
public const DATAMODE = Mode::NUMBER;
3636

3737
/**
3838
* @inheritDoc
@@ -66,7 +66,7 @@ public function write(BitBuffer $bitBuffer, int $versionNumber):QRDataModeInterf
6666
$len = $this->getCharCount();
6767

6868
$bitBuffer
69-
->put($this::$datamode, 4)
69+
->put(self::DATAMODE, 4)
7070
->put($len, $this::getLengthBits($versionNumber))
7171
;
7272

src/Data/QRData.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ private function estimateTotalBitLength():int{
140140

141141
foreach($this->dataSegments as $segment){
142142
// data length in bits of the current segment +4 bits for each mode descriptor
143-
$length += ($segment->getLengthInBits() + Mode::getLengthBitsForVersion($segment->getDataMode(), 1) + 4);
143+
$length += ($segment->getLengthInBits() + Mode::getLengthBitsForVersion($segment::DATAMODE, 1) + 4);
144144

145145
if(!$segment instanceof ECI){
146146
// mode length bits margin to the next breakpoint

src/Data/QRDataModeAbstract.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@
1717
*/
1818
abstract class QRDataModeAbstract implements QRDataModeInterface{
1919

20-
/**
21-
* the current data mode: Num, Alphanum, Kanji, Hanzi, Byte
22-
*/
23-
protected static int $datamode;
24-
2520
/**
2621
* The data to write
2722
*/
@@ -49,13 +44,6 @@ protected function getCharCount():int{
4944
return strlen($this->data);
5045
}
5146

52-
/**
53-
* @inheritDoc
54-
*/
55-
public function getDataMode():int{
56-
return $this::$datamode;
57-
}
58-
5947
/**
6048
* @inheritDoc
6149
*/
@@ -67,7 +55,7 @@ public static function convertEncoding(string $string):string{
6755
* shortcut
6856
*/
6957
protected static function getLengthBits(int $versionNumber):int{
70-
return Mode::getLengthBitsForVersion(static::$datamode, $versionNumber);
58+
return Mode::getLengthBitsForVersion(static::DATAMODE, $versionNumber);
7159
}
7260

7361
}

src/Data/QRDataModeInterface.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
interface QRDataModeInterface{
1919

2020
/**
21-
* returns the current data mode constant
21+
* the current data mode: Number, Alphanum, Kanji, Hanzi, Byte, ECI
22+
*
23+
* @var int
2224
*/
23-
public function getDataMode():int;
25+
public const DATAMODE = -1;
2426

2527
/**
2628
* retruns the length in bits of the data string

tests/Data/DataInterfaceTestAbstract.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public function testDecodeSegment(int $version):void{
132132
// get the filled bitbuffer
133133
$bitBuffer = $this->QRData->getBitBuffer();
134134
// read the first 4 bits
135-
$this::assertSame($datamodeInterface->getDataMode(), $bitBuffer->read(4));
135+
$this::assertSame($datamodeInterface::DATAMODE, $bitBuffer->read(4));
136136
// decode the data
137137
/** @noinspection PhpUndefinedMethodInspection */
138138
$this::assertSame($this->testdata, $this->FQN::decodeSegment($bitBuffer, $options->version));

0 commit comments

Comments
 (0)