瀏覽代碼

:octocat: move format info & masking

smiley 2 年之前
父節點
當前提交
6b9f6f8459
共有 5 個文件被更改,包括 28 次插入18 次删除
  1. 4 3
      src/Common/MaskPattern.php
  2. 2 4
      src/Data/QRData.php
  3. 13 5
      src/QRCode.php
  4. 5 3
      tests/Data/DataInterfaceTestAbstract.php
  5. 4 3
      tests/Data/QRDataTest.php

+ 4 - 3
src/Common/MaskPattern.php

@@ -11,7 +11,7 @@
 
 
 namespace chillerlan\QRCode\Common;
 namespace chillerlan\QRCode\Common;
 
 
-use chillerlan\QRCode\Data\QRData;
+use chillerlan\QRCode\Data\QRMatrix;
 use chillerlan\QRCode\QRCodeException;
 use chillerlan\QRCode\QRCodeException;
 use Closure;
 use Closure;
 use function abs, array_search, count, min;
 use function abs, array_search, count, min;
@@ -112,11 +112,12 @@ final class MaskPattern{
 	/**
 	/**
 	 * Evaluates the matrix of the given data interface and returns a new mask pattern instance for the best result
 	 * Evaluates the matrix of the given data interface and returns a new mask pattern instance for the best result
 	 */
 	 */
-	public static function getBestPattern(QRData $dataInterface):self{
+	public static function getBestPattern(QRMatrix $QRMatrix):self{
 		$penalties = [];
 		$penalties = [];
 
 
 		foreach(self::PATTERNS as $pattern){
 		foreach(self::PATTERNS as $pattern){
-			$matrix  = $dataInterface->writeMatrix(new self($pattern))->getMatrix(true);
+			$mp      = new self($pattern);
+			$matrix  = (clone $QRMatrix)->setFormatInfo($mp)->mask($mp)->getMatrix(true);
 			$penalty = 0;
 			$penalty = 0;
 
 
 			for($level = 1; $level <= 4; $level++){
 			for($level = 1; $level <= 4; $level++){

+ 2 - 4
src/Data/QRData.php

@@ -10,7 +10,7 @@
 
 
 namespace chillerlan\QRCode\Data;
 namespace chillerlan\QRCode\Data;
 
 
-use chillerlan\QRCode\Common\{BitBuffer, EccLevel, MaskPattern, Mode, Version};
+use chillerlan\QRCode\Common\{BitBuffer, EccLevel, Mode, Version};
 use chillerlan\Settings\SettingsContainerInterface;
 use chillerlan\Settings\SettingsContainerInterface;
 
 
 use function count;
 use function count;
@@ -122,12 +122,10 @@ final class QRData{
 	/**
 	/**
 	 * returns a fresh matrix object with the data written and masked with the given $maskPattern
 	 * returns a fresh matrix object with the data written and masked with the given $maskPattern
 	 */
 	 */
-	public function writeMatrix(MaskPattern $maskPattern):QRMatrix{
+	public function writeMatrix():QRMatrix{
 		return (new QRMatrix($this->version, $this->eccLevel))
 		return (new QRMatrix($this->version, $this->eccLevel))
 			->initFunctionalPatterns()
 			->initFunctionalPatterns()
 			->writeCodewords($this->bitBuffer)
 			->writeCodewords($this->bitBuffer)
-			->setFormatInfo($maskPattern)
-			->mask($maskPattern)
 		;
 		;
 	}
 	}
 
 

+ 13 - 5
src/QRCode.php

@@ -236,14 +236,22 @@ class QRCode{
 	 * @throws \chillerlan\QRCode\Data\QRCodeDataException
 	 * @throws \chillerlan\QRCode\Data\QRCodeDataException
 	 */
 	 */
 	public function getQRMatrix():QRMatrix{
 	public function getQRMatrix():QRMatrix{
-		$dataInterface = new QRData($this->options, $this->dataSegments);
-		$maskPattern   = $this->options->maskPattern === MaskPattern::AUTO
-			? MaskPattern::getBestPattern($dataInterface)
+		$matrix = (new QRData($this->options, $this->dataSegments))->writeMatrix();
+
+		$maskPattern = $this->options->maskPattern === MaskPattern::AUTO
+			? MaskPattern::getBestPattern($matrix)
 			: new MaskPattern($this->options->maskPattern);
 			: new MaskPattern($this->options->maskPattern);
 
 
-		$matrix = $dataInterface->writeMatrix($maskPattern);
+		$matrix->setFormatInfo($maskPattern)->mask($maskPattern);
+
+		return $this->addMatrixModifications($matrix);
+	}
+
+	/**
+	 * add matrix modifications after mask pattern evaluation and before handing over to output
+	 */
+	protected function addMatrixModifications(QRMatrix $matrix):QRMatrix{
 
 
-		// add matrix modifications after mask pattern evaluation and before handing over to output
 		if($this->options->addLogoSpace){
 		if($this->options->addLogoSpace){
 			$logoSpaceWidth  = $this->options->logoSpaceWidth;
 			$logoSpaceWidth  = $this->options->logoSpaceWidth;
 			$logoSpaceHeight = $this->options->logoSpaceHeight;
 			$logoSpaceHeight = $this->options->logoSpaceHeight;

+ 5 - 3
tests/Data/DataInterfaceTestAbstract.php

@@ -67,13 +67,15 @@ abstract class DataInterfaceTestAbstract extends TestCase{
 	 *
 	 *
 	 * @dataProvider maskPatternProvider
 	 * @dataProvider maskPatternProvider
 	 */
 	 */
-	public function testInitMatrix(int $maskPattern):void{
+	public function testInitMatrix(int $pattern):void{
+		$maskPattern = new MaskPattern($pattern);
+
 		$this->QRData->setData([new static::$FQN(static::$testdata)]);
 		$this->QRData->setData([new static::$FQN(static::$testdata)]);
 
 
-		$matrix = $this->QRData->writeMatrix(new MaskPattern($maskPattern));
+		$matrix = $this->QRData->writeMatrix()->setFormatInfo($maskPattern)->mask($maskPattern);
 
 
 		$this::assertInstanceOf(QRMatrix::class, $matrix);
 		$this::assertInstanceOf(QRMatrix::class, $matrix);
-		$this::assertSame($maskPattern, $matrix->getMaskPattern()->getPattern());
+		$this::assertSame($pattern, $matrix->getMaskPattern()->getPattern());
 	}
 	}
 
 
 	abstract public static function stringValidateProvider():array;
 	abstract public static function stringValidateProvider():array;

+ 4 - 3
tests/Data/QRDataTest.php

@@ -42,9 +42,10 @@ final class QRDataTest extends TestCase{
 
 
 		$options     = new QROptions(['version' => 3]);
 		$options     = new QROptions(['version' => 3]);
 		$bitBuffer   = new BitBuffer($rawBytes);
 		$bitBuffer   = new BitBuffer($rawBytes);
-		$QRData      = (new QRData($options))->setBitBuffer($bitBuffer);
-		$maskPattern = MaskPattern::getBestPattern($QRData);
-		$matrix      = $QRData->writeMatrix($maskPattern);
+		$matrix      = (new QRData($options))->setBitBuffer($bitBuffer)->writeMatrix();
+		$maskPattern = MaskPattern::getBestPattern($matrix);
+
+		$matrix->setFormatInfo($maskPattern)->mask($maskPattern);
 
 
 		$this::assertSame(3, $matrix->getVersion()->getVersionNumber());
 		$this::assertSame(3, $matrix->getVersion()->getVersionNumber());