:octocat: extract RGBArrayModuleValueTrait

This commit is contained in:
smiley
2024-05-06 23:18:27 +02:00
parent de3d321b00
commit 74dc3b2dbf
6 changed files with 117 additions and 106 deletions
+3 -57
View File
@@ -16,7 +16,7 @@ use chillerlan\QRCode\Data\QRMatrix;
use chillerlan\Settings\SettingsContainerInterface;
use FPDF;
use function array_values, class_exists, count, intval, is_array, is_numeric, max, min;
use function class_exists;
/**
* QRFpdf output module (requires fpdf)
@@ -25,6 +25,7 @@ use function array_values, class_exists, count, intval, is_array, is_numeric, ma
* @see http://www.fpdf.org/
*/
class QRFpdf extends QROutputAbstract{
use RGBArrayModuleValueTrait;
final public const MIME_TYPE = 'application/pdf';
@@ -42,7 +43,7 @@ class QRFpdf extends QROutputAbstract{
// @codeCoverageIgnoreStart
throw new QRCodeOutputException(
'The QRFpdf output requires FPDF (https://github.com/Setasign/FPDF)'.
' as dependency but the class "\\FPDF" couldn\'t be found.'
' as dependency but the class "\\FPDF" could not be found.'
);
// @codeCoverageIgnoreEnd
}
@@ -50,61 +51,6 @@ class QRFpdf extends QROutputAbstract{
parent::__construct($options, $matrix);
}
/**
* @inheritDoc
*/
public static function moduleValueIsValid(mixed $value):bool{
if(!is_array($value) || count($value) < 3){
return false;
}
// check the first 3 values of the array
foreach(array_values($value) as $i => $val){
if($i > 2){
break;
}
if(!is_numeric($val)){
return false;
}
}
return true;
}
/**
* @inheritDoc
* @throws \chillerlan\QRCode\Output\QRCodeOutputException
*/
protected function prepareModuleValue(mixed $value):array{
$values = [];
foreach(array_values($value) as $i => $val){
if($i > 2){
break;
}
$values[] = max(0, min(255, intval($val)));
}
if(count($values) !== 3){
throw new QRCodeOutputException('invalid color value');
}
return $values;
}
/**
* @inheritDoc
*/
protected function getDefaultModuleValue(bool $isDark):array{
return ($isDark) ? [0, 0, 0] : [255, 255, 255];
}
/**
* Initializes an FPDF instance
*/
+3 -27
View File
@@ -16,9 +16,9 @@ use chillerlan\QRCode\QROptions;
use chillerlan\QRCode\Data\QRMatrix;
use chillerlan\Settings\SettingsContainerInterface;
use ErrorException, GdImage, Throwable;
use function array_values, count, extension_loaded, imagecolorallocate, imagecolortransparent,
use function extension_loaded, imagecolorallocate, imagecolortransparent,
imagecreatetruecolor, imagedestroy, imagefilledellipse, imagefilledrectangle,
imagescale, imagetypes, intdiv, intval, is_array, is_numeric, max, min, ob_end_clean, ob_get_contents, ob_start,
imagescale, imagetypes, intdiv, intval, max, min, ob_end_clean, ob_get_contents, ob_start,
restore_error_handler, set_error_handler, sprintf;
use const IMG_AVIF, IMG_BMP, IMG_GIF, IMG_JPG, IMG_PNG, IMG_WEBP;
@@ -29,6 +29,7 @@ use const IMG_AVIF, IMG_BMP, IMG_GIF, IMG_JPG, IMG_PNG, IMG_WEBP;
* @see https://github.com/chillerlan/php-qrcode/issues/223
*/
abstract class QRGdImage extends QROutputAbstract{
use RGBArrayModuleValueTrait;
/**
* The GD image resource
@@ -105,31 +106,6 @@ abstract class QRGdImage extends QROutputAbstract{
}
/**
* @inheritDoc
*/
public static function moduleValueIsValid(mixed $value):bool{
if(!is_array($value) || count($value) < 3){
return false;
}
// check the first 3 values of the array
foreach(array_values($value) as $i => $val){
if($i > 2){
break;
}
if(!is_numeric($val)){
return false;
}
}
return true;
}
/**
* @inheritDoc
* @throws \chillerlan\QRCode\Output\QRCodeOutputException
+78
View File
@@ -0,0 +1,78 @@
<?php
/**
* RGBArrayModuleValueTrait.php
*
* @created 06.05.2024
* @author smiley <smiley@chillerlan.net>
* @copyright 2024 smiley
* @license MIT
*/
namespace chillerlan\QRCode\Output;
use function array_values, count, intval, is_array, is_numeric, max, min;
/**
* Module value checks for output classes that use RGB color arrays
*/
trait RGBArrayModuleValueTrait{
/**
* @implements \chillerlan\QRCode\Output\QROutputInterface::moduleValueIsValid()
* @inheritDoc
*/
public static function moduleValueIsValid(mixed $value):bool{
if(!is_array($value) || count($value) < 3){
return false;
}
// check the first 3 values of the array
foreach(array_values($value) as $i => $val){
if($i > 2){
break;
}
if(!is_numeric($val)){
return false;
}
}
return true;
}
/**
* @implements \chillerlan\QRCode\Output\QROutputAbstract::prepareModuleValue()
* @inheritDoc
* @throws \chillerlan\QRCode\Output\QRCodeOutputException
*/
protected function prepareModuleValue(mixed $value):array{
$values = [];
foreach(array_values($value) as $i => $val){
if($i > 2){
break;
}
$values[] = max(0, min(255, intval($val)));
}
if(count($values) !== 3){
throw new QRCodeOutputException('invalid color value');
}
return $values;
}
/**
* @implements \chillerlan\QRCode\Output\QROutputAbstract::getDefaultModuleValue()
* @inheritDoc
*/
protected function getDefaultModuleValue(bool $isDark):array{
return ($isDark) ? [0, 0, 0] : [255, 255, 255];
}
}