BlockPair.php
11 months ago
ByteMatrix.php
11 months ago
Encoder.php
11 months ago
MaskUtil.php
11 months ago
MatrixUtil.php
11 months ago
QrCode.php
11 months ago
ByteMatrix.php
131 lines
| 1 | <?php |
| 2 | |
| 3 | declare (strict_types=1); |
| 4 | namespace WP2FA_Vendor\BaconQrCode\Encoder; |
| 5 | |
| 6 | use SplFixedArray; |
| 7 | use Traversable; |
| 8 | /** |
| 9 | * Byte matrix. |
| 10 | */ |
| 11 | final class ByteMatrix |
| 12 | { |
| 13 | /** |
| 14 | * Bytes in the matrix, represented as array. |
| 15 | * |
| 16 | * @var SplFixedArray<SplFixedArray<int>> |
| 17 | */ |
| 18 | private $bytes; |
| 19 | /** |
| 20 | * Width of the matrix. |
| 21 | * |
| 22 | * @var int |
| 23 | */ |
| 24 | private $width; |
| 25 | /** |
| 26 | * Height of the matrix. |
| 27 | * |
| 28 | * @var int |
| 29 | */ |
| 30 | private $height; |
| 31 | public function __construct(int $width, int $height) |
| 32 | { |
| 33 | $this->height = $height; |
| 34 | $this->width = $width; |
| 35 | $this->bytes = new SplFixedArray($height); |
| 36 | for ($y = 0; $y < $height; ++$y) { |
| 37 | $this->bytes[$y] = SplFixedArray::fromArray(\array_fill(0, $width, 0)); |
| 38 | } |
| 39 | } |
| 40 | /** |
| 41 | * Gets the width of the matrix. |
| 42 | */ |
| 43 | public function getWidth() : int |
| 44 | { |
| 45 | return $this->width; |
| 46 | } |
| 47 | /** |
| 48 | * Gets the height of the matrix. |
| 49 | */ |
| 50 | public function getHeight() : int |
| 51 | { |
| 52 | return $this->height; |
| 53 | } |
| 54 | /** |
| 55 | * Gets the internal representation of the matrix. |
| 56 | * |
| 57 | * @return SplFixedArray<SplFixedArray<int>> |
| 58 | */ |
| 59 | public function getArray() : SplFixedArray |
| 60 | { |
| 61 | return $this->bytes; |
| 62 | } |
| 63 | /** |
| 64 | * @return Traversable<int> |
| 65 | */ |
| 66 | public function getBytes() : Traversable |
| 67 | { |
| 68 | foreach ($this->bytes as $row) { |
| 69 | foreach ($row as $byte) { |
| 70 | (yield $byte); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | /** |
| 75 | * Gets the byte for a specific position. |
| 76 | */ |
| 77 | public function get(int $x, int $y) : int |
| 78 | { |
| 79 | return $this->bytes[$y][$x]; |
| 80 | } |
| 81 | /** |
| 82 | * Sets the byte for a specific position. |
| 83 | */ |
| 84 | public function set(int $x, int $y, int $value) : void |
| 85 | { |
| 86 | $this->bytes[$y][$x] = $value; |
| 87 | } |
| 88 | /** |
| 89 | * Clears the matrix with a specific value. |
| 90 | */ |
| 91 | public function clear(int $value) : void |
| 92 | { |
| 93 | for ($y = 0; $y < $this->height; ++$y) { |
| 94 | for ($x = 0; $x < $this->width; ++$x) { |
| 95 | $this->bytes[$y][$x] = $value; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | public function __clone() |
| 100 | { |
| 101 | $this->bytes = clone $this->bytes; |
| 102 | foreach ($this->bytes as $index => $row) { |
| 103 | $this->bytes[$index] = clone $row; |
| 104 | } |
| 105 | } |
| 106 | /** |
| 107 | * Returns a string representation of the matrix. |
| 108 | */ |
| 109 | public function __toString() : string |
| 110 | { |
| 111 | $result = ''; |
| 112 | for ($y = 0; $y < $this->height; $y++) { |
| 113 | for ($x = 0; $x < $this->width; $x++) { |
| 114 | switch ($this->bytes[$y][$x]) { |
| 115 | case 0: |
| 116 | $result .= ' 0'; |
| 117 | break; |
| 118 | case 1: |
| 119 | $result .= ' 1'; |
| 120 | break; |
| 121 | default: |
| 122 | $result .= ' '; |
| 123 | break; |
| 124 | } |
| 125 | } |
| 126 | $result .= "\n"; |
| 127 | } |
| 128 | return $result; |
| 129 | } |
| 130 | } |
| 131 |