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