Skip to content

Commit

Permalink
:octocat: re-introduce QRMatrix::flip() (6c66d56) as bitmask rework (#205) wo…
Browse files Browse the repository at this point in the history
…n't happen any time soon
  • Loading branch information
codemasher committed Sep 8, 2023
1 parent 81ef065 commit 85776d2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Data/QRMatrix.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,18 @@ public function setArea(int $startX, int $startY, int $width, int $height, bool
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
*
Expand Down Expand Up @@ -617,7 +629,7 @@ public function invert():self{
continue;
}

$this->set($x, $y, ($val & $this::IS_DARK) !== $this::IS_DARK, $val);
$this->flip($x, $y);
}
}

Expand Down Expand Up @@ -774,7 +786,7 @@ public function mask(MaskPattern $maskPattern):self{
}

if($mask($x, $y)){
$this->set($x, $y, ($val & $this::IS_DARK) !== $this::IS_DARK, $val);
$this->flip($x, $y);
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions tests/Data/QRMatrixTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,26 @@ public function testSetLogoSpaceMaxSizeException():void{
(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
*/
Expand Down

0 comments on commit 85776d2

Please sign in to comment.