Skip to content

Commit 50179a9

Browse files
committed
:octocat: move mask pattern parameter
1 parent 9bc23a2 commit 50179a9

File tree

6 files changed

+38
-28
lines changed

6 files changed

+38
-28
lines changed

src/Data/QRData.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,11 @@ public function setBitBuffer(BitBuffer $bitBuffer):self{
123123
* returns a fresh matrix object with the data written and masked with the given $maskPattern
124124
*/
125125
public function writeMatrix(MaskPattern $maskPattern):QRMatrix{
126-
return (new QRMatrix($this->version, $this->eccLevel, $maskPattern))
126+
return (new QRMatrix($this->version, $this->eccLevel))
127127
->initFunctionalPatterns()
128128
->writeCodewords($this->bitBuffer)
129-
->mask()
129+
->setFormatInfo($maskPattern)
130+
->mask($maskPattern)
130131
;
131132
}
132133

src/Data/QRMatrix.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,23 @@ class QRMatrix{
8383
];
8484

8585
/**
86-
* the used mask pattern, set via QRMatrix::mask()
86+
* the matrix version - always set in QRMatrix, may be null in BitMatrix
8787
*/
88-
protected ?MaskPattern $maskPattern = null;
88+
protected ?Version $version = null;
8989

9090
/**
91-
* the current ECC level
91+
* the current ECC level - always set in QRMatrix, may be null in BitMatrix
9292
*/
9393
protected ?EccLevel $eccLevel = null;
9494

9595
/**
96-
* a Version instance
96+
* the mask pattern that was used in the most recent operation, set via:
97+
*
98+
* - QRMatrix::setFormatInfo()
99+
* - QRMatrix::mask()
100+
* - BitMatrix::readFormatInformation()
97101
*/
98-
protected ?Version $version = null;
102+
protected ?MaskPattern $maskPattern = null;
99103

100104
/**
101105
* the size (side length) of the matrix, including quiet zone (if created)
@@ -112,10 +116,9 @@ class QRMatrix{
112116
/**
113117
* QRMatrix constructor.
114118
*/
115-
public function __construct(Version $version, EccLevel $eccLevel, MaskPattern $maskPattern){
119+
public function __construct(Version $version, EccLevel $eccLevel){
116120
$this->version = $version;
117121
$this->eccLevel = $eccLevel;
118-
$this->maskPattern = $maskPattern;
119122
$this->moduleCount = $this->version->getDimension();
120123
$this->matrix = $this->createMatrix($this->moduleCount, $this::M_NULL);
121124
}
@@ -502,12 +505,16 @@ public function setVersionNumber():self{
502505
}
503506

504507
/**
505-
* Draws the format info along the finder patterns
508+
* Draws the format info along the finder patterns. If no $maskPattern, all format info modules will be set to false.
506509
*
507510
* ISO/IEC 18004:2000 Section 8.9
508511
*/
509-
public function setFormatInfo():self{
510-
$bits = $this->eccLevel->getformatPattern($this->maskPattern);
512+
public function setFormatInfo(MaskPattern $maskPattern = null):self{
513+
$this->maskPattern = $maskPattern;
514+
515+
$bits = ($this->maskPattern instanceof MaskPattern)
516+
? $this->eccLevel->getformatPattern($this->maskPattern)
517+
: 0; // sets all format fields to false (test mode)
511518

512519
for($i = 0; $i < 15; $i++){
513520
$v = (($bits >> $i) & 1) === 1;
@@ -703,8 +710,9 @@ public function writeCodewords(BitBuffer $bitBuffer):self{
703710
*
704711
* ISO/IEC 18004:2000 Section 8.8.1
705712
*/
706-
public function mask():self{
707-
$mask = $this->maskPattern->getMask();
713+
public function mask(MaskPattern $maskPattern):self{
714+
$this->maskPattern = $maskPattern;
715+
$mask = $this->maskPattern->getMask();
708716

709717
foreach($this->matrix as $y => $row){
710718
foreach($row as $x => $val){

src/Decoder/BitMatrix.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,15 @@ public function readCodewords():array{
117117
$this
118118
->readFormatInformation()
119119
->readVersion()
120-
->mask() // reverse the mask pattern
120+
->mask($this->maskPattern) // reverse the mask pattern
121121
;
122122

123123
// invoke a fresh matrix with only the function & format patterns to compare against
124-
$fp = (new QRMatrix($this->version, $this->eccLevel, $this->maskPattern))->initFunctionalPatterns();
124+
$matrix = (new QRMatrix($this->version, $this->eccLevel))
125+
->initFunctionalPatterns()
126+
->setFormatInfo($this->maskPattern)
127+
;
128+
125129
$result = [];
126130
$byte = 0;
127131
$bitsRead = 0;
@@ -143,7 +147,7 @@ public function readCodewords():array{
143147
$x = ($i - $col);
144148

145149
// Ignore bits covered by the function pattern
146-
if($fp->get($x, $y) !== $this::M_NULL){
150+
if($matrix->get($x, $y) !== $this::M_NULL){
147151
continue;
148152
}
149153

src/Decoder/DecoderResult.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,11 @@ public function hasStructuredAppend():bool{
8888
* Returns a QRMatrix instance with thesettings and data of the reader result
8989
*/
9090
public function getQRMatrix():QRMatrix{
91-
return (new QRMatrix($this->version, $this->eccLevel, $this->maskPattern))
91+
return (new QRMatrix($this->version, $this->eccLevel))
9292
->initFunctionalPatterns()
9393
->writeCodewords($this->rawBytes)
94-
->mask()
94+
->setFormatInfo($this->maskPattern)
95+
->mask($this->maskPattern)
9596
;
9697
}
9798

tests/Data/QRMatrixTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ final class QRMatrixTest extends TestCase{
3232
protected function setUp():void{
3333
$this->matrix = new QRMatrix(
3434
new Version($this::version),
35-
new EccLevel(EccLevel::L),
36-
new MaskPattern(MaskPattern::PATTERN_000)
35+
new EccLevel(EccLevel::L)
3736
);
3837
}
3938

@@ -175,11 +174,10 @@ public function testGetSetCheck():void{
175174
* Version data provider for several pattern tests
176175
*/
177176
public static function matrixProvider():Generator{
178-
$ecc = new EccLevel(EccLevel::L);
179-
$mask = new MaskPattern(MaskPattern::PATTERN_000);
177+
$ecc = new EccLevel(EccLevel::L);
180178

181179
foreach(range(1, 40) as $i){
182-
yield 'version: '.$i => [new QRMatrix(new Version($i), $ecc, $mask)];
180+
yield 'version: '.$i => [new QRMatrix(new Version($i), $ecc)];
183181
}
184182
}
185183

@@ -315,7 +313,7 @@ public function testSetVersionNumber(QRMatrix $matrix):void{
315313
* @dataProvider matrixProvider
316314
*/
317315
public function testSetFormatInfo(QRMatrix $matrix):void{
318-
$matrix->setFormatInfo();
316+
$matrix->setFormatInfo(new MaskPattern(MaskPattern::PATTERN_000));
319317

320318
$this->dm($matrix);
321319

tests/Output/QROutputTestAbstract.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ protected function setUp():void{
4343

4444
$this->options = new QROptions;
4545
$this->options->outputType = $this->type;
46-
47-
$this->matrix = (new QRData($this->options, [new Byte('testdata')]))
48-
->writeMatrix(new MaskPattern(MaskPattern::PATTERN_010));
46+
$this->matrix = (new QRCode($this->options))->addByteSegment('testdata')->getQRMatrix();
4947
$this->outputInterface = new $this->FQN($this->options, $this->matrix);
5048
}
5149

0 commit comments

Comments
 (0)