1<?php
2/**
3 * BaconQrCode
4 *
5 * @link      http://github.com/Bacon/BaconQrCode For the canonical source repository
6 * @copyright 2013 Ben 'DASPRiD' Scholzen
7 * @license   http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
8 */
9
10namespace BaconQrCode\Encoder;
11
12use BaconQrCode\Common\ErrorCorrectionLevel;
13use BaconQrCode\Common\Mode;
14use BaconQrCode\Common\Version;
15
16/**
17 * QR code.
18 */
19class QrCode
20{
21    /**
22     * Number of possible mask patterns.
23     */
24    const NUM_MASK_PATTERNS = 8;
25
26    /**
27     * Mode of the QR code.
28     *
29     * @var Mode
30     */
31    protected $mode;
32
33    /**
34     * EC level of the QR code.
35     *
36     * @var ErrorCorrectionLevel
37     */
38    protected $errorCorrectionLevel;
39
40    /**
41     * Version of the QR code.
42     *
43     * @var Version
44     */
45    protected $version;
46
47    /**
48     * Mask pattern of the QR code.
49     *
50     * @var integer
51     */
52    protected $maskPattern = -1;
53
54    /**
55     * Matrix of the QR code.
56     *
57     * @var ByteMatrix
58     */
59    protected $matrix;
60
61    /**
62     * Gets the mode.
63     *
64     * @return Mode
65     */
66    public function getMode()
67    {
68        return $this->mode;
69    }
70
71    /**
72     * Sets the mode.
73     *
74     * @param  Mode $mode
75     * @return void
76     */
77    public function setMode(Mode $mode)
78    {
79        $this->mode = $mode;
80    }
81
82    /**
83     * Gets the EC level.
84     *
85     * @return ErrorCorrectionLevel
86     */
87    public function getErrorCorrectionLevel()
88    {
89        return $this->errorCorrectionLevel;
90    }
91
92    /**
93     * Sets the EC level.
94     *
95     * @param  ErrorCorrectionLevel $errorCorrectionLevel
96     * @return void
97     */
98    public function setErrorCorrectionLevel(ErrorCorrectionLevel $errorCorrectionLevel)
99    {
100        $this->errorCorrectionLevel = $errorCorrectionLevel;
101    }
102
103    /**
104     * Gets the version.
105     *
106     * @return Version
107     */
108    public function getVersion()
109    {
110        return $this->version;
111    }
112
113    /**
114     * Sets the version.
115     *
116     * @param  Version $version
117     * @return void
118     */
119    public function setVersion(Version $version)
120    {
121        $this->version = $version;
122    }
123
124    /**
125     * Gets the mask pattern.
126     *
127     * @return integer
128     */
129    public function getMaskPattern()
130    {
131        return $this->maskPattern;
132    }
133
134    /**
135     * Sets the mask pattern.
136     *
137     * @param  integer $maskPattern
138     * @return void
139     */
140    public function setMaskPattern($maskPattern)
141    {
142        $this->maskPattern = $maskPattern;
143    }
144
145    /**
146     * Gets the matrix.
147     *
148     * @return ByteMatrix
149     */
150    public function getMatrix()
151    {
152        return $this->matrix;
153    }
154
155    /**
156     * Sets the matrix.
157     *
158     * @param  ByteMatrix $matrix
159     * @return void
160     */
161    public function setMatrix(ByteMatrix $matrix)
162    {
163        $this->matrix = $matrix;
164    }
165
166    /**
167     * Validates whether a mask pattern is valid.
168     *
169     * @param  integer $maskPattern
170     * @return boolean
171     */
172    public static function isValidMaskPattern($maskPattern)
173    {
174        return $maskPattern > 0 && $maskPattern < self::NUM_MASK_PATTERNS;
175    }
176
177    /**
178     * Returns a string representation of the QR code.
179     *
180     * @return string
181     */
182    public function __toString()
183    {
184        $result = "<<\n"
185                . " mode: " . $this->mode . "\n"
186                . " ecLevel: " . $this->errorCorrectionLevel . "\n"
187                . " version: " . $this->version . "\n"
188                . " maskPattern: " . $this->maskPattern . "\n";
189
190        if ($this->matrix === null) {
191            $result .= " matrix: null\n";
192        } else {
193            $result .= " matrix:\n";
194            $result .= $this->matrix;
195        }
196
197        $result .= ">>\n";
198
199        return $result;
200    }
201}
202