Test Failed
Push — master ( 3c8af1...9dafd4 )
by smiley
03:49
created

QROptionsTrait::__set()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 2
dl 0
loc 20
rs 9.6
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 chillerlan\QRCode\Output\QRImage;
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
	 * string substitute for dark
144
	 *
145
	 * @var string
146
	 */
147
	protected $textDark = '🔴';
148
149
	/**
150
	 * string substitute for light
151
	 *
152
	 * @var string
153
	 */
154
	protected $textLight = '⭕';
155
156
	/**
157
	 * markup substitute for dark (CSS value)
158
	 *
159
	 * @var string
160
	 */
161
	protected $markupDark = '#000';
162
163
	/**
164
	 * markup substitute for light (CSS value)
165
	 *
166
	 * @var string
167
	 */
168
	protected $markupLight = '#fff';
169
170
	/**
171
	 * toggle base64 or raw image data
172
	 *
173
	 * @var bool
174
	 */
175
	protected $imageBase64 = true;
176
177
	/**
178
	 * toggle transparency, not supported by jpg
179
	 *
180
	 * @var bool
181
	 */
182
	protected $imageTransparent = true;
183
184
	/**
185
	 * @see imagecolortransparent()
186
	 *
187
	 * @var array [R, G, B]
188
	 */
189
	protected $imageTransparencyBG = [255, 255, 255];
190
191
	/**
192
	 * @see imagepng()
193
	 *
194
	 * @var int
195
	 */
196
	protected $pngCompression = -1;
197
198
	/**
199
	 * @see imagejpeg()
200
	 *
201
	 * @var int
202
	 */
203
	protected $jpegQuality = 85;
204
205
	/**
206
	 * Imagick output format
207
	 *
208
	 * @see Imagick::setType()
209
	 *
210
	 * @var string
211
	 */
212
	protected $imagickFormat = 'png';
213
214
	/**
215
	 * Imagick background color (defaults to "transparent")
216
	 *
217
	 * @see \ImagickPixel::__construct()
218
	 *
219
	 * @var string
220
	 */
221
	protected $imagickBG;
222
223
	/**
224
	 * Module values map
225
	 *
226
	 *   HTML, IMAGICK: #ABCDEF, cssname, rgb(), rgba()...
227
	 *   IMAGE: [63, 127, 255] // R, G, B
228
	 *
229
	 * @var array
230
	 */
231
	protected $moduleValues;
232
233
	/**
234
	 * set/clamp some special values, call the parent setter otherwise
235
	 *
236
	 * @param string $property
237
	 * @param mixed  $value
238
	 *
239
	 * @return void
240
	 */
241
	public function __set(string $property, $value):void{
242
243
		if(in_array($property, ['eccLevel', 'maskPattern', 'imageTransparencyBG', 'version'], true)){
244
			$this->{'set_'.$property}($value);
245
246
			return;
247
		}
248
		elseif($property === 'versionMin'){
249
			$this->setMinMaxVersion($value, $this->versionMax);
250
251
			return;
252
		}
253
		elseif($property === 'versionMax'){
254
			$this->setMinMaxVersion($this->versionMin, $value);
255
256
			return;
257
		}
258
259
		parent::__set($property, $value);
260
	}
261
262
	/**
263
	 * clamp min/max version number
264
	 *
265
	 * @param int $versionMin
266
	 * @param int $versionMax
267
	 *
268
	 * @return void
269
	 */
270
	protected function setMinMaxVersion(int $versionMin, int $versionMax):void{
271
		$min = max(1, min(40, $versionMin));
272
		$max = max(1, min(40, $versionMax));
273
274
		$this->versionMin = min($min, $max);
275
		$this->versionMax = max($min, $max);
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('Invalid error correct level: '.$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