Debian Codegraph @

vendor/endroid/qr-code/src/Matrix/Matrix.php

raw
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Endroid\QrCode\Matrix;
6
7 use Endroid\QrCode\RoundBlockSizeMode;
8
9 final class Matrix implements MatrixInterface
10 {
11 private readonly float $blockSize;
12 private readonly int $innerSize;
13 private readonly int $outerSize;
14 private readonly int $marginLeft;
15 private readonly int $marginRight;
16
17 /** @param array<array<int>> $blockValues */
18 public function __construct(
19 private array $blockValues,
20 int $size,
21 int $margin,
22 RoundBlockSizeMode $roundBlockSizeMode
23 ) {
24 $blockSize = $size / $this->getBlockCount();
25 $innerSize = $size;
26 $outerSize = $size + 2 * $margin;
27
28 switch ($roundBlockSizeMode) {
29 case RoundBlockSizeMode::Enlarge:
30 $blockSize = intval(ceil($blockSize));
31 $innerSize = intval($blockSize * $this->getBlockCount());
32 $outerSize = $innerSize + 2 * $margin;
33 break;
34 case RoundBlockSizeMode::Shrink:
35 $blockSize = intval(floor($blockSize));
36 $innerSize = intval($blockSize * $this->getBlockCount());
37 $outerSize = $innerSize + 2 * $margin;
38 break;
39 case RoundBlockSizeMode::Margin:
40 $blockSize = intval(floor($blockSize));
41 $innerSize = intval($blockSize * $this->getBlockCount());
42 break;
43 }
44
45 if ($blockSize < 1) {
46 throw new \Exception('Too much data: increase image dimensions or lower error correction level');
47 }
48
49 $this->blockSize = $blockSize;
50 $this->innerSize = $innerSize;
51 $this->outerSize = $outerSize;
52 $this->marginLeft = intval(($this->outerSize - $this->innerSize) / 2);
53 $this->marginRight = $this->outerSize - $this->innerSize - $this->marginLeft;
54 }
55
56 public function getBlockValue(int $rowIndex, int $columnIndex): int
57 {
58 return $this->blockValues[$rowIndex][$columnIndex];
59 }
60
61 public function getBlockCount(): int
62 {
63 return count($this->blockValues[0]);
64 }
65
66 public function getBlockSize(): float
67 {
68 return $this->blockSize;
69 }
70
71 public function getInnerSize(): int
72 {
73 return $this->innerSize;
74 }
75
76 public function getOuterSize(): int
77 {
78 return $this->outerSize;
79 }
80
81 public function getMarginLeft(): int
82 {
83 return $this->marginLeft;
84 }
85
86 public function getMarginRight(): int
87 {
88 return $this->marginRight;
89 }
90 }