Skip to content

Commit 85776d2

Browse files
committed
:octocat: re-introduce QRMatrix::flip() (6c66d56) as bitmask rework (#205) won't happen any time soon
1 parent 81ef065 commit 85776d2

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/Data/QRMatrix.php

+14-2
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,18 @@ public function setArea(int $startX, int $startY, int $width, int $height, bool
314314
return $this;
315315
}
316316

317+
/**
318+
* Flips the value of the module at ($x, $y)
319+
*/
320+
public function flip(int $x, int $y):self{
321+
322+
if(isset($this->matrix[$y][$x])){
323+
$this->matrix[$y][$x] ^= $this::IS_DARK;
324+
}
325+
326+
return $this;
327+
}
328+
317329
/**
318330
* Checks whether the module at ($x, $y) is of the given $M_TYPE
319331
*
@@ -617,7 +629,7 @@ public function invert():self{
617629
continue;
618630
}
619631

620-
$this->set($x, $y, ($val & $this::IS_DARK) !== $this::IS_DARK, $val);
632+
$this->flip($x, $y);
621633
}
622634
}
623635

@@ -774,7 +786,7 @@ public function mask(MaskPattern $maskPattern):self{
774786
}
775787

776788
if($mask($x, $y)){
777-
$this->set($x, $y, ($val & $this::IS_DARK) !== $this::IS_DARK, $val);
789+
$this->flip($x, $y);
778790
}
779791
}
780792
}

tests/Data/QRMatrixTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,26 @@ public function testSetLogoSpaceMaxSizeException():void{
518518
(new QRCode($o))->addByteSegment('testdata')->getQRMatrix()->setLogoSpace(37, 37);
519519
}
520520

521+
/**
522+
* Tests flipping the value of a module
523+
*/
524+
public function testFlip():void{
525+
$this->matrix->set(20, 20, true, QRMatrix::M_TEST);
526+
527+
// cover checkType()
528+
$this::assertTrue($this->matrix->checkType(20, 20, QRMatrix::M_TEST));
529+
// verify the current state (dark)
530+
$this::assertSame(QRMatrix::M_TEST_DARK, $this->matrix->get(20, 20));
531+
// flip
532+
$this->matrix->flip(20, 20);
533+
// verify flip
534+
$this::assertSame(QRMatrix::M_TEST, $this->matrix->get(20, 20));
535+
// flip again
536+
$this->matrix->flip(20, 20);
537+
// verify flip
538+
$this::assertSame(QRMatrix::M_TEST_DARK, $this->matrix->get(20, 20));
539+
}
540+
521541
/**
522542
* Tests checking whether the M_TYPE of a module is not one of an array of M_TYPES
523543
*/

0 commit comments

Comments
 (0)