Просмотр исходного кода

:octocat: re-introduce QRMatrix::flip() (6c66d56a31b558d3474b259feba777bc4ae25f2b) as bitmask rework (#205) won't happen any time soon

smiley 2 лет назад
Родитель
Сommit
85776d2db9
2 измененных файлов с 34 добавлено и 2 удалено
  1. 14 2
      src/Data/QRMatrix.php
  2. 20 0
      tests/Data/QRMatrixTest.php

+ 14 - 2
src/Data/QRMatrix.php

@@ -314,6 +314,18 @@ class QRMatrix{
 		return $this;
 	}
 
+	/**
+	 * Flips the value of the module at ($x, $y)
+	 */
+	public function flip(int $x, int $y):self{
+
+		if(isset($this->matrix[$y][$x])){
+			$this->matrix[$y][$x] ^= $this::IS_DARK;
+		}
+
+		return $this;
+	}
+
 	/**
 	 * Checks whether the module at ($x, $y) is of the given $M_TYPE
 	 *
@@ -617,7 +629,7 @@ class QRMatrix{
 					continue;
 				}
 
-				$this->set($x, $y, ($val & $this::IS_DARK) !== $this::IS_DARK, $val);
+				$this->flip($x, $y);
 			}
 		}
 
@@ -774,7 +786,7 @@ class QRMatrix{
 				}
 
 				if($mask($x, $y)){
-					$this->set($x, $y, ($val & $this::IS_DARK) !== $this::IS_DARK, $val);
+					$this->flip($x, $y);
 				}
 			}
 		}

+ 20 - 0
tests/Data/QRMatrixTest.php

@@ -518,6 +518,26 @@ final class QRMatrixTest extends TestCase{
 		(new QRCode($o))->addByteSegment('testdata')->getQRMatrix()->setLogoSpace(37, 37);
 	}
 
+	/**
+	 * Tests flipping the value of a module
+	 */
+	public function testFlip():void{
+		$this->matrix->set(20, 20, true, QRMatrix::M_TEST);
+
+		// cover checkType()
+		$this::assertTrue($this->matrix->checkType(20, 20, QRMatrix::M_TEST));
+		// verify the current state (dark)
+		$this::assertSame(QRMatrix::M_TEST_DARK, $this->matrix->get(20, 20));
+		// flip
+		$this->matrix->flip(20, 20);
+		// verify flip
+		$this::assertSame(QRMatrix::M_TEST, $this->matrix->get(20, 20));
+		// flip again
+		$this->matrix->flip(20, 20);
+		// verify flip
+		$this::assertSame(QRMatrix::M_TEST_DARK, $this->matrix->get(20, 20));
+	}
+
 	/**
 	 * Tests checking whether the M_TYPE of a module is not one of an array of M_TYPES
 	 */