Skip to content

Commit 71509a5

Browse files
committed
:octocat:
1 parent 0ac2d49 commit 71509a5

File tree

3 files changed

+52
-7
lines changed

3 files changed

+52
-7
lines changed

src/Data/QRMatrix.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
namespace chillerlan\QRCode\Data;
1212

1313
use chillerlan\QRCode\Common\{BitBuffer, EccLevel, MaskPattern, ReedSolomonEncoder, Version};
14-
use function array_fill, count, floor;
14+
use function array_fill, array_map, array_reverse, count, floor;
1515

1616
/**
1717
* Holds an array representation of the final QR Code that contains numerical values for later output modifications;
@@ -569,6 +569,16 @@ public function setQuietZone(int $quietZoneSize):self{
569569
return $this;
570570
}
571571

572+
/**
573+
* Rotates the matrix by 90 degrees clock wise
574+
*/
575+
public function rotate90():self{
576+
/** @phan-suppress-next-line PhanParamTooFewInternalUnpack */
577+
$this->matrix = array_map((fn(int ...$a):array => array_reverse($a)), ...$this->matrix);
578+
579+
return $this;
580+
}
581+
572582
/**
573583
* Clears a space of $width * $height in order to add a logo or text.
574584
* If no $height is given, the space will be assumed a square of $width.

src/Decoder/BitMatrix.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use chillerlan\QRCode\Common\{EccLevel, MaskPattern, Version};
1515
use chillerlan\QRCode\Data\{QRCodeDataException, QRMatrix};
16-
use function array_fill, array_map, array_reverse, count;
16+
use function array_fill, array_reverse, count;
1717
use const PHP_INT_MAX, PHP_INT_SIZE;
1818

1919
/**
@@ -95,12 +95,10 @@ public function mirrorDiagonal():self{
9595
$this->mirror = !$this->mirror;
9696

9797
// mirror vertically
98-
$matrix = array_reverse($this->matrix);
98+
$this->matrix = array_reverse($this->matrix);
9999
// rotate by 90 degrees clockwise
100-
/** @phan-suppress-next-line PhanParamTooFewInternalUnpack */
101-
$this->matrix = array_map(fn(...$a):array => array_reverse($a), ...$matrix);
102-
103-
return $this;
100+
/** @phan-suppress-next-line PhanTypeMismatchReturnSuperType */
101+
return $this->rotate90();
104102
}
105103

106104
/**

tests/Data/QRMatrixTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,43 @@ public function testSetQuietZoneException():void{
367367
$this->matrix->setQuietZone(42);
368368
}
369369

370+
/**
371+
* Tests rotating the matrix by 90 degrees CW
372+
*
373+
* @dataProvider matrixProvider
374+
*/
375+
public function testRotate90(QRMatrix $matrix):void{
376+
$matrix->initFunctionalPatterns();
377+
378+
// matrix size
379+
$size = $matrix->getSize();
380+
// quiet zone size
381+
$qz = (($size - $matrix->getVersion()->getDimension()) / 2);
382+
383+
// initial dark module position
384+
$this::assertSame(QRMatrix::M_DARKMODULE, $matrix->get((8 + $qz), ($size - 8 - $qz)));
385+
386+
// first rotation
387+
$matrix->rotate90();
388+
$this->dm($matrix);
389+
$this::assertSame(QRMatrix::M_DARKMODULE, $matrix->get((7 + $qz), (8 + $qz)));
390+
391+
// second rotation
392+
$matrix->rotate90();
393+
$this->dm($matrix);
394+
$this::assertSame(QRMatrix::M_DARKMODULE, $matrix->get(($size - 9 - $qz), (7 + $qz)));
395+
396+
// third rotation
397+
$matrix->rotate90();
398+
$this->dm($matrix);
399+
$this::assertSame(QRMatrix::M_DARKMODULE, $matrix->get(($size - 8 - $qz), ($size - 9 - $qz)));
400+
401+
// fourth rotation
402+
$matrix->rotate90();
403+
$this->dm($matrix);
404+
$this::assertSame(QRMatrix::M_DARKMODULE, $matrix->get((8 + $qz), ($size - 8 - $qz)));
405+
}
406+
370407
/**
371408
* Tests if the logo space is drawn square if one of the dimensions is omitted
372409
*/

0 commit comments

Comments
 (0)