Passed
Push — master ( 5e6c02...a0bc42 )
by smiley
01:35
created

QROptionsTrait::set_fpdfMeasureUnit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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