Bläddra i källkod

:octocat: clarify default module value setting

codemasher 4 år sedan
förälder
incheckning
3e14260144

+ 29 - 7
examples/MyCustomOutput.php

@@ -14,20 +14,42 @@ use chillerlan\QRCode\Output\QROutputAbstract;
 
 class MyCustomOutput extends QROutputAbstract{
 
-	protected function setModuleValues():void{
-		// TODO: Implement setModuleValues() method.
+	/**
+	 * @inheritDoc
+	 */
+	protected function moduleValueIsValid($value):bool{
+		// TODO: Implement moduleValueIsValid() method. (abstract)
+		return false;
 	}
 
-	public function dump(string $file = null):string{
+	/**
+	 * @inheritDoc
+	 */
+	protected function getModuleValue($value){
+		// TODO: Implement getModuleValue() method. (abstract)
+		return null;
+	}
 
+	/**
+	 * @inheritDoc
+	 */
+	protected function getDefaultModuleValue(bool $isDark){
+		// TODO: Implement getDefaultModuleValue() method. (abstract)
+		return null;
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	public function dump(string $file = null):string{
 		$output = '';
 
-		for($row = 0; $row < $this->moduleCount; $row++){
-			for($col = 0; $col < $this->moduleCount; $col++){
-				$output .= (int)$this->matrix->check($col, $row);
+		for($y = 0; $y < $this->moduleCount; $y++){
+			for($x = 0; $x < $this->moduleCount; $x++){
+				$output .= (int)$this->matrix->check($x, $y);
 			}
 
-			$output .= \PHP_EOL;
+			$output .= $this->options->eol;
 		}
 
 		return $output;

+ 15 - 15
src/Output/QRFpdf.php

@@ -47,22 +47,22 @@ class QRFpdf extends QROutputAbstract{
 	/**
 	 * @inheritDoc
 	 */
-	protected function setModuleValues():void{
-
-		foreach($this::DEFAULT_MODULE_VALUES as $M_TYPE => $defaultValue){
-			$v = $this->options->moduleValues[$M_TYPE] ?? null;
-
-			if(!is_array($v) || count($v) < 3){
-				$this->moduleValues[$M_TYPE] = $defaultValue
-					? [0, 0, 0]
-					: [255, 255, 255];
-			}
-			else{
-				$this->moduleValues[$M_TYPE] = array_values($v);
-			}
+	protected function moduleValueIsValid($value):bool{
+		return is_array($value) && count($value) >= 3;
+	}
 
-		}
+	/**
+	 * @inheritDoc
+	 */
+	protected function getModuleValue($value):array{
+		return array_values($value);
+	}
 
+	/**
+	 * @inheritDoc
+	 */
+	protected function getDefaultModuleValue(bool $isDark):array{
+		return $isDark ? [0, 0, 0] : [255, 255, 255];
 	}
 
 	/**
@@ -84,7 +84,7 @@ class QRFpdf extends QROutputAbstract{
 				/** @var int $M_TYPE */
 				$color = $this->moduleValues[$M_TYPE];
 
-				if($prevColor === null || $prevColor !== $color){
+				if($prevColor !== $color){
 					/** @phan-suppress-next-line PhanParamTooFewUnpack */
 					$fpdf->SetFillColor(...$color);
 					$prevColor = $color;

+ 14 - 14
src/Output/QRImage.php

@@ -66,22 +66,22 @@ class QRImage extends QROutputAbstract{
 	/**
 	 * @inheritDoc
 	 */
-	protected function setModuleValues():void{
-
-		foreach($this::DEFAULT_MODULE_VALUES as $M_TYPE => $defaultValue){
-			$v = $this->options->moduleValues[$M_TYPE] ?? null;
-
-			if(!is_array($v) || count($v) < 3){
-				$this->moduleValues[$M_TYPE] = $defaultValue
-					? [0, 0, 0]
-					: [255, 255, 255];
-			}
-			else{
-				$this->moduleValues[$M_TYPE] = array_values($v);
-			}
+	protected function moduleValueIsValid($value):bool{
+		return is_array($value) && count($value) >= 3;
+	}
 
-		}
+	/**
+	 * @inheritDoc
+	 */
+	protected function getModuleValue($value):array{
+		return array_values($value);
+	}
 
+	/**
+	 * @inheritDoc
+	 */
+	protected function getDefaultModuleValue(bool $isDark):array{
+		return $isDark ? [0, 0, 0] : [255, 255, 255];
 	}
 
 	/**

+ 16 - 14
src/Output/QRImagick.php

@@ -46,20 +46,22 @@ class QRImagick extends QROutputAbstract{
 	/**
 	 * @inheritDoc
 	 */
-	protected function setModuleValues():void{
+	protected function moduleValueIsValid($value):bool{
+		return is_string($value);
+	}
 
-		foreach($this::DEFAULT_MODULE_VALUES as $type => $defaultValue){
-			$v = $this->options->moduleValues[$type] ?? null;
+	/**
+	 * @inheritDoc
+	 */
+	protected function getModuleValue($value):ImagickPixel{
+		return new ImagickPixel($value);
+	}
 
-			if(!is_string($v)){
-				$this->moduleValues[$type] = $defaultValue
-					? new ImagickPixel($this->options->markupDark)
-					: new ImagickPixel($this->options->markupLight);
-			}
-			else{
-				$this->moduleValues[$type] = new ImagickPixel($v);
-			}
-		}
+	/**
+	 * @inheritDoc
+	 */
+	protected function getDefaultModuleValue(bool $isDark):ImagickPixel{
+		return new ImagickPixel($isDark ? $this->options->markupDark : $this->options->markupLight);
 	}
 
 	/**
@@ -68,7 +70,7 @@ class QRImagick extends QROutputAbstract{
 	 * @return string|\Imagick
 	 */
 	public function dump(string $file = null){
-		$file ??= $this->options->cachefile;
+		$file          ??= $this->options->cachefile;
 		$this->imagick = new Imagick;
 
 		$this->imagick->newImage(
@@ -129,7 +131,7 @@ class QRImagick extends QROutputAbstract{
 				$y * $this->scale,
 				($x + 1) * $this->scale,
 				($y + 1) * $this->scale
-		);
+			);
 	}
 
 }

+ 14 - 14
src/Output/QRMarkup.php

@@ -25,22 +25,22 @@ class QRMarkup extends QROutputAbstract{
 	/**
 	 * @inheritDoc
 	 */
-	protected function setModuleValues():void{
-
-		foreach($this::DEFAULT_MODULE_VALUES as $M_TYPE => $defaultValue){
-			$v = $this->options->moduleValues[$M_TYPE] ?? null;
-
-			if(!is_string($v)){
-				$this->moduleValues[$M_TYPE] = $defaultValue
-					? $this->options->markupDark
-					: $this->options->markupLight;
-			}
-			else{
-				$this->moduleValues[$M_TYPE] = trim(strip_tags($v), " '\"\r\n\t");
-			}
+	protected function moduleValueIsValid($value):bool{
+		return is_string($value);
+	}
 
-		}
+	/**
+	 * @inheritDoc
+	 */
+	protected function getModuleValue($value):string{
+		return trim(strip_tags($value), " '\"\r\n\t");
+	}
 
+	/**
+	 * @inheritDoc
+	 */
+	protected function getDefaultModuleValue(bool $isDark):string{
+		return $isDark ? $this->options->markupDark : $this->options->markupLight;
 	}
 
 	/**

+ 35 - 2
src/Output/QROutputAbstract.php

@@ -84,9 +84,42 @@ abstract class QROutputAbstract implements QROutputInterface{
 	}
 
 	/**
-	 * Sets the initial module values (clean-up & defaults)
+	 * Sets the initial module values
 	 */
-	abstract protected function setModuleValues():void;
+	protected function setModuleValues():void{
+
+		foreach($this::DEFAULT_MODULE_VALUES as $M_TYPE => $defaultValue){
+			$value = $this->options->moduleValues[$M_TYPE] ?? null;
+
+			$this->moduleValues[$M_TYPE] = $this->moduleValueIsValid($value)
+				? $this->getModuleValue($value)
+				: $this->getDefaultModuleValue($defaultValue);
+		}
+
+	}
+
+	/**
+	 * Determines whether the given value is valid
+	 *
+	 * @param mixed|null $value
+	 */
+	abstract protected function moduleValueIsValid($value):bool;
+
+	/**
+	 * Returns the final value for the given input (return value depends on the output module)
+	 *
+	 * @param mixed $value
+	 *
+	 * @return mixed
+	 */
+	abstract protected function getModuleValue($value);
+
+	/**
+	 * Returns a defualt value for either dark or light modules (return value depends on the output module)
+	 *
+	 * @return mixed
+	 */
+	abstract protected function getDefaultModuleValue(bool $isDark);
 
 	/**
 	 * Returns a base64 data URI for the given string and mime type

+ 14 - 14
src/Output/QRString.php

@@ -27,22 +27,22 @@ class QRString extends QROutputAbstract{
 	/**
 	 * @inheritDoc
 	 */
-	protected function setModuleValues():void{
-
-		foreach($this::DEFAULT_MODULE_VALUES as $M_TYPE => $defaultValue){
-			$v = $this->options->moduleValues[$M_TYPE] ?? null;
-
-			if(!is_string($v)){
-				$this->moduleValues[$M_TYPE] = $defaultValue
-					? $this->options->textDark
-					: $this->options->textLight;
-			}
-			else{
-				$this->moduleValues[$M_TYPE] = $v;
-			}
+	protected function moduleValueIsValid($value):bool{
+		return is_string($value);
+	}
 
-		}
+	/**
+	 * @inheritDoc
+	 */
+	protected function getModuleValue($value):string{
+		return $value;
+	}
 
+	/**
+	 * @inheritDoc
+	 */
+	protected function getDefaultModuleValue(bool $isDark):string{
+		return $isDark ? $this->options->textDark : $this->options->textLight;
 	}
 
 	/**