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