Skip to content

Commit 14779f9

Browse files
committed
🚿 extract method QROutputAbstract::collectModules()
1 parent 3531b63 commit 14779f9

File tree

4 files changed

+39
-29
lines changed

4 files changed

+39
-29
lines changed

examples/svg.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
'drawCircularModules' => true,
3737
'circleRadius' => 0.4,
3838
// connect paths
39-
'svgConnectPaths' => true,
39+
'connectPaths' => true,
4040
// keep modules of thhese types as square
4141
'keepAsSquare' => [
4242
QRMatrix::M_FINDER|QRMatrix::IS_DARK,

src/Output/QRMarkup.php

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use chillerlan\QRCode\Data\QRMatrix;
1414
use chillerlan\QRCode\QRCode;
1515

16-
use function implode, is_string, ksort, sprintf, strip_tags, trim;
16+
use function implode, is_string, sprintf, strip_tags, trim;
1717

1818
/**
1919
* Converts the matrix into markup types: HTML, SVG, ...
@@ -150,30 +150,8 @@ protected function svgHeader():string{
150150
* @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/path
151151
*/
152152
protected function svgPaths():string{
153-
$paths = [];
154-
155-
// collect the modules for each type
156-
foreach($this->matrix->matrix() as $y => $row){
157-
foreach($row as $x => $M_TYPE){
158-
159-
if($this->options->svgConnectPaths && !$this->matrix->checkTypes($x, $y, $this->options->svgExcludeFromConnect)){
160-
// to connect paths we'll redeclare the $M_TYPE to data only
161-
$M_TYPE = QRMatrix::M_DATA;
162-
163-
if($this->matrix->check($x, $y)){
164-
$M_TYPE |= QRMatrix::IS_DARK;
165-
}
166-
}
167-
168-
// collect the modules per $M_TYPE
169-
$paths[$M_TYPE][] = $this->svgModule($x, $y);
170-
}
171-
}
172-
173-
// beautify output
174-
ksort($paths);
175-
176-
$svg = [];
153+
$paths = $this->collectModules(fn(int $x, int $y):string => $this->svgModule($x, $y));
154+
$svg = [];
177155

178156
// create the path elements
179157
foreach($paths as $M_TYPE => $path){

src/Output/QROutputAbstract.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
use chillerlan\QRCode\Data\QRMatrix;
1414
use chillerlan\Settings\SettingsContainerInterface;
15-
use function base64_encode, dirname, file_put_contents, is_writable, sprintf;
15+
use Closure;
16+
use function base64_encode, dirname, file_put_contents, is_writable, ksort, sprintf;
1617

1718
/**
1819
* common output abstract
@@ -130,4 +131,35 @@ protected function saveToFile(string $data, string $file):void{
130131
}
131132
}
132133

134+
/**
135+
* collects the modules per QRMatrix::M_* type and runs a $transform functio on each module and
136+
* returns an array with the transformed modules
137+
*/
138+
protected function collectModules(Closure $transform):array{
139+
$paths = [];
140+
141+
// collect the modules for each type
142+
foreach($this->matrix->matrix() as $y => $row){
143+
foreach($row as $x => $M_TYPE){
144+
145+
if($this->options->connectPaths && !$this->matrix->checkTypes($x, $y, $this->options->excludeFromConnect)){
146+
// to connect paths we'll redeclare the $M_TYPE to data only
147+
$M_TYPE = QRMatrix::M_DATA;
148+
149+
if($this->matrix->check($x, $y)){
150+
$M_TYPE |= QRMatrix::IS_DARK;
151+
}
152+
}
153+
154+
// collect the modules per $M_TYPE
155+
$paths[$M_TYPE][] = $transform($x, $y);
156+
}
157+
}
158+
159+
// beautify output
160+
ksort($paths);
161+
162+
return $paths;
163+
}
164+
133165
}

src/QROptionsTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,12 @@ trait QROptionsTrait{
153153
*
154154
* @see https://github.com/chillerlan/php-qrcode/issues/57
155155
*/
156-
protected bool $svgConnectPaths = false;
156+
protected bool $connectPaths = false;
157157

158158
/**
159159
* specify which paths/patterns to exclude from connecting if $svgConnectPaths is set to true
160160
*/
161-
protected array $svgExcludeFromConnect = [];
161+
protected array $excludeFromConnect = [];
162162

163163
/**
164164
* specify whether to draw the modules as filled circles

0 commit comments

Comments
 (0)