Debian Codegraph @

src/Encoder/QrCode.php

raw
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 * Mask pattern of the QR code.
22 */
23 private int $maskPattern = -1;
24
25 /**
26 * Matrix of the QR code.
27 */
28 private ByteMatrix $matrix;
29
30 public function __construct(
31 private readonly Mode $mode,
32 private readonly ErrorCorrectionLevel $errorCorrectionLevel,
33 private readonly Version $version,
34 int $maskPattern,
35 ByteMatrix $matrix
36 ) {
37 $this->maskPattern = $maskPattern;
38 $this->matrix = $matrix;
39 }
40
41 /**
42 * Gets the mode.
43 */
44 public function getMode() : Mode
45 {
46 return $this->mode;
47 }
48
49 /**
50 * Gets the EC level.
51 */
52 public function getErrorCorrectionLevel() : ErrorCorrectionLevel
53 {
54 return $this->errorCorrectionLevel;
55 }
56
57 /**
58 * Gets the version.
59 */
60 public function getVersion() : Version
61 {
62 return $this->version;
63 }
64
65 /**
66 * Gets the mask pattern.
67 */
68 public function getMaskPattern() : int
69 {
70 return $this->maskPattern;
71 }
72
73 public function getMatrix(): ByteMatrix
74 {
75 return $this->matrix;
76 }
77
78 /**
79 * Validates whether a mask pattern is valid.
80 */
81 public static function isValidMaskPattern(int $maskPattern) : bool
82 {
83 return $maskPattern > 0 && $maskPattern < self::NUM_MASK_PATTERNS;
84 }
85
86 /**
87 * Returns a string representation of the QR code.
88 */
89 public function __toString() : string
90 {
91 $result = "<<\n"
92 . ' mode: ' . $this->mode . "\n"
93 . ' ecLevel: ' . $this->errorCorrectionLevel . "\n"
94 . ' version: ' . $this->version . "\n"
95 . ' maskPattern: ' . $this->maskPattern . "\n";
96
97 if ($this->matrix === null) {
98 $result .= " matrix: null\n";
99 } else {
100 $result .= " matrix:\n";
101 $result .= $this->matrix;
102 }
103
104 $result .= ">>\n";
105
106 return $result;
107 }
108 }