Просмотр исходного кода

:shower: extract method QROutputAbstract::collectModules()

smiley 3 лет назад
Родитель
Сommit
14779f9538
4 измененных файлов с 39 добавлено и 29 удалено
  1. 1 1
      examples/svg.php
  2. 3 25
      src/Output/QRMarkup.php
  3. 33 1
      src/Output/QROutputAbstract.php
  4. 2 2
      src/QROptionsTrait.php

+ 1 - 1
examples/svg.php

@@ -36,7 +36,7 @@ $options = new QROptions([
 	'drawCircularModules' => true,
 	'circleRadius'        => 0.4,
 	// connect paths
-	'svgConnectPaths'     => true,
+	'connectPaths'        => true,
 	// keep modules of thhese types as square
 	'keepAsSquare'        => [
 		QRMatrix::M_FINDER|QRMatrix::IS_DARK,

+ 3 - 25
src/Output/QRMarkup.php

@@ -13,7 +13,7 @@ namespace chillerlan\QRCode\Output;
 use chillerlan\QRCode\Data\QRMatrix;
 use chillerlan\QRCode\QRCode;
 
-use function implode, is_string, ksort, sprintf, strip_tags, trim;
+use function implode, is_string, sprintf, strip_tags, trim;
 
 /**
  * Converts the matrix into markup types: HTML, SVG, ...
@@ -150,30 +150,8 @@ class QRMarkup extends QROutputAbstract{
 	 * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/path
 	 */
 	protected function svgPaths():string{
-		$paths = [];
-
-		// collect the modules for each type
-		foreach($this->matrix->matrix() as $y => $row){
-			foreach($row as $x => $M_TYPE){
-
-				if($this->options->svgConnectPaths && !$this->matrix->checkTypes($x, $y, $this->options->svgExcludeFromConnect)){
-					// to connect paths we'll redeclare the $M_TYPE to data only
-					$M_TYPE = QRMatrix::M_DATA;
-
-					if($this->matrix->check($x, $y)){
-						$M_TYPE |= QRMatrix::IS_DARK;
-					}
-				}
-
-				// collect the modules per $M_TYPE
-				$paths[$M_TYPE][] = $this->svgModule($x, $y);
-			}
-		}
-
-		// beautify output
-		ksort($paths);
-
-		$svg = [];
+		$paths = $this->collectModules(fn(int $x, int $y):string => $this->svgModule($x, $y));
+		$svg   = [];
 
 		// create the path elements
 		foreach($paths as $M_TYPE => $path){

+ 33 - 1
src/Output/QROutputAbstract.php

@@ -12,7 +12,8 @@ namespace chillerlan\QRCode\Output;
 
 use chillerlan\QRCode\Data\QRMatrix;
 use chillerlan\Settings\SettingsContainerInterface;
-use function base64_encode, dirname, file_put_contents, is_writable, sprintf;
+use Closure;
+use function base64_encode, dirname, file_put_contents, is_writable, ksort, sprintf;
 
 /**
  * common output abstract
@@ -130,4 +131,35 @@ abstract class QROutputAbstract implements QROutputInterface{
 		}
 	}
 
+	/**
+	 * collects the modules per QRMatrix::M_* type and runs a $transform functio on each module and
+	 * returns an array with the transformed modules
+	 */
+	protected function collectModules(Closure $transform):array{
+		$paths = [];
+
+		// collect the modules for each type
+		foreach($this->matrix->matrix() as $y => $row){
+			foreach($row as $x => $M_TYPE){
+
+				if($this->options->connectPaths && !$this->matrix->checkTypes($x, $y, $this->options->excludeFromConnect)){
+					// to connect paths we'll redeclare the $M_TYPE to data only
+					$M_TYPE = QRMatrix::M_DATA;
+
+					if($this->matrix->check($x, $y)){
+						$M_TYPE |= QRMatrix::IS_DARK;
+					}
+				}
+
+				// collect the modules per $M_TYPE
+				$paths[$M_TYPE][] = $transform($x, $y);
+			}
+		}
+
+		// beautify output
+		ksort($paths);
+
+		return $paths;
+	}
+
 }

+ 2 - 2
src/QROptionsTrait.php

@@ -153,12 +153,12 @@ trait QROptionsTrait{
 	 *
 	 * @see https://github.com/chillerlan/php-qrcode/issues/57
 	 */
-	protected bool $svgConnectPaths = false;
+	protected bool $connectPaths = false;
 
 	/**
 	 * specify which paths/patterns to exclude from connecting if $svgConnectPaths is set to true
 	 */
-	protected array $svgExcludeFromConnect = [];
+	protected array $excludeFromConnect = [];
 
 	/**
 	 * specify whether to draw the modules as filled circles