瀏覽代碼

:octocat: QROutputAbstract::collectModules(): remove Closure parameter in favor of concrete method moduleTransform()

smiley 2 月之前
父節點
當前提交
ff729b3aba

+ 3 - 3
examples/svgMeltedModules.php

@@ -32,7 +32,7 @@ class MeltedSVGQRCodeOutput extends QRMarkupSVG{
 		return sprintf('<path class="%s" d="%s"/>', $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;
 

+ 2 - 2
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

+ 2 - 2
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;

+ 2 - 2
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;

+ 2 - 2
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)

+ 3 - 3
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);

+ 3 - 3
src/Output/QRMarkupSVG.php

@@ -116,7 +116,7 @@ class QRMarkupSVG extends QRMarkup{
 	 * returns one or more SVG <path> 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)){

+ 23 - 11
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<int, mixed>
 	 */
-	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;
+	}
+
 }