Skip to content

Commit 3e14260

Browse files
committed
:octocat: clarify default module value setting
1 parent 709a032 commit 3e14260

File tree

7 files changed

+137
-80
lines changed

7 files changed

+137
-80
lines changed

examples/MyCustomOutput.php

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,42 @@
1414

1515
class MyCustomOutput extends QROutputAbstract{
1616

17-
protected function setModuleValues():void{
18-
// TODO: Implement setModuleValues() method.
17+
/**
18+
* @inheritDoc
19+
*/
20+
protected function moduleValueIsValid($value):bool{
21+
// TODO: Implement moduleValueIsValid() method. (abstract)
22+
return false;
1923
}
2024

21-
public function dump(string $file = null):string{
25+
/**
26+
* @inheritDoc
27+
*/
28+
protected function getModuleValue($value){
29+
// TODO: Implement getModuleValue() method. (abstract)
30+
return null;
31+
}
2232

33+
/**
34+
* @inheritDoc
35+
*/
36+
protected function getDefaultModuleValue(bool $isDark){
37+
// TODO: Implement getDefaultModuleValue() method. (abstract)
38+
return null;
39+
}
40+
41+
/**
42+
* @inheritDoc
43+
*/
44+
public function dump(string $file = null):string{
2345
$output = '';
2446

25-
for($row = 0; $row < $this->moduleCount; $row++){
26-
for($col = 0; $col < $this->moduleCount; $col++){
27-
$output .= (int)$this->matrix->check($col, $row);
47+
for($y = 0; $y < $this->moduleCount; $y++){
48+
for($x = 0; $x < $this->moduleCount; $x++){
49+
$output .= (int)$this->matrix->check($x, $y);
2850
}
2951

30-
$output .= \PHP_EOL;
52+
$output .= $this->options->eol;
3153
}
3254

3355
return $output;

src/Output/QRFpdf.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,22 @@ public function __construct(SettingsContainerInterface $options, QRMatrix $matri
4747
/**
4848
* @inheritDoc
4949
*/
50-
protected function setModuleValues():void{
51-
52-
foreach($this::DEFAULT_MODULE_VALUES as $M_TYPE => $defaultValue){
53-
$v = $this->options->moduleValues[$M_TYPE] ?? null;
54-
55-
if(!is_array($v) || count($v) < 3){
56-
$this->moduleValues[$M_TYPE] = $defaultValue
57-
? [0, 0, 0]
58-
: [255, 255, 255];
59-
}
60-
else{
61-
$this->moduleValues[$M_TYPE] = array_values($v);
62-
}
50+
protected function moduleValueIsValid($value):bool{
51+
return is_array($value) && count($value) >= 3;
52+
}
6353

64-
}
54+
/**
55+
* @inheritDoc
56+
*/
57+
protected function getModuleValue($value):array{
58+
return array_values($value);
59+
}
6560

61+
/**
62+
* @inheritDoc
63+
*/
64+
protected function getDefaultModuleValue(bool $isDark):array{
65+
return $isDark ? [0, 0, 0] : [255, 255, 255];
6666
}
6767

6868
/**
@@ -84,7 +84,7 @@ public function dump(string $file = null){
8484
/** @var int $M_TYPE */
8585
$color = $this->moduleValues[$M_TYPE];
8686

87-
if($prevColor === null || $prevColor !== $color){
87+
if($prevColor !== $color){
8888
/** @phan-suppress-next-line PhanParamTooFewUnpack */
8989
$fpdf->SetFillColor(...$color);
9090
$prevColor = $color;

src/Output/QRImage.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,22 @@ public function __construct(SettingsContainerInterface $options, QRMatrix $matri
6666
/**
6767
* @inheritDoc
6868
*/
69-
protected function setModuleValues():void{
70-
71-
foreach($this::DEFAULT_MODULE_VALUES as $M_TYPE => $defaultValue){
72-
$v = $this->options->moduleValues[$M_TYPE] ?? null;
73-
74-
if(!is_array($v) || count($v) < 3){
75-
$this->moduleValues[$M_TYPE] = $defaultValue
76-
? [0, 0, 0]
77-
: [255, 255, 255];
78-
}
79-
else{
80-
$this->moduleValues[$M_TYPE] = array_values($v);
81-
}
69+
protected function moduleValueIsValid($value):bool{
70+
return is_array($value) && count($value) >= 3;
71+
}
8272

83-
}
73+
/**
74+
* @inheritDoc
75+
*/
76+
protected function getModuleValue($value):array{
77+
return array_values($value);
78+
}
8479

80+
/**
81+
* @inheritDoc
82+
*/
83+
protected function getDefaultModuleValue(bool $isDark):array{
84+
return $isDark ? [0, 0, 0] : [255, 255, 255];
8585
}
8686

8787
/**

src/Output/QRImagick.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,22 @@ public function __construct(SettingsContainerInterface $options, QRMatrix $matri
4646
/**
4747
* @inheritDoc
4848
*/
49-
protected function setModuleValues():void{
49+
protected function moduleValueIsValid($value):bool{
50+
return is_string($value);
51+
}
5052

51-
foreach($this::DEFAULT_MODULE_VALUES as $type => $defaultValue){
52-
$v = $this->options->moduleValues[$type] ?? null;
53+
/**
54+
* @inheritDoc
55+
*/
56+
protected function getModuleValue($value):ImagickPixel{
57+
return new ImagickPixel($value);
58+
}
5359

54-
if(!is_string($v)){
55-
$this->moduleValues[$type] = $defaultValue
56-
? new ImagickPixel($this->options->markupDark)
57-
: new ImagickPixel($this->options->markupLight);
58-
}
59-
else{
60-
$this->moduleValues[$type] = new ImagickPixel($v);
61-
}
62-
}
60+
/**
61+
* @inheritDoc
62+
*/
63+
protected function getDefaultModuleValue(bool $isDark):ImagickPixel{
64+
return new ImagickPixel($isDark ? $this->options->markupDark : $this->options->markupLight);
6365
}
6466

6567
/**
@@ -68,7 +70,7 @@ protected function setModuleValues():void{
6870
* @return string|\Imagick
6971
*/
7072
public function dump(string $file = null){
71-
$file ??= $this->options->cachefile;
73+
$file ??= $this->options->cachefile;
7274
$this->imagick = new Imagick;
7375

7476
$this->imagick->newImage(
@@ -129,7 +131,7 @@ protected function setPixel(int $x, int $y, int $M_TYPE):void{
129131
$y * $this->scale,
130132
($x + 1) * $this->scale,
131133
($y + 1) * $this->scale
132-
);
134+
);
133135
}
134136

135137
}

src/Output/QRMarkup.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,22 @@ class QRMarkup extends QROutputAbstract{
2525
/**
2626
* @inheritDoc
2727
*/
28-
protected function setModuleValues():void{
29-
30-
foreach($this::DEFAULT_MODULE_VALUES as $M_TYPE => $defaultValue){
31-
$v = $this->options->moduleValues[$M_TYPE] ?? null;
32-
33-
if(!is_string($v)){
34-
$this->moduleValues[$M_TYPE] = $defaultValue
35-
? $this->options->markupDark
36-
: $this->options->markupLight;
37-
}
38-
else{
39-
$this->moduleValues[$M_TYPE] = trim(strip_tags($v), " '\"\r\n\t");
40-
}
28+
protected function moduleValueIsValid($value):bool{
29+
return is_string($value);
30+
}
4131

42-
}
32+
/**
33+
* @inheritDoc
34+
*/
35+
protected function getModuleValue($value):string{
36+
return trim(strip_tags($value), " '\"\r\n\t");
37+
}
4338

39+
/**
40+
* @inheritDoc
41+
*/
42+
protected function getDefaultModuleValue(bool $isDark):string{
43+
return $isDark ? $this->options->markupDark : $this->options->markupLight;
4444
}
4545

4646
/**

src/Output/QROutputAbstract.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,42 @@ public function __construct(SettingsContainerInterface $options, QRMatrix $matri
8484
}
8585

8686
/**
87-
* Sets the initial module values (clean-up & defaults)
87+
* Sets the initial module values
8888
*/
89-
abstract protected function setModuleValues():void;
89+
protected function setModuleValues():void{
90+
91+
foreach($this::DEFAULT_MODULE_VALUES as $M_TYPE => $defaultValue){
92+
$value = $this->options->moduleValues[$M_TYPE] ?? null;
93+
94+
$this->moduleValues[$M_TYPE] = $this->moduleValueIsValid($value)
95+
? $this->getModuleValue($value)
96+
: $this->getDefaultModuleValue($defaultValue);
97+
}
98+
99+
}
100+
101+
/**
102+
* Determines whether the given value is valid
103+
*
104+
* @param mixed|null $value
105+
*/
106+
abstract protected function moduleValueIsValid($value):bool;
107+
108+
/**
109+
* Returns the final value for the given input (return value depends on the output module)
110+
*
111+
* @param mixed $value
112+
*
113+
* @return mixed
114+
*/
115+
abstract protected function getModuleValue($value);
116+
117+
/**
118+
* Returns a defualt value for either dark or light modules (return value depends on the output module)
119+
*
120+
* @return mixed
121+
*/
122+
abstract protected function getDefaultModuleValue(bool $isDark);
90123

91124
/**
92125
* Returns a base64 data URI for the given string and mime type

src/Output/QRString.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,22 @@ class QRString extends QROutputAbstract{
2727
/**
2828
* @inheritDoc
2929
*/
30-
protected function setModuleValues():void{
31-
32-
foreach($this::DEFAULT_MODULE_VALUES as $M_TYPE => $defaultValue){
33-
$v = $this->options->moduleValues[$M_TYPE] ?? null;
34-
35-
if(!is_string($v)){
36-
$this->moduleValues[$M_TYPE] = $defaultValue
37-
? $this->options->textDark
38-
: $this->options->textLight;
39-
}
40-
else{
41-
$this->moduleValues[$M_TYPE] = $v;
42-
}
30+
protected function moduleValueIsValid($value):bool{
31+
return is_string($value);
32+
}
4333

44-
}
34+
/**
35+
* @inheritDoc
36+
*/
37+
protected function getModuleValue($value):string{
38+
return $value;
39+
}
4540

41+
/**
42+
* @inheritDoc
43+
*/
44+
protected function getDefaultModuleValue(bool $isDark):string{
45+
return $isDark ? $this->options->textDark : $this->options->textLight;
4646
}
4747

4848
/**

0 commit comments

Comments
 (0)