:octocat: clarify default module value setting

This commit is contained in:
codemasher
2021-12-11 17:52:22 +01:00
parent 709a0322c9
commit 3e14260144
7 changed files with 137 additions and 80 deletions
+29 -7
View File
@@ -14,20 +14,42 @@ use chillerlan\QRCode\Output\QROutputAbstract;
class MyCustomOutput extends QROutputAbstract{
protected function setModuleValues():void{
// TODO: Implement setModuleValues() method.
/**
* @inheritDoc
*/
protected function moduleValueIsValid($value):bool{
// TODO: Implement moduleValueIsValid() method. (abstract)
return false;
}
public function dump(string $file = null):string{
/**
* @inheritDoc
*/
protected function getModuleValue($value){
// TODO: Implement getModuleValue() method. (abstract)
return null;
}
/**
* @inheritDoc
*/
protected function getDefaultModuleValue(bool $isDark){
// TODO: Implement getDefaultModuleValue() method. (abstract)
return null;
}
/**
* @inheritDoc
*/
public function dump(string $file = null):string{
$output = '';
for($row = 0; $row < $this->moduleCount; $row++){
for($col = 0; $col < $this->moduleCount; $col++){
$output .= (int)$this->matrix->check($col, $row);
for($y = 0; $y < $this->moduleCount; $y++){
for($x = 0; $x < $this->moduleCount; $x++){
$output .= (int)$this->matrix->check($x, $y);
}
$output .= \PHP_EOL;
$output .= $this->options->eol;
}
return $output;
+15 -15
View File
@@ -47,22 +47,22 @@ class QRFpdf extends QROutputAbstract{
/**
* @inheritDoc
*/
protected function setModuleValues():void{
protected function moduleValueIsValid($value):bool{
return is_array($value) && count($value) >= 3;
}
foreach($this::DEFAULT_MODULE_VALUES as $M_TYPE => $defaultValue){
$v = $this->options->moduleValues[$M_TYPE] ?? null;
if(!is_array($v) || count($v) < 3){
$this->moduleValues[$M_TYPE] = $defaultValue
? [0, 0, 0]
: [255, 255, 255];
}
else{
$this->moduleValues[$M_TYPE] = array_values($v);
}
}
/**
* @inheritDoc
*/
protected function getModuleValue($value):array{
return array_values($value);
}
/**
* @inheritDoc
*/
protected function getDefaultModuleValue(bool $isDark):array{
return $isDark ? [0, 0, 0] : [255, 255, 255];
}
/**
@@ -84,7 +84,7 @@ class QRFpdf extends QROutputAbstract{
/** @var int $M_TYPE */
$color = $this->moduleValues[$M_TYPE];
if($prevColor === null || $prevColor !== $color){
if($prevColor !== $color){
/** @phan-suppress-next-line PhanParamTooFewUnpack */
$fpdf->SetFillColor(...$color);
$prevColor = $color;
+14 -14
View File
@@ -66,22 +66,22 @@ class QRImage extends QROutputAbstract{
/**
* @inheritDoc
*/
protected function setModuleValues():void{
protected function moduleValueIsValid($value):bool{
return is_array($value) && count($value) >= 3;
}
foreach($this::DEFAULT_MODULE_VALUES as $M_TYPE => $defaultValue){
$v = $this->options->moduleValues[$M_TYPE] ?? null;
if(!is_array($v) || count($v) < 3){
$this->moduleValues[$M_TYPE] = $defaultValue
? [0, 0, 0]
: [255, 255, 255];
}
else{
$this->moduleValues[$M_TYPE] = array_values($v);
}
}
/**
* @inheritDoc
*/
protected function getModuleValue($value):array{
return array_values($value);
}
/**
* @inheritDoc
*/
protected function getDefaultModuleValue(bool $isDark):array{
return $isDark ? [0, 0, 0] : [255, 255, 255];
}
/**
+16 -14
View File
@@ -46,20 +46,22 @@ class QRImagick extends QROutputAbstract{
/**
* @inheritDoc
*/
protected function setModuleValues():void{
protected function moduleValueIsValid($value):bool{
return is_string($value);
}
foreach($this::DEFAULT_MODULE_VALUES as $type => $defaultValue){
$v = $this->options->moduleValues[$type] ?? null;
/**
* @inheritDoc
*/
protected function getModuleValue($value):ImagickPixel{
return new ImagickPixel($value);
}
if(!is_string($v)){
$this->moduleValues[$type] = $defaultValue
? new ImagickPixel($this->options->markupDark)
: new ImagickPixel($this->options->markupLight);
}
else{
$this->moduleValues[$type] = new ImagickPixel($v);
}
}
/**
* @inheritDoc
*/
protected function getDefaultModuleValue(bool $isDark):ImagickPixel{
return new ImagickPixel($isDark ? $this->options->markupDark : $this->options->markupLight);
}
/**
@@ -68,7 +70,7 @@ class QRImagick extends QROutputAbstract{
* @return string|\Imagick
*/
public function dump(string $file = null){
$file ??= $this->options->cachefile;
$file ??= $this->options->cachefile;
$this->imagick = new Imagick;
$this->imagick->newImage(
@@ -129,7 +131,7 @@ class QRImagick extends QROutputAbstract{
$y * $this->scale,
($x + 1) * $this->scale,
($y + 1) * $this->scale
);
);
}
}
+14 -14
View File
@@ -25,22 +25,22 @@ class QRMarkup extends QROutputAbstract{
/**
* @inheritDoc
*/
protected function setModuleValues():void{
protected function moduleValueIsValid($value):bool{
return is_string($value);
}
foreach($this::DEFAULT_MODULE_VALUES as $M_TYPE => $defaultValue){
$v = $this->options->moduleValues[$M_TYPE] ?? null;
if(!is_string($v)){
$this->moduleValues[$M_TYPE] = $defaultValue
? $this->options->markupDark
: $this->options->markupLight;
}
else{
$this->moduleValues[$M_TYPE] = trim(strip_tags($v), " '\"\r\n\t");
}
}
/**
* @inheritDoc
*/
protected function getModuleValue($value):string{
return trim(strip_tags($value), " '\"\r\n\t");
}
/**
* @inheritDoc
*/
protected function getDefaultModuleValue(bool $isDark):string{
return $isDark ? $this->options->markupDark : $this->options->markupLight;
}
/**
+35 -2
View File
@@ -84,9 +84,42 @@ abstract class QROutputAbstract implements QROutputInterface{
}
/**
* Sets the initial module values (clean-up & defaults)
* Sets the initial module values
*/
abstract protected function setModuleValues():void;
protected function setModuleValues():void{
foreach($this::DEFAULT_MODULE_VALUES as $M_TYPE => $defaultValue){
$value = $this->options->moduleValues[$M_TYPE] ?? null;
$this->moduleValues[$M_TYPE] = $this->moduleValueIsValid($value)
? $this->getModuleValue($value)
: $this->getDefaultModuleValue($defaultValue);
}
}
/**
* Determines whether the given value is valid
*
* @param mixed|null $value
*/
abstract protected function moduleValueIsValid($value):bool;
/**
* Returns the final value for the given input (return value depends on the output module)
*
* @param mixed $value
*
* @return mixed
*/
abstract protected function getModuleValue($value);
/**
* Returns a defualt value for either dark or light modules (return value depends on the output module)
*
* @return mixed
*/
abstract protected function getDefaultModuleValue(bool $isDark);
/**
* Returns a base64 data URI for the given string and mime type
+14 -14
View File
@@ -27,22 +27,22 @@ class QRString extends QROutputAbstract{
/**
* @inheritDoc
*/
protected function setModuleValues():void{
protected function moduleValueIsValid($value):bool{
return is_string($value);
}
foreach($this::DEFAULT_MODULE_VALUES as $M_TYPE => $defaultValue){
$v = $this->options->moduleValues[$M_TYPE] ?? null;
if(!is_string($v)){
$this->moduleValues[$M_TYPE] = $defaultValue
? $this->options->textDark
: $this->options->textLight;
}
else{
$this->moduleValues[$M_TYPE] = $v;
}
}
/**
* @inheritDoc
*/
protected function getModuleValue($value):string{
return $value;
}
/**
* @inheritDoc
*/
protected function getDefaultModuleValue(bool $isDark):string{
return $isDark ? $this->options->textDark : $this->options->textLight;
}
/**