Skip to content

Commit d0462ee

Browse files
committed
:octocat: allow invocation with options iterable for the public facing classes
1 parent 490cc43 commit d0462ee

File tree

7 files changed

+35
-14
lines changed

7 files changed

+35
-14
lines changed

examples/imageWithRoundedShapes.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,15 @@
2828

2929
class QRGdRounded extends QRGdImagePNG{
3030

31-
public function __construct(SettingsContainerInterface|QROptions $options, QRMatrix $matrix){
31+
public function __construct(SettingsContainerInterface|QROptions|iterable $options, QRMatrix $matrix){
32+
33+
if(is_iterable($options)){
34+
$options = new QROptions($options);
35+
}
36+
3237
// enable the internal scaling for better rounding results at scale < 20
38+
// we need to do this before calling the parent constructor as these values are used there
39+
$options->gdImageUseUpscale = true;
3340
$options->drawCircularModules = true;
3441

3542
parent::__construct($options, $matrix);

src/Decoder/Decoder.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use chillerlan\QRCode\Detector\Detector;
1919
use chillerlan\Settings\SettingsContainerInterface;
2020
use Throwable;
21-
use function chr, str_replace;
21+
use function chr, is_iterable, str_replace;
2222

2323
/**
2424
* The main class which implements QR Code decoding -- as opposed to locating and extracting
@@ -36,7 +36,12 @@ final class Decoder{
3636
private BitBuffer $bitBuffer;
3737
private Detector $detector;
3838

39-
public function __construct(SettingsContainerInterface|QROptions $options = new QROptions){
39+
public function __construct(SettingsContainerInterface|QROptions|iterable $options = new QROptions){
40+
41+
if(is_iterable($options)){
42+
$options = new QROptions($options);
43+
}
44+
4045
$this->options = $options;
4146
}
4247

src/Output/QRFpdf.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class QRFpdf extends QROutputAbstract{
3939
*
4040
* @throws \chillerlan\QRCode\Output\QRCodeOutputException
4141
*/
42-
public function __construct(SettingsContainerInterface|QROptions $options, QRMatrix $matrix){
42+
public function __construct(SettingsContainerInterface|QROptions|iterable $options, QRMatrix $matrix){
4343

4444
if(!class_exists(FPDF::class)){
4545
// @codeCoverageIgnoreStart

src/Output/QRGdImage.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717
use chillerlan\QRCode\Data\QRMatrix;
1818
use chillerlan\Settings\SettingsContainerInterface;
1919
use GdImage;
20-
use function extension_loaded, imagecolorallocate, imagecolortransparent,
21-
imagecreatetruecolor, imagefilledellipse, imagefilledrectangle,
22-
imagescale, imagetypes, intdiv, intval, max, min, ob_end_clean, ob_get_contents, ob_start,
23-
sprintf;
20+
use function extension_loaded, imagecolorallocate, imagecolortransparent, imagecreatetruecolor,
21+
imagefilledellipse, imagefilledrectangle, imagescale, imagetypes, intdiv, intval, is_iterable,
22+
max, min, ob_end_clean, ob_get_contents, ob_start, sprintf;
2423
use const IMG_AVIF, IMG_BMP, IMG_GIF, IMG_JPG, IMG_PNG, IMG_WEBP;
2524

2625
/**
@@ -57,7 +56,12 @@ abstract class QRGdImage extends QROutputAbstract{
5756
* @throws \chillerlan\QRCode\Output\QRCodeOutputException
5857
* @noinspection PhpMissingParentConstructorInspection
5958
*/
60-
public function __construct(SettingsContainerInterface|QROptions $options, QRMatrix $matrix){
59+
public function __construct(SettingsContainerInterface|QROptions|iterable $options, QRMatrix $matrix){
60+
61+
if(is_iterable($options)){
62+
$options = new QROptions($options);
63+
}
64+
6165
$this->options = $options;
6266
$this->matrix = $matrix;
6367

src/Output/QRImagick.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class QRImagick extends QROutputAbstract{
4747
*
4848
* @throws \chillerlan\QRCode\Output\QRCodeOutputException
4949
*/
50-
public function __construct(SettingsContainerInterface|QROptions $options, QRMatrix $matrix){
50+
public function __construct(SettingsContainerInterface|QROptions|iterable $options, QRMatrix $matrix){
5151

5252
foreach(['fileinfo', 'imagick'] as $ext){
5353
if(!extension_loaded($ext)){

src/Output/QRInterventionImage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class QRInterventionImage extends QROutputAbstract{
4848
*
4949
* @throws \chillerlan\QRCode\Output\QRCodeOutputException
5050
*/
51-
public function __construct(SettingsContainerInterface|QROptions $options, QRMatrix $matrix){
51+
public function __construct(SettingsContainerInterface|QROptions|iterable $options, QRMatrix $matrix){
5252

5353
if(!class_exists(ImageManager::class)){
5454
// @codeCoverageIgnoreStart

src/Output/QROutputAbstract.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use chillerlan\QRCode\Data\QRMatrix;
1818
use chillerlan\Settings\SettingsContainerInterface;
1919
use finfo;
20-
use function base64_encode, dirname, extension_loaded, file_put_contents, is_writable, ksort, sprintf;
20+
use function base64_encode, dirname, extension_loaded, file_put_contents, is_iterable, is_writable, ksort, sprintf;
2121
use const FILEINFO_MIME_TYPE;
2222

2323
/**
@@ -81,7 +81,12 @@ abstract class QROutputAbstract implements QROutputInterface{
8181
/**
8282
* QROutputAbstract constructor.
8383
*/
84-
public function __construct(SettingsContainerInterface|QROptions $options, QRMatrix $matrix){
84+
public function __construct(SettingsContainerInterface|QROptions|iterable $options, QRMatrix $matrix){
85+
86+
if(is_iterable($options)){
87+
$options = new QROptions($options);
88+
}
89+
8590
$this->options = $options;
8691
$this->matrix = $matrix;
8792

@@ -95,7 +100,7 @@ public function __construct(SettingsContainerInterface|QROptions $options, QRMat
95100
}
96101

97102
/**
98-
* Creates copies of several QROptions values to avoid calling the magic getters
103+
* Creates copies of several QROptions values to avoid calling the magic getters/property hooks
99104
* in long loops for a significant performance increase.
100105
*
101106
* These variables are usually used in the "module" methods and are called up to 31329 times (at version 40).

0 commit comments

Comments
 (0)