codemasher 7 лет назад
Родитель
Сommit
c13e006947
3 измененных файлов с 28 добавлено и 32 удалено
  1. 1 30
      src/QRCode.php
  2. 25 0
      src/QROptionsTrait.php
  3. 2 2
      tests/QRCodeTest.php

+ 1 - 30
src/QRCode.php

@@ -108,36 +108,7 @@ class QRCode{
 	public function __construct(SettingsContainerInterface $options = null){
 		mb_internal_encoding('UTF-8');
 
-		$this->setOptions($options ?? new QROptions);
-	}
-
-	/**
-	 * Sets the options, called internally by the constructor
-	 *
-	 * @param \chillerlan\Settings\SettingsContainerInterface $options
-	 *
-	 * @return \chillerlan\QRCode\QRCode
-	 * @throws \chillerlan\QRCode\QRCodeException
-	 */
-	public function setOptions(SettingsContainerInterface $options):QRCode{
-
-		if(!array_key_exists($options->eccLevel, $this::ECC_MODES)){
-			throw new QRCodeException('Invalid error correct level: '.$options->eccLevel);
-		}
-
-		if(!is_array($options->imageTransparencyBG) || count($options->imageTransparencyBG) < 3){
-			$options->imageTransparencyBG = [255, 255, 255];
-		}
-
-		$options->version = (int)$options->version;
-
-		// clamp min/max version number
-		$options->versionMin = (int)min($options->versionMin, $options->versionMax);
-		$options->versionMax = (int)max($options->versionMin, $options->versionMax);
-
-		$this->options = $options;
-
-		return $this;
+		$this->options = $options ?? new QROptions;
 	}
 
 	/**

+ 25 - 0
src/QROptionsTrait.php

@@ -218,4 +218,29 @@ trait QROptionsTrait{
 		QRMatrix::M_TEST << 8       => true,  // 65280
 	];
 
+	/**
+	 * Sets the options, called internally by the constructor
+	 *
+	 * @return void
+	 * @throws \chillerlan\QRCode\QRCodeException
+	 */
+	public function QROptionsTrait():void{
+
+		if(!array_key_exists($this->eccLevel, QRCode::ECC_MODES)){
+			throw new QRCodeException('Invalid error correct level: '.$this->eccLevel);
+		}
+
+		if(!is_array($this->imageTransparencyBG) || count($this->imageTransparencyBG) < 3){
+			$this->imageTransparencyBG = [255, 255, 255];
+		}
+
+		$this->version = (int)$this->version;
+
+		// clamp min/max version number
+		$max = $this->versionMax;
+		$min = $this->versionMin;
+		$this->versionMin = (int)min($min, $max);
+		$this->versionMax = (int)max($min, $max);
+	}
+
 }

+ 2 - 2
tests/QRCodeTest.php

@@ -75,7 +75,7 @@ class QRCodeTest extends QRTestAbstract{
 	 * @expectedExceptionMessage Invalid error correct level: 42
 	 */
 	public function testSetOptionsException(){
-		$this->qrcode->setOptions(new QROptions(['eccLevel' => 42]));
+		new QROptions(['eccLevel' => 42]);
 	}
 
 	/**
@@ -83,7 +83,7 @@ class QRCodeTest extends QRTestAbstract{
 	 * @expectedExceptionMessage invalid output type
 	 */
 	public function testInitDataInterfaceException(){
-		$this->qrcode->setOptions(new QROptions(['outputType' => 'foo']))->render('test');
+		(new QRCode(new QROptions(['outputType' => 'foo'])))->render('test');
 	}
 
 	/**