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
QrCode.php
142 lines
| 1 | <?php |
| 2 | declare(strict_types = 1); |
| 3 | |
| 4 | namespace BaconQrCode\Encoder; |
| 5 | |
| 6 | use BaconQrCode\Common\ErrorCorrectionLevel; |
| 7 | use BaconQrCode\Common\Mode; |
| 8 | use BaconQrCode\Common\Version; |
| 9 | |
| 10 | /** |
| 11 | * QR code. |
| 12 | */ |
| 13 | final class QrCode |
| 14 | { |
| 15 | /** |
| 16 | * Number of possible mask patterns. |
| 17 | */ |
| 18 | public const NUM_MASK_PATTERNS = 8; |
| 19 | |
| 20 | /** |
| 21 | * Mode of the QR code. |
| 22 | * |
| 23 | * @var Mode |
| 24 | */ |
| 25 | private $mode; |
| 26 | |
| 27 | /** |
| 28 | * EC level of the QR code. |
| 29 | * |
| 30 | * @var ErrorCorrectionLevel |
| 31 | */ |
| 32 | private $errorCorrectionLevel; |
| 33 | |
| 34 | /** |
| 35 | * Version of the QR code. |
| 36 | * |
| 37 | * @var Version |
| 38 | */ |
| 39 | private $version; |
| 40 | |
| 41 | /** |
| 42 | * Mask pattern of the QR code. |
| 43 | * |
| 44 | * @var int |
| 45 | */ |
| 46 | private $maskPattern = -1; |
| 47 | |
| 48 | /** |
| 49 | * Matrix of the QR code. |
| 50 | * |
| 51 | * @var ByteMatrix |
| 52 | */ |
| 53 | private $matrix; |
| 54 | |
| 55 | public function __construct( |
| 56 | Mode $mode, |
| 57 | ErrorCorrectionLevel $errorCorrectionLevel, |
| 58 | Version $version, |
| 59 | int $maskPattern, |
| 60 | ByteMatrix $matrix |
| 61 | ) { |
| 62 | $this->mode = $mode; |
| 63 | $this->errorCorrectionLevel = $errorCorrectionLevel; |
| 64 | $this->version = $version; |
| 65 | $this->maskPattern = $maskPattern; |
| 66 | $this->matrix = $matrix; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Gets the mode. |
| 71 | */ |
| 72 | public function getMode() : Mode |
| 73 | { |
| 74 | return $this->mode; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Gets the EC level. |
| 79 | */ |
| 80 | public function getErrorCorrectionLevel() : ErrorCorrectionLevel |
| 81 | { |
| 82 | return $this->errorCorrectionLevel; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Gets the version. |
| 87 | */ |
| 88 | public function getVersion() : Version |
| 89 | { |
| 90 | return $this->version; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Gets the mask pattern. |
| 95 | */ |
| 96 | public function getMaskPattern() : int |
| 97 | { |
| 98 | return $this->maskPattern; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Gets the matrix. |
| 103 | * |
| 104 | * @return ByteMatrix |
| 105 | */ |
| 106 | public function getMatrix() |
| 107 | { |
| 108 | return $this->matrix; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Validates whether a mask pattern is valid. |
| 113 | */ |
| 114 | public static function isValidMaskPattern(int $maskPattern) : bool |
| 115 | { |
| 116 | return $maskPattern > 0 && $maskPattern < self::NUM_MASK_PATTERNS; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Returns a string representation of the QR code. |
| 121 | */ |
| 122 | public function __toString() : string |
| 123 | { |
| 124 | $result = "<<\n" |
| 125 | . ' mode: ' . $this->mode . "\n" |
| 126 | . ' ecLevel: ' . $this->errorCorrectionLevel . "\n" |
| 127 | . ' version: ' . $this->version . "\n" |
| 128 | . ' maskPattern: ' . $this->maskPattern . "\n"; |
| 129 | |
| 130 | if ($this->matrix === null) { |
| 131 | $result .= " matrix: null\n"; |
| 132 | } else { |
| 133 | $result .= " matrix:\n"; |
| 134 | $result .= $this->matrix; |
| 135 | } |
| 136 | |
| 137 | $result .= ">>\n"; |
| 138 | |
| 139 | return $result; |
| 140 | } |
| 141 | } |
| 142 |