🚿 extract method QROutputAbstract::collectModules()

This commit is contained in:
smiley
2022-05-11 19:32:53 +02:00
parent 3531b63085
commit 14779f9538
4 changed files with 39 additions and 29 deletions
+1 -1
View File
@@ -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
View File
@@ -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
View File
@@ -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
View File
@@ -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