Ver Fonte

:octocat: move mask pattern parameter

smiley há 2 anos atrás
pai
commit
50179a97ef

+ 3 - 2
src/Data/QRData.php

@@ -123,10 +123,11 @@ final class QRData{
 	 * returns a fresh matrix object with the data written and masked with the given $maskPattern
 	 */
 	public function writeMatrix(MaskPattern $maskPattern):QRMatrix{
-		return (new QRMatrix($this->version, $this->eccLevel, $maskPattern))
+		return (new QRMatrix($this->version, $this->eccLevel))
 			->initFunctionalPatterns()
 			->writeCodewords($this->bitBuffer)
-			->mask()
+			->setFormatInfo($maskPattern)
+			->mask($maskPattern)
 		;
 	}
 

+ 20 - 12
src/Data/QRMatrix.php

@@ -83,19 +83,23 @@ class QRMatrix{
 	];
 
 	/**
-	 * the used mask pattern, set via QRMatrix::mask()
+	 * the matrix version - always set in QRMatrix, may be null in BitMatrix
 	 */
-	protected ?MaskPattern $maskPattern = null;
+	protected ?Version $version = null;
 
 	/**
-	 * the current ECC level
+	 * the current ECC level - always set in QRMatrix, may be null in BitMatrix
 	 */
 	protected ?EccLevel $eccLevel = null;
 
 	/**
-	 * a Version instance
+	 * the mask pattern that was used in the most recent operation, set via:
+	 *
+	 * - QRMatrix::setFormatInfo()
+	 * - QRMatrix::mask()
+	 * - BitMatrix::readFormatInformation()
 	 */
-	protected ?Version $version = null;
+	protected ?MaskPattern $maskPattern = null;
 
 	/**
 	 * the size (side length) of the matrix, including quiet zone (if created)
@@ -112,10 +116,9 @@ class QRMatrix{
 	/**
 	 * QRMatrix constructor.
 	 */
-	public function __construct(Version $version, EccLevel $eccLevel, MaskPattern $maskPattern){
+	public function __construct(Version $version, EccLevel $eccLevel){
 		$this->version     = $version;
 		$this->eccLevel    = $eccLevel;
-		$this->maskPattern = $maskPattern;
 		$this->moduleCount = $this->version->getDimension();
 		$this->matrix      = $this->createMatrix($this->moduleCount, $this::M_NULL);
 	}
@@ -502,12 +505,16 @@ class QRMatrix{
 	}
 
 	/**
-	 * Draws the format info along the finder patterns
+	 * Draws the format info along the finder patterns. If no $maskPattern, all format info modules will be set to false.
 	 *
 	 * ISO/IEC 18004:2000 Section 8.9
 	 */
-	public function setFormatInfo():self{
-		$bits = $this->eccLevel->getformatPattern($this->maskPattern);
+	public function setFormatInfo(MaskPattern $maskPattern = null):self{
+		$this->maskPattern = $maskPattern;
+
+		$bits = ($this->maskPattern instanceof MaskPattern)
+			? $this->eccLevel->getformatPattern($this->maskPattern)
+			: 0; // sets all format fields to false (test mode)
 
 		for($i = 0; $i < 15; $i++){
 			$v = (($bits >> $i) & 1) === 1;
@@ -703,8 +710,9 @@ class QRMatrix{
 	 *
 	 * ISO/IEC 18004:2000 Section 8.8.1
 	 */
-	public function mask():self{
-		$mask = $this->maskPattern->getMask();
+	public function mask(MaskPattern $maskPattern):self{
+		$this->maskPattern = $maskPattern;
+		$mask              = $this->maskPattern->getMask();
 
 		foreach($this->matrix as $y => $row){
 			foreach($row as $x => $val){

+ 7 - 3
src/Decoder/BitMatrix.php

@@ -117,11 +117,15 @@ final class BitMatrix extends QRMatrix{
 		$this
 			->readFormatInformation()
 			->readVersion()
-			->mask() // reverse the mask pattern
+			->mask($this->maskPattern) // reverse the mask pattern
 		;
 
 		// invoke a fresh matrix with only the function & format patterns to compare against
-		$fp        = (new QRMatrix($this->version, $this->eccLevel, $this->maskPattern))->initFunctionalPatterns();
+		$matrix = (new QRMatrix($this->version, $this->eccLevel))
+			->initFunctionalPatterns()
+			->setFormatInfo($this->maskPattern)
+		;
+
 		$result    = [];
 		$byte      = 0;
 		$bitsRead  = 0;
@@ -143,7 +147,7 @@ final class BitMatrix extends QRMatrix{
 					$x = ($i - $col);
 
 					// Ignore bits covered by the function pattern
-					if($fp->get($x, $y) !== $this::M_NULL){
+					if($matrix->get($x, $y) !== $this::M_NULL){
 						continue;
 					}
 

+ 3 - 2
src/Decoder/DecoderResult.php

@@ -88,10 +88,11 @@ final class DecoderResult{
 	 * Returns a QRMatrix instance with thesettings and data of the reader result
 	 */
 	public function getQRMatrix():QRMatrix{
-		return (new QRMatrix($this->version, $this->eccLevel, $this->maskPattern))
+		return (new QRMatrix($this->version, $this->eccLevel))
 			->initFunctionalPatterns()
 			->writeCodewords($this->rawBytes)
-			->mask()
+			->setFormatInfo($this->maskPattern)
+			->mask($this->maskPattern)
 		;
 	}
 

+ 4 - 6
tests/Data/QRMatrixTest.php

@@ -32,8 +32,7 @@ final class QRMatrixTest extends TestCase{
 	protected function setUp():void{
 		$this->matrix = new QRMatrix(
 			new Version($this::version),
-			new EccLevel(EccLevel::L),
-			new MaskPattern(MaskPattern::PATTERN_000)
+			new EccLevel(EccLevel::L)
 		);
 	}
 
@@ -175,11 +174,10 @@ final class QRMatrixTest extends TestCase{
 	 * Version data provider for several pattern tests
 	 */
 	public static function matrixProvider():Generator{
-		$ecc  = new EccLevel(EccLevel::L);
-		$mask = new MaskPattern(MaskPattern::PATTERN_000);
+		$ecc = new EccLevel(EccLevel::L);
 
 		foreach(range(1, 40) as $i){
-			yield 'version: '.$i => [new QRMatrix(new Version($i), $ecc, $mask)];
+			yield 'version: '.$i => [new QRMatrix(new Version($i), $ecc)];
 		}
 	}
 
@@ -315,7 +313,7 @@ final class QRMatrixTest extends TestCase{
 	 * @dataProvider matrixProvider
 	 */
 	public function testSetFormatInfo(QRMatrix $matrix):void{
-		$matrix->setFormatInfo();
+		$matrix->setFormatInfo(new MaskPattern(MaskPattern::PATTERN_000));
 
 		$this->dm($matrix);
 

+ 1 - 3
tests/Output/QROutputTestAbstract.php

@@ -43,9 +43,7 @@ abstract class QROutputTestAbstract extends TestCase{
 
 		$this->options             = new QROptions;
 		$this->options->outputType = $this->type;
-
-		$this->matrix              = (new QRData($this->options, [new Byte('testdata')]))
-			->writeMatrix(new MaskPattern(MaskPattern::PATTERN_010));
+		$this->matrix              = (new QRCode($this->options))->addByteSegment('testdata')->getQRMatrix();
 		$this->outputInterface     = new $this->FQN($this->options, $this->matrix);
 	}