diff --git a/examples/svgMeltedModules.php b/examples/svgMeltedModules.php index 200a38a1a..628654b71 100644 --- a/examples/svgMeltedModules.php +++ b/examples/svgMeltedModules.php @@ -32,7 +32,7 @@ class MeltedSVGQRCodeOutput extends QRMarkupSVG{ return sprintf('', $this->getCssClass($M_TYPE), $path); } - protected function collectModules(Closure $transform):array{ + protected function collectModules():array{ $paths = []; $melt = $this->options->melt; // avoid magic getter in long loops @@ -57,7 +57,7 @@ class MeltedSVGQRCodeOutput extends QRMarkupSVG{ } // collect the modules per $M_TYPE - $module = $transform($x, $y, $M_TYPE, $M_TYPE_LAYER); + $module = $this->moduleTransform($x, $y, $M_TYPE, $M_TYPE_LAYER); if(!empty($module)){ $paths[$M_TYPE_LAYER][] = $module; @@ -71,7 +71,7 @@ class MeltedSVGQRCodeOutput extends QRMarkupSVG{ return $paths; } - protected function module(int $x, int $y, int $M_TYPE):string{ + protected function moduleTransform(int $x, int $y, int $M_TYPE, int $M_TYPE_LAYER):string{ $bits = $this->matrix->checkNeighbours($x, $y, null); $check = fn(int $all, int $any = 0):bool => ($bits & ($all | (~$any & 0xff))) === $all; diff --git a/examples/svgModuleJitter.php b/examples/svgModuleJitter.php index f3a62d0ce..e3aa38a90 100644 --- a/examples/svgModuleJitter.php +++ b/examples/svgModuleJitter.php @@ -42,11 +42,11 @@ class ModuleJitterSVGoutput extends QRMarkupSVG{ return (random_int(0, PHP_INT_MAX) / PHP_INT_MAX); } - protected function module(int $x, int $y, int $M_TYPE):string{ + protected function moduleTransform(int $x, int $y, int $M_TYPE, int $M_TYPE_LAYER):string|null{ // skip light modules if((!$this->options->drawLightModules && !$this->matrix->check($x, $y))){ - return ''; + return null; } // early exit on pure square modules diff --git a/examples/svgRandomColoredDots.php b/examples/svgRandomColoredDots.php index 4b9f22288..80079b782 100644 --- a/examples/svgRandomColoredDots.php +++ b/examples/svgRandomColoredDots.php @@ -37,7 +37,7 @@ class RandomDotsSVGOutput extends QRMarkupSVG{ * * @inheritDoc */ - protected function collectModules(Closure $transform):array{ + protected function collectModules():array{ $paths = []; $dotColors = $this->options->dotColors; // avoid magic getter in long loops @@ -63,7 +63,7 @@ class RandomDotsSVGOutput extends QRMarkupSVG{ } // collect the modules per $M_TYPE - $module = $transform($x, $y, $M_TYPE, $M_TYPE_LAYER); + $module = $this->moduleTransform($x, $y, $M_TYPE, $M_TYPE_LAYER); if(!empty($module)){ $paths[$M_TYPE_LAYER][] = $module; diff --git a/examples/svgRoundQuietzone.php b/examples/svgRoundQuietzone.php index 649079480..a13938902 100644 --- a/examples/svgRoundQuietzone.php +++ b/examples/svgRoundQuietzone.php @@ -158,7 +158,7 @@ class RoundQuietzoneSVGoutput extends QRMarkupSVG{ ); } - protected function collectModules(Closure $transform):array{ + protected function collectModules():array{ $paths = []; $dotColors = $this->options->dotColors; // avoid magic getter in long loops @@ -184,7 +184,7 @@ class RoundQuietzoneSVGoutput extends QRMarkupSVG{ } // collect the modules per $M_TYPE - $module = $transform($x, $y, $M_TYPE, $M_TYPE_LAYER); + $module = $this->moduleTransform($x, $y, $M_TYPE, $M_TYPE_LAYER); if(!empty($module)){ $paths[$M_TYPE_LAYER][] = $module; diff --git a/examples/svgWithLogoAndCustomShapes.php b/examples/svgWithLogoAndCustomShapes.php index 99eb1676a..34487b3f3 100644 --- a/examples/svgWithLogoAndCustomShapes.php +++ b/examples/svgWithLogoAndCustomShapes.php @@ -56,7 +56,7 @@ class QRSvgWithLogoAndCustomShapes extends QRMarkupSVG{ * * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d */ - protected function module(int $x, int $y, int $M_TYPE):string{ + protected function moduleTransform(int $x, int $y, int $M_TYPE, int $M_TYPE_LAYER):string|null{ if( !$this->matrix->isDark($M_TYPE) @@ -64,7 +64,7 @@ class QRSvgWithLogoAndCustomShapes extends QRMarkupSVG{ || $this->matrix->checkType($x, $y, QRMatrix::M_FINDER) || $this->matrix->checkType($x, $y, QRMatrix::M_FINDER_DOT) ){ - return ''; + return null; } // return a heart shape (or any custom shape for that matter) diff --git a/src/Output/QREps.php b/src/Output/QREps.php index e5aed9785..5a6cf5885 100644 --- a/src/Output/QREps.php +++ b/src/Output/QREps.php @@ -143,7 +143,7 @@ class QREps extends QROutputAbstract{ * returns one or more EPS path blocks */ protected function paths():string{ - $paths = $this->collectModules($this->module(...)); + $paths = $this->collectModules(); $eps = []; foreach($paths as $M_TYPE => $path){ @@ -162,10 +162,10 @@ class QREps extends QROutputAbstract{ /** * Returns a path segment for a single module */ - protected function module(int $x, int $y, int $M_TYPE):string{ + protected function moduleTransform(int $x, int $y, int $M_TYPE, int $M_TYPE_LAYER):string|null{ if(!$this->drawLightModules && !$this->matrix->isDark($M_TYPE)){ - return ''; + return null; } $outputX = ($x * $this->scale); diff --git a/src/Output/QRMarkupSVG.php b/src/Output/QRMarkupSVG.php index d1bbfeff8..37b734d17 100644 --- a/src/Output/QRMarkupSVG.php +++ b/src/Output/QRMarkupSVG.php @@ -116,7 +116,7 @@ class QRMarkupSVG extends QRMarkup{ * returns one or more SVG elements */ protected function paths():string{ - $paths = $this->collectModules($this->module(...)); + $paths = $this->collectModules(); $svg = []; // create the path elements @@ -165,10 +165,10 @@ class QRMarkupSVG extends QRMarkup{ * * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d */ - protected function module(int $x, int $y, int $M_TYPE):string{ + protected function moduleTransform(int $x, int $y, int $M_TYPE, int $M_TYPE_LAYER):string|null{ if(!$this->drawLightModules && !$this->matrix->isDark($M_TYPE)){ - return ''; + return null; } if($this->drawCircularModules && !$this->matrix->checkTypeIn($x, $y, $this->keepAsSquare)){ diff --git a/src/Output/QROutputAbstract.php b/src/Output/QROutputAbstract.php index ece420c79..345750bfc 100644 --- a/src/Output/QROutputAbstract.php +++ b/src/Output/QROutputAbstract.php @@ -252,19 +252,14 @@ abstract class QROutputAbstract implements QROutputInterface{ } /** - * collects the modules per QRMatrix::M_* type and runs a $transform function on each module and - * returns an array with the transformed modules + * collects the modules per QRMatrix::M_* type, runs a transform method on each module and + * returns an array with the transformed modules. * - * The transform callback is called with the following parameters: - * - * $x - current column - * $y - current row - * $M_TYPE - field value - * $M_TYPE_LAYER - (possibly modified) field value that acts as layer id + * @see \chillerlan\QRCode\Output\QROutputAbstract::moduleTransform() * * @return array */ - protected function collectModules(Closure $transform):array{ + protected function collectModules():array{ $paths = []; // collect the modules for each type @@ -282,9 +277,9 @@ abstract class QROutputAbstract implements QROutputInterface{ } // collect the modules per $M_TYPE - $module = $transform($x, $y, $M_TYPE, $M_TYPE_LAYER); + $module = $this->moduleTransform($x, $y, $M_TYPE, $M_TYPE_LAYER); - if(!empty($module)){ + if($module !== null){ $paths[$M_TYPE_LAYER][] = $module; } } @@ -296,4 +291,21 @@ abstract class QROutputAbstract implements QROutputInterface{ return $paths; } + /** + * The transform callback for the module collector + * + * $x - current column + * $y - current row + * $M_TYPE - field value + * $M_TYPE_LAYER - (possibly modified) field value that acts as layer id ($paths array key) + * + * This method should return a value suitable for the current output class. + * It must return `null` for an empty value. + * + * @see \chillerlan\QRCode\Output\QROutputAbstract::collectModules() + */ + protected function moduleTransform(int $x, int $y, int $M_TYPE, int $M_TYPE_LAYER):mixed{ + return null; + } + }