Skip to content

Commit 6b9f6f8

Browse files
committed
:octocat: move format info & masking
1 parent b0fc9ae commit 6b9f6f8

File tree

5 files changed

+28
-18
lines changed

5 files changed

+28
-18
lines changed

src/Common/MaskPattern.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace chillerlan\QRCode\Common;
1313

14-
use chillerlan\QRCode\Data\QRData;
14+
use chillerlan\QRCode\Data\QRMatrix;
1515
use chillerlan\QRCode\QRCodeException;
1616
use Closure;
1717
use function abs, array_search, count, min;
@@ -112,11 +112,12 @@ public function getMask():Closure{
112112
/**
113113
* Evaluates the matrix of the given data interface and returns a new mask pattern instance for the best result
114114
*/
115-
public static function getBestPattern(QRData $dataInterface):self{
115+
public static function getBestPattern(QRMatrix $QRMatrix):self{
116116
$penalties = [];
117117

118118
foreach(self::PATTERNS as $pattern){
119-
$matrix = $dataInterface->writeMatrix(new self($pattern))->getMatrix(true);
119+
$mp = new self($pattern);
120+
$matrix = (clone $QRMatrix)->setFormatInfo($mp)->mask($mp)->getMatrix(true);
120121
$penalty = 0;
121122

122123
for($level = 1; $level <= 4; $level++){

src/Data/QRData.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace chillerlan\QRCode\Data;
1212

13-
use chillerlan\QRCode\Common\{BitBuffer, EccLevel, MaskPattern, Mode, Version};
13+
use chillerlan\QRCode\Common\{BitBuffer, EccLevel, Mode, Version};
1414
use chillerlan\Settings\SettingsContainerInterface;
1515

1616
use function count;
@@ -122,12 +122,10 @@ public function setBitBuffer(BitBuffer $bitBuffer):self{
122122
/**
123123
* returns a fresh matrix object with the data written and masked with the given $maskPattern
124124
*/
125-
public function writeMatrix(MaskPattern $maskPattern):QRMatrix{
125+
public function writeMatrix():QRMatrix{
126126
return (new QRMatrix($this->version, $this->eccLevel))
127127
->initFunctionalPatterns()
128128
->writeCodewords($this->bitBuffer)
129-
->setFormatInfo($maskPattern)
130-
->mask($maskPattern)
131129
;
132130
}
133131

src/QRCode.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,14 +236,22 @@ public function renderMatrix(QRMatrix $matrix, string $file = null){
236236
* @throws \chillerlan\QRCode\Data\QRCodeDataException
237237
*/
238238
public function getQRMatrix():QRMatrix{
239-
$dataInterface = new QRData($this->options, $this->dataSegments);
240-
$maskPattern = $this->options->maskPattern === MaskPattern::AUTO
241-
? MaskPattern::getBestPattern($dataInterface)
239+
$matrix = (new QRData($this->options, $this->dataSegments))->writeMatrix();
240+
241+
$maskPattern = $this->options->maskPattern === MaskPattern::AUTO
242+
? MaskPattern::getBestPattern($matrix)
242243
: new MaskPattern($this->options->maskPattern);
243244

244-
$matrix = $dataInterface->writeMatrix($maskPattern);
245+
$matrix->setFormatInfo($maskPattern)->mask($maskPattern);
246+
247+
return $this->addMatrixModifications($matrix);
248+
}
249+
250+
/**
251+
* add matrix modifications after mask pattern evaluation and before handing over to output
252+
*/
253+
protected function addMatrixModifications(QRMatrix $matrix):QRMatrix{
245254

246-
// add matrix modifications after mask pattern evaluation and before handing over to output
247255
if($this->options->addLogoSpace){
248256
$logoSpaceWidth = $this->options->logoSpaceWidth;
249257
$logoSpaceHeight = $this->options->logoSpaceHeight;

tests/Data/DataInterfaceTestAbstract.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,15 @@ public static function maskPatternProvider():array{
6767
*
6868
* @dataProvider maskPatternProvider
6969
*/
70-
public function testInitMatrix(int $maskPattern):void{
70+
public function testInitMatrix(int $pattern):void{
71+
$maskPattern = new MaskPattern($pattern);
72+
7173
$this->QRData->setData([new static::$FQN(static::$testdata)]);
7274

73-
$matrix = $this->QRData->writeMatrix(new MaskPattern($maskPattern));
75+
$matrix = $this->QRData->writeMatrix()->setFormatInfo($maskPattern)->mask($maskPattern);
7476

7577
$this::assertInstanceOf(QRMatrix::class, $matrix);
76-
$this::assertSame($maskPattern, $matrix->getMaskPattern()->getPattern());
78+
$this::assertSame($pattern, $matrix->getMaskPattern()->getPattern());
7779
}
7880

7981
abstract public static function stringValidateProvider():array;

tests/Data/QRDataTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ public function testSetBitBuffer():void{
4242

4343
$options = new QROptions(['version' => 3]);
4444
$bitBuffer = new BitBuffer($rawBytes);
45-
$QRData = (new QRData($options))->setBitBuffer($bitBuffer);
46-
$maskPattern = MaskPattern::getBestPattern($QRData);
47-
$matrix = $QRData->writeMatrix($maskPattern);
45+
$matrix = (new QRData($options))->setBitBuffer($bitBuffer)->writeMatrix();
46+
$maskPattern = MaskPattern::getBestPattern($matrix);
47+
48+
$matrix->setFormatInfo($maskPattern)->mask($maskPattern);
4849

4950
$this::assertSame(3, $matrix->getVersion()->getVersionNumber());
5051

0 commit comments

Comments
 (0)