diff --git a/examples/MyCustomOutput.php b/examples/MyCustomOutput.php index 40f4aae2b..55f01278e 100644 --- a/examples/MyCustomOutput.php +++ b/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; diff --git a/src/Output/QRFpdf.php b/src/Output/QRFpdf.php index 062c0f9c8..2a38ff95e 100644 --- a/src/Output/QRFpdf.php +++ b/src/Output/QRFpdf.php @@ -47,22 +47,22 @@ class QRFpdf extends QROutputAbstract{ /** * @inheritDoc */ - protected function setModuleValues():void{ + protected function moduleValueIsValid($value):bool{ + return is_array($value) && count($value) >= 3; + } - 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); - } - - } + /** + * @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; diff --git a/src/Output/QRImage.php b/src/Output/QRImage.php index ce80424b4..471a11ce9 100644 --- a/src/Output/QRImage.php +++ b/src/Output/QRImage.php @@ -66,22 +66,22 @@ class QRImage extends QROutputAbstract{ /** * @inheritDoc */ - protected function setModuleValues():void{ + protected function moduleValueIsValid($value):bool{ + return is_array($value) && count($value) >= 3; + } - 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); - } - - } + /** + * @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]; } /** diff --git a/src/Output/QRImagick.php b/src/Output/QRImagick.php index 1ebed18d4..3e46ed8e1 100644 --- a/src/Output/QRImagick.php +++ b/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 - ); + ); } } diff --git a/src/Output/QRMarkup.php b/src/Output/QRMarkup.php index 7447b7f74..eef52bdee 100644 --- a/src/Output/QRMarkup.php +++ b/src/Output/QRMarkup.php @@ -25,22 +25,22 @@ class QRMarkup extends QROutputAbstract{ /** * @inheritDoc */ - protected function setModuleValues():void{ + protected function moduleValueIsValid($value):bool{ + return is_string($value); + } - 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"); - } - - } + /** + * @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; } /** diff --git a/src/Output/QROutputAbstract.php b/src/Output/QROutputAbstract.php index e8d2b82ef..6aa552c14 100644 --- a/src/Output/QROutputAbstract.php +++ b/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 diff --git a/src/Output/QRString.php b/src/Output/QRString.php index a5dbcf8d8..a70c51568 100644 --- a/src/Output/QRString.php +++ b/src/Output/QRString.php @@ -27,22 +27,22 @@ class QRString extends QROutputAbstract{ /** * @inheritDoc */ - protected function setModuleValues():void{ + protected function moduleValueIsValid($value):bool{ + return is_string($value); + } - 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; - } - - } + /** + * @inheritDoc + */ + protected function getModuleValue($value):string{ + return $value; + } + /** + * @inheritDoc + */ + protected function getDefaultModuleValue(bool $isDark):string{ + return $isDark ? $this->options->textDark : $this->options->textLight; } /**