Skip to content

Commit ff729b3

Browse files
committed
:octocat: QROutputAbstract::collectModules(): remove Closure parameter in favor of concrete method moduleTransform()
1 parent 5c948cf commit ff729b3

File tree

8 files changed

+40
-28
lines changed

8 files changed

+40
-28
lines changed

examples/svgMeltedModules.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected function path(string $path, int $M_TYPE):string{
3232
return sprintf('<path class="%s" d="%s"/>', $this->getCssClass($M_TYPE), $path);
3333
}
3434

35-
protected function collectModules(Closure $transform):array{
35+
protected function collectModules():array{
3636
$paths = [];
3737
$melt = $this->options->melt; // avoid magic getter in long loops
3838

@@ -57,7 +57,7 @@ protected function collectModules(Closure $transform):array{
5757
}
5858

5959
// collect the modules per $M_TYPE
60-
$module = $transform($x, $y, $M_TYPE, $M_TYPE_LAYER);
60+
$module = $this->moduleTransform($x, $y, $M_TYPE, $M_TYPE_LAYER);
6161

6262
if(!empty($module)){
6363
$paths[$M_TYPE_LAYER][] = $module;
@@ -71,7 +71,7 @@ protected function collectModules(Closure $transform):array{
7171
return $paths;
7272
}
7373

74-
protected function module(int $x, int $y, int $M_TYPE):string{
74+
protected function moduleTransform(int $x, int $y, int $M_TYPE, int $M_TYPE_LAYER):string{
7575
$bits = $this->matrix->checkNeighbours($x, $y, null);
7676
$check = fn(int $all, int $any = 0):bool => ($bits & ($all | (~$any & 0xff))) === $all;
7777

examples/svgModuleJitter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ protected function random():float{
4242
return (random_int(0, PHP_INT_MAX) / PHP_INT_MAX);
4343
}
4444

45-
protected function module(int $x, int $y, int $M_TYPE):string{
45+
protected function moduleTransform(int $x, int $y, int $M_TYPE, int $M_TYPE_LAYER):string|null{
4646

4747
// skip light modules
4848
if((!$this->options->drawLightModules && !$this->matrix->check($x, $y))){
49-
return '';
49+
return null;
5050
}
5151

5252
// early exit on pure square modules

examples/svgRandomColoredDots.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected function path(string $path, int $M_TYPE):string{
3737
*
3838
* @inheritDoc
3939
*/
40-
protected function collectModules(Closure $transform):array{
40+
protected function collectModules():array{
4141
$paths = [];
4242
$dotColors = $this->options->dotColors; // avoid magic getter in long loops
4343

@@ -63,7 +63,7 @@ protected function collectModules(Closure $transform):array{
6363
}
6464

6565
// collect the modules per $M_TYPE
66-
$module = $transform($x, $y, $M_TYPE, $M_TYPE_LAYER);
66+
$module = $this->moduleTransform($x, $y, $M_TYPE, $M_TYPE_LAYER);
6767

6868
if(!empty($module)){
6969
$paths[$M_TYPE_LAYER][] = $module;

examples/svgRoundQuietzone.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ protected function getLogo():string{
158158
);
159159
}
160160

161-
protected function collectModules(Closure $transform):array{
161+
protected function collectModules():array{
162162
$paths = [];
163163
$dotColors = $this->options->dotColors; // avoid magic getter in long loops
164164

@@ -184,7 +184,7 @@ protected function collectModules(Closure $transform):array{
184184
}
185185

186186
// collect the modules per $M_TYPE
187-
$module = $transform($x, $y, $M_TYPE, $M_TYPE_LAYER);
187+
$module = $this->moduleTransform($x, $y, $M_TYPE, $M_TYPE_LAYER);
188188

189189
if(!empty($module)){
190190
$paths[$M_TYPE_LAYER][] = $module;

examples/svgWithLogoAndCustomShapes.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ protected function path(string $path, int $M_TYPE):string{
5656
*
5757
* @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d
5858
*/
59-
protected function module(int $x, int $y, int $M_TYPE):string{
59+
protected function moduleTransform(int $x, int $y, int $M_TYPE, int $M_TYPE_LAYER):string|null{
6060

6161
if(
6262
!$this->matrix->isDark($M_TYPE)
6363
// we're skipping the finder patterns here
6464
|| $this->matrix->checkType($x, $y, QRMatrix::M_FINDER)
6565
|| $this->matrix->checkType($x, $y, QRMatrix::M_FINDER_DOT)
6666
){
67-
return '';
67+
return null;
6868
}
6969

7070
// return a heart shape (or any custom shape for that matter)

src/Output/QREps.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ protected function header():string{
143143
* returns one or more EPS path blocks
144144
*/
145145
protected function paths():string{
146-
$paths = $this->collectModules($this->module(...));
146+
$paths = $this->collectModules();
147147
$eps = [];
148148

149149
foreach($paths as $M_TYPE => $path){
@@ -162,10 +162,10 @@ protected function paths():string{
162162
/**
163163
* Returns a path segment for a single module
164164
*/
165-
protected function module(int $x, int $y, int $M_TYPE):string{
165+
protected function moduleTransform(int $x, int $y, int $M_TYPE, int $M_TYPE_LAYER):string|null{
166166

167167
if(!$this->drawLightModules && !$this->matrix->isDark($M_TYPE)){
168-
return '';
168+
return null;
169169
}
170170

171171
$outputX = ($x * $this->scale);

src/Output/QRMarkupSVG.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ protected function header():string{
116116
* returns one or more SVG <path> elements
117117
*/
118118
protected function paths():string{
119-
$paths = $this->collectModules($this->module(...));
119+
$paths = $this->collectModules();
120120
$svg = [];
121121

122122
// create the path elements
@@ -165,10 +165,10 @@ protected function path(string $path, int $M_TYPE):string{
165165
*
166166
* @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d
167167
*/
168-
protected function module(int $x, int $y, int $M_TYPE):string{
168+
protected function moduleTransform(int $x, int $y, int $M_TYPE, int $M_TYPE_LAYER):string|null{
169169

170170
if(!$this->drawLightModules && !$this->matrix->isDark($M_TYPE)){
171-
return '';
171+
return null;
172172
}
173173

174174
if($this->drawCircularModules && !$this->matrix->checkTypeIn($x, $y, $this->keepAsSquare)){

src/Output/QROutputAbstract.php

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -252,19 +252,14 @@ protected function saveToFile(string $data, string|null $file = null):void{
252252
}
253253

254254
/**
255-
* collects the modules per QRMatrix::M_* type and runs a $transform function on each module and
256-
* returns an array with the transformed modules
255+
* collects the modules per QRMatrix::M_* type, runs a transform method on each module and
256+
* returns an array with the transformed modules.
257257
*
258-
* The transform callback is called with the following parameters:
259-
*
260-
* $x - current column
261-
* $y - current row
262-
* $M_TYPE - field value
263-
* $M_TYPE_LAYER - (possibly modified) field value that acts as layer id
258+
* @see \chillerlan\QRCode\Output\QROutputAbstract::moduleTransform()
264259
*
265260
* @return array<int, mixed>
266261
*/
267-
protected function collectModules(Closure $transform):array{
262+
protected function collectModules():array{
268263
$paths = [];
269264

270265
// collect the modules for each type
@@ -282,9 +277,9 @@ protected function collectModules(Closure $transform):array{
282277
}
283278

284279
// collect the modules per $M_TYPE
285-
$module = $transform($x, $y, $M_TYPE, $M_TYPE_LAYER);
280+
$module = $this->moduleTransform($x, $y, $M_TYPE, $M_TYPE_LAYER);
286281

287-
if(!empty($module)){
282+
if($module !== null){
288283
$paths[$M_TYPE_LAYER][] = $module;
289284
}
290285
}
@@ -296,4 +291,21 @@ protected function collectModules(Closure $transform):array{
296291
return $paths;
297292
}
298293

294+
/**
295+
* The transform callback for the module collector
296+
*
297+
* $x - current column
298+
* $y - current row
299+
* $M_TYPE - field value
300+
* $M_TYPE_LAYER - (possibly modified) field value that acts as layer id ($paths array key)
301+
*
302+
* This method should return a value suitable for the current output class.
303+
* It must return `null` for an empty value.
304+
*
305+
* @see \chillerlan\QRCode\Output\QROutputAbstract::collectModules()
306+
*/
307+
protected function moduleTransform(int $x, int $y, int $M_TYPE, int $M_TYPE_LAYER):mixed{
308+
return null;
309+
}
310+
299311
}

0 commit comments

Comments
 (0)