@@ -231,60 +231,118 @@ trait QROptionsTrait{
231231 protected $ moduleValues ;
232232
233233 /**
234- * Sets the options, called internally by the constructor
234+ * set/clamp some special values, call the parent setter otherwise
235+ *
236+ * @param string $property
237+ * @param mixed $value
235238 *
236239 * @return void
237- * @throws \chillerlan\QRCode\QRCodeException
238240 */
239- public function QROptionsTrait ():void {
241+ public function __set (string $ property , $ value ):void {
242+
243+ if (in_array ($ property , ['eccLevel ' , 'maskPattern ' , 'imageTransparencyBG ' , 'version ' ], true )){
244+ $ this ->{'set_ ' .$ property }($ value );
240245
241- if (!array_key_exists ($ this ->eccLevel , QRCode::ECC_MODES )){
242- throw new QRCodeException ('Invalid error correct level: ' .$ this ->eccLevel );
246+ return ;
243247 }
248+ elseif ($ property === 'versionMin ' ){
249+ $ this ->setMinMaxVersion ($ value , $ this ->versionMax );
244250
245- if (in_array ($ this ->outputType , QRCode::OUTPUT_MODES [QRImage::class], true )){
246- $ this ->clampRGBValues ();
251+ return ;
247252 }
253+ elseif ($ property === 'versionMax ' ){
254+ $ this ->setMinMaxVersion ($ this ->versionMin , $ value );
248255
249- if ($ this ->version !== QRCode::VERSION_AUTO ){
250- $ this ->version = max (1 , min (40 , (int )$ this ->version ));
256+ return ;
251257 }
252258
253- // clamp min/max version number
254- $ min = max (1 , min (40 , (int )$ this ->versionMin ));
255- $ max = max (1 , min (40 , (int )$ this ->versionMax ));
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 ));
256273
257274 $ this ->versionMin = min ($ min , $ max );
258275 $ this ->versionMax = max ($ min , $ max );
276+ }
259277
260- if ($ this ->maskPattern !== QRCode::MASK_PATTERN_AUTO ){
261- $ this ->maskPattern = max (0 , min (7 , (int )$ this ->maskPattern ));
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 );
262288 }
289+
290+ $ this ->eccLevel = $ eccLevel ;
263291 }
264292
265293 /**
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
266310 * @throws \chillerlan\QRCode\QRCodeException
267311 */
268- protected function clampRGBValues ( ):void {
312+ protected function set_imageTransparencyBG ( $ imageTransparencyBG ):void {
269313
270- if (!is_array ($ this ->imageTransparencyBG ) || count ($ this ->imageTransparencyBG ) < 3 ){
314+ // invalid value - set to white as default
315+ if (!is_array ($ imageTransparencyBG ) || count ($ imageTransparencyBG ) < 3 ){
271316 $ this ->imageTransparencyBG = [255 , 255 , 255 ];
272- }
273- else {
274317
275- foreach ($ this ->imageTransparencyBG as $ k => $ v ){
318+ return ;
319+ }
276320
277- if (!is_numeric ($ v )){
278- throw new QRCodeException ('Invalid RGB value. ' );
279- }
321+ foreach ($ imageTransparencyBG as $ k => $ v ){
280322
281- // clamp the values
282- $ this -> imageTransparencyBG [ $ k ] = max ( 0 , min ( 255 , ( int ) $ v ) );
323+ if (! is_numeric ( $ v )){
324+ throw new QRCodeException ( ' Invalid RGB value. ' );
283325 }
284326
285- // use the array values to not run into errors with the spread operator (...$arr)
286- $ this ->imageTransparencyBG = array_values ( $ this -> imageTransparencyBG );
327+ // clamp the values
328+ $ this ->imageTransparencyBG [ $ k ] = max ( 0 , min ( 255 , ( int ) $ v ) );
287329 }
288330
331+ // use the array values to not run into errors with the spread operator (...$arr)
332+ $ this ->imageTransparencyBG = array_values ($ this ->imageTransparencyBG );
289333 }
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+
290348}
0 commit comments