smiley %!s(int64=2) %!d(string=hai) anos
pai
achega
71509a5a94
Modificáronse 3 ficheiros con 52 adicións e 7 borrados
  1. 11 1
      src/Data/QRMatrix.php
  2. 4 6
      src/Decoder/BitMatrix.php
  3. 37 0
      tests/Data/QRMatrixTest.php

+ 11 - 1
src/Data/QRMatrix.php

@@ -11,7 +11,7 @@
 namespace chillerlan\QRCode\Data;
 
 use chillerlan\QRCode\Common\{BitBuffer, EccLevel, MaskPattern, ReedSolomonEncoder, Version};
-use function array_fill, count, floor;
+use function array_fill, array_map, array_reverse, count, floor;
 
 /**
  * Holds an array representation of the final QR Code that contains numerical values for later output modifications;
@@ -569,6 +569,16 @@ class QRMatrix{
 		return $this;
 	}
 
+	/**
+	 * Rotates the matrix by 90 degrees clock wise
+	 */
+	public function rotate90():self{
+		/** @phan-suppress-next-line PhanParamTooFewInternalUnpack */
+		$this->matrix = array_map((fn(int ...$a):array => array_reverse($a)), ...$this->matrix);
+
+		return $this;
+	}
+
 	/**
 	 * Clears a space of $width * $height in order to add a logo or text.
 	 * If no $height is given, the space will be assumed a square of $width.

+ 4 - 6
src/Decoder/BitMatrix.php

@@ -13,7 +13,7 @@ namespace chillerlan\QRCode\Decoder;
 
 use chillerlan\QRCode\Common\{EccLevel, MaskPattern, Version};
 use chillerlan\QRCode\Data\{QRCodeDataException, QRMatrix};
-use function array_fill, array_map, array_reverse, count;
+use function array_fill, array_reverse, count;
 use const PHP_INT_MAX, PHP_INT_SIZE;
 
 /**
@@ -95,12 +95,10 @@ final class BitMatrix extends QRMatrix{
 		$this->mirror = !$this->mirror;
 
 		// mirror vertically
-		$matrix = array_reverse($this->matrix);
+		$this->matrix = array_reverse($this->matrix);
 		// rotate by 90 degrees clockwise
-		/** @phan-suppress-next-line PhanParamTooFewInternalUnpack */
-		$this->matrix = array_map(fn(...$a):array => array_reverse($a), ...$matrix);
-
-		return $this;
+		/** @phan-suppress-next-line PhanTypeMismatchReturnSuperType */
+		return $this->rotate90();
 	}
 
 	/**

+ 37 - 0
tests/Data/QRMatrixTest.php

@@ -367,6 +367,43 @@ final class QRMatrixTest extends TestCase{
 		$this->matrix->setQuietZone(42);
 	}
 
+	/**
+	 * Tests rotating the matrix by 90 degrees CW
+	 *
+	 * @dataProvider matrixProvider
+	 */
+	public function testRotate90(QRMatrix $matrix):void{
+		$matrix->initFunctionalPatterns();
+
+		// matrix size
+		$size = $matrix->getSize();
+		// quiet zone size
+		$qz   = (($size - $matrix->getVersion()->getDimension()) / 2);
+
+		// initial dark module position
+		$this::assertSame(QRMatrix::M_DARKMODULE, $matrix->get((8 + $qz), ($size - 8 - $qz)));
+
+		// first rotation
+		$matrix->rotate90();
+		$this->dm($matrix);
+		$this::assertSame(QRMatrix::M_DARKMODULE, $matrix->get((7 + $qz), (8 + $qz)));
+
+		// second rotation
+		$matrix->rotate90();
+		$this->dm($matrix);
+		$this::assertSame(QRMatrix::M_DARKMODULE, $matrix->get(($size - 9 - $qz), (7 + $qz)));
+
+		// third rotation
+		$matrix->rotate90();
+		$this->dm($matrix);
+		$this::assertSame(QRMatrix::M_DARKMODULE, $matrix->get(($size - 8 - $qz), ($size - 9 - $qz)));
+
+		// fourth rotation
+		$matrix->rotate90();
+		$this->dm($matrix);
+		$this::assertSame(QRMatrix::M_DARKMODULE, $matrix->get((8 + $qz), ($size - 8 - $qz)));
+	}
+
 	/**
 	 * Tests if the logo space is drawn square if one of the dimensions is omitted
 	 */