From d0462ee466479029c945dff7060d0690e1eab40a Mon Sep 17 00:00:00 2001 From: smiley Date: Mon, 17 Nov 2025 18:38:10 +0100 Subject: [PATCH] :octocat: allow invocation with options iterable for the public facing classes --- examples/imageWithRoundedShapes.php | 9 ++++++++- src/Decoder/Decoder.php | 9 +++++++-- src/Output/QRFpdf.php | 2 +- src/Output/QRGdImage.php | 14 +++++++++----- src/Output/QRImagick.php | 2 +- src/Output/QRInterventionImage.php | 2 +- src/Output/QROutputAbstract.php | 11 ++++++++--- 7 files changed, 35 insertions(+), 14 deletions(-) diff --git a/examples/imageWithRoundedShapes.php b/examples/imageWithRoundedShapes.php index a5434a75e..4742ed2e4 100644 --- a/examples/imageWithRoundedShapes.php +++ b/examples/imageWithRoundedShapes.php @@ -28,8 +28,15 @@ require_once __DIR__.'/../vendor/autoload.php'; class QRGdRounded extends QRGdImagePNG{ - public function __construct(SettingsContainerInterface|QROptions $options, QRMatrix $matrix){ + public function __construct(SettingsContainerInterface|QROptions|iterable $options, QRMatrix $matrix){ + + if(is_iterable($options)){ + $options = new QROptions($options); + } + // enable the internal scaling for better rounding results at scale < 20 + // we need to do this before calling the parent constructor as these values are used there + $options->gdImageUseUpscale = true; $options->drawCircularModules = true; parent::__construct($options, $matrix); diff --git a/src/Decoder/Decoder.php b/src/Decoder/Decoder.php index a34e954ce..c3d876df6 100644 --- a/src/Decoder/Decoder.php +++ b/src/Decoder/Decoder.php @@ -18,7 +18,7 @@ use chillerlan\QRCode\Data\{AlphaNum, Byte, ECI, Hanzi, Kanji, Number}; use chillerlan\QRCode\Detector\Detector; use chillerlan\Settings\SettingsContainerInterface; use Throwable; -use function chr, str_replace; +use function chr, is_iterable, str_replace; /** * The main class which implements QR Code decoding -- as opposed to locating and extracting @@ -36,7 +36,12 @@ final class Decoder{ private BitBuffer $bitBuffer; private Detector $detector; - public function __construct(SettingsContainerInterface|QROptions $options = new QROptions){ + public function __construct(SettingsContainerInterface|QROptions|iterable $options = new QROptions){ + + if(is_iterable($options)){ + $options = new QROptions($options); + } + $this->options = $options; } diff --git a/src/Output/QRFpdf.php b/src/Output/QRFpdf.php index 23020fb5d..23438952a 100644 --- a/src/Output/QRFpdf.php +++ b/src/Output/QRFpdf.php @@ -39,7 +39,7 @@ class QRFpdf extends QROutputAbstract{ * * @throws \chillerlan\QRCode\Output\QRCodeOutputException */ - public function __construct(SettingsContainerInterface|QROptions $options, QRMatrix $matrix){ + public function __construct(SettingsContainerInterface|QROptions|iterable $options, QRMatrix $matrix){ if(!class_exists(FPDF::class)){ // @codeCoverageIgnoreStart diff --git a/src/Output/QRGdImage.php b/src/Output/QRGdImage.php index bf11f73d1..73495ca59 100644 --- a/src/Output/QRGdImage.php +++ b/src/Output/QRGdImage.php @@ -17,10 +17,9 @@ use chillerlan\QRCode\QROptions; use chillerlan\QRCode\Data\QRMatrix; use chillerlan\Settings\SettingsContainerInterface; use GdImage; -use function extension_loaded, imagecolorallocate, imagecolortransparent, - imagecreatetruecolor, imagefilledellipse, imagefilledrectangle, - imagescale, imagetypes, intdiv, intval, max, min, ob_end_clean, ob_get_contents, ob_start, - sprintf; +use function extension_loaded, imagecolorallocate, imagecolortransparent, imagecreatetruecolor, + imagefilledellipse, imagefilledrectangle, imagescale, imagetypes, intdiv, intval, is_iterable, + max, min, ob_end_clean, ob_get_contents, ob_start, sprintf; use const IMG_AVIF, IMG_BMP, IMG_GIF, IMG_JPG, IMG_PNG, IMG_WEBP; /** @@ -57,7 +56,12 @@ abstract class QRGdImage extends QROutputAbstract{ * @throws \chillerlan\QRCode\Output\QRCodeOutputException * @noinspection PhpMissingParentConstructorInspection */ - public function __construct(SettingsContainerInterface|QROptions $options, QRMatrix $matrix){ + public function __construct(SettingsContainerInterface|QROptions|iterable $options, QRMatrix $matrix){ + + if(is_iterable($options)){ + $options = new QROptions($options); + } + $this->options = $options; $this->matrix = $matrix; diff --git a/src/Output/QRImagick.php b/src/Output/QRImagick.php index 47f0f0e55..e7be0d1af 100644 --- a/src/Output/QRImagick.php +++ b/src/Output/QRImagick.php @@ -47,7 +47,7 @@ class QRImagick extends QROutputAbstract{ * * @throws \chillerlan\QRCode\Output\QRCodeOutputException */ - public function __construct(SettingsContainerInterface|QROptions $options, QRMatrix $matrix){ + public function __construct(SettingsContainerInterface|QROptions|iterable $options, QRMatrix $matrix){ foreach(['fileinfo', 'imagick'] as $ext){ if(!extension_loaded($ext)){ diff --git a/src/Output/QRInterventionImage.php b/src/Output/QRInterventionImage.php index b887d25b1..af8b6a294 100644 --- a/src/Output/QRInterventionImage.php +++ b/src/Output/QRInterventionImage.php @@ -48,7 +48,7 @@ class QRInterventionImage extends QROutputAbstract{ * * @throws \chillerlan\QRCode\Output\QRCodeOutputException */ - public function __construct(SettingsContainerInterface|QROptions $options, QRMatrix $matrix){ + public function __construct(SettingsContainerInterface|QROptions|iterable $options, QRMatrix $matrix){ if(!class_exists(ImageManager::class)){ // @codeCoverageIgnoreStart diff --git a/src/Output/QROutputAbstract.php b/src/Output/QROutputAbstract.php index dd1d8350f..7770bce50 100644 --- a/src/Output/QROutputAbstract.php +++ b/src/Output/QROutputAbstract.php @@ -17,7 +17,7 @@ use chillerlan\QRCode\QROptions; use chillerlan\QRCode\Data\QRMatrix; use chillerlan\Settings\SettingsContainerInterface; use finfo; -use function base64_encode, dirname, extension_loaded, file_put_contents, is_writable, ksort, sprintf; +use function base64_encode, dirname, extension_loaded, file_put_contents, is_iterable, is_writable, ksort, sprintf; use const FILEINFO_MIME_TYPE; /** @@ -81,7 +81,12 @@ abstract class QROutputAbstract implements QROutputInterface{ /** * QROutputAbstract constructor. */ - public function __construct(SettingsContainerInterface|QROptions $options, QRMatrix $matrix){ + public function __construct(SettingsContainerInterface|QROptions|iterable $options, QRMatrix $matrix){ + + if(is_iterable($options)){ + $options = new QROptions($options); + } + $this->options = $options; $this->matrix = $matrix; @@ -95,7 +100,7 @@ abstract class QROutputAbstract implements QROutputInterface{ } /** - * Creates copies of several QROptions values to avoid calling the magic getters + * Creates copies of several QROptions values to avoid calling the magic getters/property hooks * in long loops for a significant performance increase. * * These variables are usually used in the "module" methods and are called up to 31329 times (at version 40).