Test Failed
Push — master ( 632146...c14ce3 )
by smiley
02:13
created

QROptionsTrait::clampRGBValues()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 4
nop 0
dl 0
loc 22
rs 9.2568
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
	 * Sets the options, called internally by the constructor
235
	 *
236
	 * @return void
237
	 * @throws \chillerlan\QRCode\QRCodeException
238
	 */
239
	public function QROptionsTrait():void{
240
241
		if(!array_key_exists($this->eccLevel, QRCode::ECC_MODES)){
242
			throw new QRCodeException('Invalid error correct level: '.$this->eccLevel);
243
		}
244
245
		if(in_array($this->outputType, QRCode::OUTPUT_MODES[QRImage::class], true)){
246
			$this->clampRGBValues();
247
		}
248
249
		$this->version = max(1, min(40, (int)$this->version));
250
251
		// clamp min/max version number
252
		$min = max(1, min(40, (int)$this->versionMin));
253
		$max = max(1, min(40, (int)$this->versionMax));
254
255
		$this->versionMin = min($min, $max);
256
		$this->versionMax = max($min, $max);
257
258
		if($this->maskPattern !== QRCode::MASK_PATTERN_AUTO){
259
			$this->maskPattern = max(0, min(7, (int)$this->maskPattern));
260
		}
261
	}
262
263
	/**
264
	 * @throws \chillerlan\QRCode\QRCodeException
265
	 */
266
	protected function clampRGBValues():void{
267
268
		if(!is_array($this->imageTransparencyBG) || count($this->imageTransparencyBG) < 3){
269
			$this->imageTransparencyBG = [255, 255, 255];
270
		}
271
		else{
272
273
			foreach($this->imageTransparencyBG as $k => $v){
274
275
				if(!is_numeric($v)){
276
					throw new QRCodeException('Invalid RGB value.');
277
				}
278
279
				// clamp the values
280
				$this->imageTransparencyBG[$k] = max(0, min(255, (int)$v));
281
			}
282
283
			// use the array values to not run into errors with the spread operator (...$arr)
284
			$this->imageTransparencyBG = array_values($this->imageTransparencyBG);
285
		}
286
287
	}
288
}
289