From 74dc3b2dbf9ae1f5c7b83f6ffaadb27c7f5cf437 Mon Sep 17 00:00:00 2001 From: smiley Date: Mon, 6 May 2024 23:18:27 +0200 Subject: [PATCH] :octocat: extract RGBArrayModuleValueTrait --- src/Output/QRFpdf.php | 60 +------------- src/Output/QRGdImage.php | 30 +------ src/Output/RGBArrayModuleValueTrait.php | 78 +++++++++++++++++++ tests/Output/QRFpdfTest.php | 12 +-- tests/Output/QRGdImageTestAbstract.php | 12 +-- .../RGBArrayModuleValueProviderTrait.php | 31 ++++++++ 6 files changed, 117 insertions(+), 106 deletions(-) create mode 100644 src/Output/RGBArrayModuleValueTrait.php create mode 100644 tests/Output/RGBArrayModuleValueProviderTrait.php diff --git a/src/Output/QRFpdf.php b/src/Output/QRFpdf.php index 0a099c386..35cf7e555 100644 --- a/src/Output/QRFpdf.php +++ b/src/Output/QRFpdf.php @@ -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 */ diff --git a/src/Output/QRGdImage.php b/src/Output/QRGdImage.php index 22a18ca50..e58cbae24 100644 --- a/src/Output/QRGdImage.php +++ b/src/Output/QRGdImage.php @@ -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 diff --git a/src/Output/RGBArrayModuleValueTrait.php b/src/Output/RGBArrayModuleValueTrait.php new file mode 100644 index 000000000..e7e73cd30 --- /dev/null +++ b/src/Output/RGBArrayModuleValueTrait.php @@ -0,0 +1,78 @@ + + * @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]; + } + +} diff --git a/tests/Output/QRFpdfTest.php b/tests/Output/QRFpdfTest.php index 216732bfe..03d5e7638 100644 --- a/tests/Output/QRFpdfTest.php +++ b/tests/Output/QRFpdfTest.php @@ -21,6 +21,7 @@ use function class_exists; * Tests the QRFpdf output module */ final class QRFpdfTest extends QROutputTestAbstract{ + use RGBArrayModuleValueProviderTrait; /** * @inheritDoc @@ -41,17 +42,6 @@ final class QRFpdfTest extends QROutputTestAbstract{ return new QRFpdf($options, $matrix); } - public static function moduleValueProvider():array{ - return [ - 'valid: int' => [[123, 123, 123], true], - 'valid: w/invalid extra element' => [[123, 123, 123, 'abc'], true], - 'valid: numeric string' => [['123', '123', '123'], true], - 'invalid: wrong type' => ['foo', false], - 'invalid: array too short' => [[1, 2], false], - 'invalid: contains non-number' => [[1, 'b', 3], false], - ]; - } - /** * @inheritDoc */ diff --git a/tests/Output/QRGdImageTestAbstract.php b/tests/Output/QRGdImageTestAbstract.php index 1e8bdc090..c79397e67 100644 --- a/tests/Output/QRGdImageTestAbstract.php +++ b/tests/Output/QRGdImageTestAbstract.php @@ -20,6 +20,7 @@ use function extension_loaded; * Tests the QRGdImage output module */ abstract class QRGdImageTestAbstract extends QROutputTestAbstract{ + use RGBArrayModuleValueProviderTrait; /** * @inheritDoc @@ -33,17 +34,6 @@ abstract class QRGdImageTestAbstract extends QROutputTestAbstract{ parent::setUp(); } - public static function moduleValueProvider():array{ - return [ - 'valid: int' => [[123, 123, 123], true], - 'valid: w/invalid extra element' => [[123, 123, 123, 'abc'], true], - 'valid: numeric string' => [['123', '123', '123'], true], - 'invalid: wrong type' => ['foo', false], - 'invalid: array too short' => [[1, 2], false], - 'invalid: contains non-number' => [[1, 'b', 3], false], - ]; - } - /** * @inheritDoc */ diff --git a/tests/Output/RGBArrayModuleValueProviderTrait.php b/tests/Output/RGBArrayModuleValueProviderTrait.php new file mode 100644 index 000000000..f22e49bac --- /dev/null +++ b/tests/Output/RGBArrayModuleValueProviderTrait.php @@ -0,0 +1,31 @@ + + * @copyright 2024 smiley + * @license MIT + */ + +namespace chillerlan\QRCodeTest\Output; + +/** + * A data provider for use in tests that include RGBArrayModuleValueTrait + * + * @see \chillerlan\QRCode\Output\RGBArrayModuleValueTrait + */ +trait RGBArrayModuleValueProviderTrait{ + + public static function moduleValueProvider():array{ + return [ + 'valid: int' => [[123, 123, 123], true], + 'valid: w/invalid extra element' => [[123, 123, 123, 'abc'], true], + 'valid: numeric string' => [['123', '123', '123'], true], + 'invalid: wrong type' => ['foo', false], + 'invalid: array too short' => [[1, 2], false], + 'invalid: contains non-number' => [[1, 'b', 3], false], + ]; + } + +}