Passed
Push — master ( 514bca...891b04 )
by smiley
02:57
created

QROptionsTrait::set_versionMin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * Trait QROptionsTrait
4
 *
5
 * @filesource   QROptionsTrait.php
6
 * @created      10.03.2018
7
 * @package      chillerlan\QRCode
8
 * @author       smiley <[email protected]>
9
 * @copyright    2018 smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\QRCode;
14
15
use function array_values, count, is_numeric, max, min, sprintf;
16
17
trait QROptionsTrait{
18
19
	/**
20
	 * QR Code version number
21
	 *
22
	 *   [1 ... 40] or QRCode::VERSION_AUTO
23
	 */
24
	protected int $version = QRCode::VERSION_AUTO;
25
26
	/**
27
	 * Minimum QR version (if $version = QRCode::VERSION_AUTO)
28
	 */
29
	protected int $versionMin = 1;
30
31
	/**
32
	 * Maximum QR version
33
	 */
34
	protected int $versionMax = 40;
35
36
	/**
37
	 * Error correct level
38
	 *
39
	 *   QRCode::ECC_X where X is
40
	 *    L =>  7%
41
	 *    M => 15%
42
	 *    Q => 25%
43
	 *    H => 30%
44
	 */
45
	protected int $eccLevel = QRCode::ECC_L;
46
47
	/**
48
	 * Mask Pattern to use
49
	 *
50
	 *   [0...7] or QRCode::MASK_PATTERN_AUTO
51
	 */
52
	protected int $maskPattern = QRCode::MASK_PATTERN_AUTO;
53
54
	/**
55
	 * Add a "quiet zone" (margin) according to the QR code spec
56
	 */
57
	protected bool $addQuietzone = true;
58
59
	/**
60
	 * Size of the quiet zone
61
	 *
62
	 *   internally clamped to [0 ... $moduleCount / 2], defaults to 4 modules
63
	 */
64
	protected int $quietzoneSize = 4;
65
66
	/**
67
	 * Use this to circumvent the data mode detection and force the usage of the given mode.
68
	 * valid modes are: Number, AlphaNum, Kanji, Byte
69
	 *
70
	 * @see https://github.com/chillerlan/php-qrcode/issues/39
71
	 */
72
	protected ?string $dataMode = null;
73
74
	/**
75
	 * QRCode::OUTPUT_MARKUP_XXXX where XXXX = HTML, SVG
76
	 * QRCode::OUTPUT_IMAGE_XXX where XXX = PNG, GIF, JPG
77
	 * QRCode::OUTPUT_STRING_XXXX where XXXX = TEXT, JSON
78
	 * QRCode::OUTPUT_CUSTOM
79
	 */
80
	protected string $outputType = QRCode::OUTPUT_IMAGE_PNG;
81
82
	/**
83
	 * the FQCN of the custom QROutputInterface if $outputType is set to QRCode::OUTPUT_CUSTOM
84
	 */
85
	protected ?string $outputInterface = null;
86
87
	/**
88
	 * /path/to/cache.file
89
	 */
90
	protected ?string $cachefile = null;
91
92
	/**
93
	 * newline string [HTML, SVG, TEXT]
94
	 */
95
	protected string $eol = PHP_EOL;
96
97
	/**
98
	 * size of a QR code pixel [SVG, IMAGE_*]
99
	 * HTML -> via CSS
100
	 */
101
	protected int $scale = 5;
102
103
	/**
104
	 * a common css class
105
	 */
106
	protected string $cssClass = '';
107
108
	/**
109
	 * SVG opacity
110
	 */
111
	protected float $svgOpacity = 1.0;
112
113
	/**
114
	 * anything between <defs>
115
	 *
116
	 * @see https://developer.mozilla.org/docs/Web/SVG/Element/defs
117
	 */
118
	protected string $svgDefs = '<style>rect{shape-rendering:crispEdges}</style>';
119
120
	/**
121
	 * SVG viewBox size. a single integer number which defines width/height of the viewBox attribute.
122
	 *
123
	 * viewBox="0 0 x x"
124
	 *
125
	 * @see https://css-tricks.com/scale-svg/#article-header-id-3
126
	 */
127
	protected ?int $svgViewBoxSize = null;
128
129
	/**
130
	 * string substitute for dark
131
	 */
132
	protected string $textDark = '🔴';
133
134
	/**
135
	 * string substitute for light
136
	 */
137
	protected string $textLight = '⭕';
138
139
	/**
140
	 * markup substitute for dark (CSS value)
141
	 */
142
	protected string $markupDark = '#000';
143
144
	/**
145
	 * markup substitute for light (CSS value)
146
	 */
147
	protected string $markupLight = '#fff';
148
149
	/**
150
	 * toggle base64 or raw image data
151
	 */
152
	protected bool $imageBase64 = true;
153
154
	/**
155
	 * toggle transparency, not supported by jpg
156
	 */
157
	protected bool $imageTransparent = true;
158
159
	/**
160
	 * @see imagecolortransparent()
161
	 *
162
	 * [R, G, B]
163
	 */
164
	protected array $imageTransparencyBG = [255, 255, 255];
165
166
	/**
167
	 * @see imagepng()
168
	 */
169
	protected int $pngCompression = -1;
170
171
	/**
172
	 * @see imagejpeg()
173
	 */
174
	protected int $jpegQuality = 85;
175
176
	/**
177
	 * Imagick output format
178
	 *
179
	 * @see Imagick::setType()
180
	 */
181
	protected string $imagickFormat = 'png';
182
183
	/**
184
	 * Imagick background color (defaults to "transparent")
185
	 *
186
	 * @see \ImagickPixel::__construct()
187
	 */
188
	protected ?string $imagickBG = null;
189
190
	/**
191
	 * Module values map
192
	 *
193
	 *   HTML, IMAGICK: #ABCDEF, cssname, rgb(), rgba()...
194
	 *   IMAGE: [63, 127, 255] // R, G, B
195
	 */
196
	protected ?array $moduleValues = null;
197
198
	/**
199
	 * clamp min/max version number
200
	 */
201
	protected function setMinMaxVersion(int $versionMin, int $versionMax):void{
202
		$min = max(1, min(40, $versionMin));
203
		$max = max(1, min(40, $versionMax));
204
205
		$this->versionMin = min($min, $max);
206
		$this->versionMax = max($min, $max);
207
	}
208
209
	/**
210
	 * sets the minimum version number
211
	 */
212
	protected function set_versionMin(int $version):void{
213
		$this->setMinMaxVersion($version, $this->versionMax);
214
	}
215
216
	/**
217
	 * sets the maximum version number
218
	 */
219
	protected function set_versionMax(int $version):void{
220
		$this->setMinMaxVersion($this->versionMin, $version);
221
	}
222
223
	/**
224
	 * sets the error correction level
225
	 *
226
	 * @throws \chillerlan\QRCode\QRCodeException
227
	 */
228
	protected function set_eccLevel(int $eccLevel):void{
229
230
		if(!isset(QRCode::ECC_MODES[$eccLevel])){
231
			throw new QRCodeException(sprintf('Invalid error correct level: %s', $eccLevel));
232
		}
233
234
		$this->eccLevel = $eccLevel;
235
	}
236
237
	/**
238
	 * sets/clamps the mask pattern
239
	 */
240
	protected function set_maskPattern(int $maskPattern):void{
241
242
		if($maskPattern !== QRCode::MASK_PATTERN_AUTO){
243
			$this->maskPattern = max(0, min(7, $maskPattern));
244
		}
245
246
	}
247
248
	/**
249
	 * sets the transparency background color
250
	 *
251
	 * @throws \chillerlan\QRCode\QRCodeException
252
	 */
253
	protected function set_imageTransparencyBG(array $imageTransparencyBG):void{
254
255
		// invalid value - set to white as default
256
		if(count($imageTransparencyBG) < 3){
257
			$this->imageTransparencyBG = [255, 255, 255];
258
259
			return;
260
		}
261
262
		foreach($imageTransparencyBG as $k => $v){
263
264
			if(!is_numeric($v)){
265
				throw new QRCodeException('Invalid RGB value.');
266
			}
267
268
			// clamp the values
269
			$this->imageTransparencyBG[$k] = max(0, min(255, (int)$v));
270
		}
271
272
		// use the array values to not run into errors with the spread operator (...$arr)
273
		$this->imageTransparencyBG = array_values($this->imageTransparencyBG);
274
	}
275
276
	/**
277
	 * sets/clamps the version number
278
	 */
279
	protected function set_version(int $version):void{
280
281
		if($version !== QRCode::VERSION_AUTO){
282
			$this->version = max(1, min(40, $version));
283
		}
284
285
	}
286
287
}
288