Completed
Push — master ( 4bf2b0...464667 )
by smiley
02:01
created

QROptionsTrait::set_versionMax()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
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_array, 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
	 * @var int
25
	 */
26
	protected $version = QRCode::VERSION_AUTO;
27
28
	/**
29
	 * Minimum QR version (if $version = QRCode::VERSION_AUTO)
30
	 *
31
	 * @var int
32
	 */
33
	protected $versionMin = 1;
34
35
	/**
36
	 * Maximum QR version
37
	 *
38
	 * @var int
39
	 */
40
	protected $versionMax = 40;
41
42
	/**
43
	 * Error correct level
44
	 *
45
	 *   QRCode::ECC_X where X is
46
	 *    L =>  7%
47
	 *    M => 15%
48
	 *    Q => 25%
49
	 *    H => 30%
50
	 *
51
	 * @var int
52
	 */
53
	protected $eccLevel = QRCode::ECC_L;
54
55
	/**
56
	 * Mask Pattern to use
57
	 *
58
	 *  [0...7] or QRCode::MASK_PATTERN_AUTO
59
	 *
60
	 * @var int
61
	 */
62
	protected $maskPattern = QRCode::MASK_PATTERN_AUTO;
63
64
	/**
65
	 * Add a "quiet zone" (margin) according to the QR code spec
66
	 *
67
	 * @var bool
68
	 */
69
	protected $addQuietzone = true;
70
71
	/**
72
	 *  Size of the quiet zone
73
	 *
74
	 *   internally clamped to [0 ... $moduleCount / 2], defaults to 4 modules
75
	 *
76
	 * @var int
77
	 */
78
	protected $quietzoneSize = 4;
79
80
	/**
81
	 * QRCode::OUTPUT_MARKUP_XXXX where XXXX = HTML, SVG
82
	 * QRCode::OUTPUT_IMAGE_XXX where XXX = PNG, GIF, JPG
83
	 * QRCode::OUTPUT_STRING_XXXX where XXXX = TEXT, JSON
84
	 * QRCode::OUTPUT_CUSTOM
85
	 *
86
	 * @var string
87
	 */
88
	protected $outputType = QRCode::OUTPUT_IMAGE_PNG;
89
90
	/**
91
	 * the FQCN of the custom QROutputInterface if $outputType is set to QRCode::OUTPUT_CUSTOM
92
	 *
93
	 * @var string
94
	 */
95
	protected $outputInterface;
96
97
	/**
98
	 * /path/to/cache.file
99
	 *
100
	 * @var string
101
	 */
102
	protected $cachefile;
103
104
	/**
105
	 * newline string [HTML, SVG, TEXT]
106
	 *
107
	 * @var string
108
	 */
109
	protected $eol = PHP_EOL;
110
111
	/**
112
	 * size of a QR code pixel [SVG, IMAGE_*]
113
	 * HTML -> via CSS
114
	 *
115
	 * @var int
116
	 */
117
	protected $scale = 5;
118
119
	/**
120
	 * a common css class
121
	 *
122
	 * @var string
123
	 */
124
	protected $cssClass;
125
126
	/**
127
	 * SVG opacity
128
	 *
129
	 * @var float
130
	 */
131
	protected $svgOpacity = 1.0;
132
133
	/**
134
	 * anything between <defs>
135
	 *
136
	 * @see https://developer.mozilla.org/docs/Web/SVG/Element/defs
137
	 *
138
	 * @var string
139
	 */
140
	protected $svgDefs = '<style>rect{shape-rendering:crispEdges}</style>';
141
142
	/**
143
	 * SVG viewBox size. a single integer number which defines width/height of the viewBox attribute.
144
	 *
145
	 * viewBox="0 0 x x"
146
	 *
147
	 * @see https://css-tricks.com/scale-svg/#article-header-id-3
148
	 *
149
	 * @var int
150
	 */
151
	protected $svgViewBoxSize;
152
153
	/**
154
	 * string substitute for dark
155
	 *
156
	 * @var string
157
	 */
158
	protected $textDark = '🔴';
159
160
	/**
161
	 * string substitute for light
162
	 *
163
	 * @var string
164
	 */
165
	protected $textLight = '⭕';
166
167
	/**
168
	 * markup substitute for dark (CSS value)
169
	 *
170
	 * @var string
171
	 */
172
	protected $markupDark = '#000';
173
174
	/**
175
	 * markup substitute for light (CSS value)
176
	 *
177
	 * @var string
178
	 */
179
	protected $markupLight = '#fff';
180
181
	/**
182
	 * toggle base64 or raw image data
183
	 *
184
	 * @var bool
185
	 */
186
	protected $imageBase64 = true;
187
188
	/**
189
	 * toggle transparency, not supported by jpg
190
	 *
191
	 * @var bool
192
	 */
193
	protected $imageTransparent = true;
194
195
	/**
196
	 * @see imagecolortransparent()
197
	 *
198
	 * @var array [R, G, B]
199
	 */
200
	protected $imageTransparencyBG = [255, 255, 255];
201
202
	/**
203
	 * @see imagepng()
204
	 *
205
	 * @var int
206
	 */
207
	protected $pngCompression = -1;
208
209
	/**
210
	 * @see imagejpeg()
211
	 *
212
	 * @var int
213
	 */
214
	protected $jpegQuality = 85;
215
216
	/**
217
	 * Imagick output format
218
	 *
219
	 * @see Imagick::setType()
220
	 *
221
	 * @var string
222
	 */
223
	protected $imagickFormat = 'png';
224
225
	/**
226
	 * Imagick background color (defaults to "transparent")
227
	 *
228
	 * @see \ImagickPixel::__construct()
229
	 *
230
	 * @var string
231
	 */
232
	protected $imagickBG;
233
234
	/**
235
	 * Module values map
236
	 *
237
	 *   HTML, IMAGICK: #ABCDEF, cssname, rgb(), rgba()...
238
	 *   IMAGE: [63, 127, 255] // R, G, B
239
	 *
240
	 * @var array
241
	 */
242
	protected $moduleValues;
243
244
	/**
245
	 * clamp min/max version number
246
	 *
247
	 * @param int $versionMin
248
	 * @param int $versionMax
249
	 *
250
	 * @return void
251
	 */
252
	protected function setMinMaxVersion(int $versionMin, int $versionMax):void{
253
		$min = max(1, min(40, $versionMin));
254
		$max = max(1, min(40, $versionMax));
255
256
		$this->versionMin = min($min, $max);
257
		$this->versionMax = max($min, $max);
258
	}
259
260
	/**
261
	 * @param int $version
262
	 *
263
	 * @return void
264
	 */
265
	protected function set_versionMin(int $version):void{
266
		$this->setMinMaxVersion($version, $this->versionMax);
267
	}
268
269
	/**
270
	 * @param int $version
271
	 *
272
	 * @return void
273
	 */
274
	protected function set_versionMax(int $version):void{
275
		$this->setMinMaxVersion($this->versionMin, $version);
276
	}
277
278
	/**
279
	 * @param int $eccLevel
280
	 *
281
	 * @return void
282
	 * @throws \chillerlan\QRCode\QRCodeException
283
	 */
284
	protected function set_eccLevel(int $eccLevel):void{
285
286
		if(!isset(QRCode::ECC_MODES[$eccLevel])){
287
			throw new QRCodeException(sprintf('Invalid error correct level: %s', $eccLevel));
288
		}
289
290
		$this->eccLevel = $eccLevel;
291
	}
292
293
	/**
294
	 * @param int $maskPattern
295
	 *
296
	 * @return void
297
	 */
298
	protected function set_maskPattern(int $maskPattern):void{
299
300
		if($maskPattern !== QRCode::MASK_PATTERN_AUTO){
301
			$this->maskPattern = max(0, min(7, $maskPattern));
302
		}
303
304
	}
305
306
	/**
307
	 * @param mixed $imageTransparencyBG
308
	 *
309
	 * @return void
310
	 * @throws \chillerlan\QRCode\QRCodeException
311
	 */
312
	protected function set_imageTransparencyBG($imageTransparencyBG):void{
313
314
		// invalid value - set to white as default
315
		if(!is_array($imageTransparencyBG) || count($imageTransparencyBG) < 3){
316
			$this->imageTransparencyBG = [255, 255, 255];
317
318
			return;
319
		}
320
321
		foreach($imageTransparencyBG as $k => $v){
322
323
			if(!is_numeric($v)){
324
				throw new QRCodeException('Invalid RGB value.');
325
			}
326
327
			// clamp the values
328
			$this->imageTransparencyBG[$k] = max(0, min(255, (int)$v));
329
		}
330
331
		// use the array values to not run into errors with the spread operator (...$arr)
332
		$this->imageTransparencyBG = array_values($this->imageTransparencyBG);
333
	}
334
335
	/**
336
	 * @param int $version
337
	 *
338
	 * @return void
339
	 */
340
	protected function set_version(int $version):void{
341
342
		if($version !== QRCode::VERSION_AUTO){
343
			$this->version = max(1, min(40, $version));
344
		}
345
346
	}
347
348
}
349