From b3e8852d93276452d3bbc46ff8fccba1fb6818e4 Mon Sep 17 00:00:00 2001 From: Austin Drummond Date: Thu, 8 Sep 2022 00:15:27 -0400 Subject: [PATCH 1/5] added support for custom renderers --- src/Generator.php | 18 ++++++++++++++++-- src/Singleton.php | 13 +++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 src/Singleton.php diff --git a/src/Generator.php b/src/Generator.php index 4ada416..57be808 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -334,7 +334,8 @@ public function gradient($startRed, $startGreen, $startBlue, $endRed, $endGreen, */ public function eye(string $style): self { - if (! in_array($style, ['square', 'circle'])) { + $isSubclass = is_subclass_of($style, EyeInterface::class) && is_subclass_of($style, Singleton::class); + if (!in_array($style, ['square', 'circle']) && !$isSubclass) { throw new InvalidArgumentException("\$style must be square or circle. {$style} is not a valid eye style."); } @@ -353,7 +354,7 @@ public function eye(string $style): self */ public function style(string $style, float $size = 0.5): self { - if (! in_array($style, ['square', 'dot', 'round'])) { + if (!in_array($style, ['square', 'dot', 'round']) && !is_subclass_of($style, ModuleInterface::class)) { throw new InvalidArgumentException("\$style must be square, dot, or round. {$style} is not a valid."); } @@ -478,6 +479,11 @@ public function getFormatter(): ImageBackEndInterface */ public function getModule(): ModuleInterface { + if (is_subclass_of($this->style, ModuleInterface::class)) { + $style = $this->style; + return new $style($this->styleSize); + } + if ($this->style === 'dot') { return new DotsModule($this->styleSize); } @@ -496,6 +502,14 @@ public function getModule(): ModuleInterface */ public function getEye(): EyeInterface { + $isSubclass = is_subclass_of($this->eyeStyle, EyeInterface::class) && is_subclass_of($this->eyeStyle, Singleton::class); + if ($isSubclass) { + /** @var Singleton $style */ + $style = $this->eyeStyle; + + return $style::instance(); + } + if ($this->eyeStyle === 'square') { return SquareEye::instance(); } diff --git a/src/Singleton.php b/src/Singleton.php new file mode 100644 index 0000000..8faa340 --- /dev/null +++ b/src/Singleton.php @@ -0,0 +1,13 @@ + Date: Thu, 8 Sep 2022 00:26:03 -0400 Subject: [PATCH 2/5] style fixes --- src/Generator.php | 80 ++++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 39 deletions(-) diff --git a/src/Generator.php b/src/Generator.php index 57be808..4feefeb 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -147,7 +147,7 @@ class Generator * Creates a new datatype object and then generates a QrCode. * * @param $method - * @param array $arguments + * @param array $arguments */ public function __call($method, array $arguments) { @@ -160,8 +160,8 @@ public function __call($method, array $arguments) /** * Generates the QrCode. * - * @param string $text - * @param string|null $filename + * @param string $text + * @param string|null$filename * @return void|\Illuminate\Support\HtmlString|string * @throws WriterException * @throws InvalidArgumentException @@ -191,9 +191,9 @@ public function generate(string $text, string $filename = null) /** * Merges an image over the QrCode. * - * @param string $filepath - * @param float $percentage - * @param SimpleSoftwareIO\QrCode\boolean|bool $absolute + * @param string $filepath + * @param float $percentage + * @param SimpleSoftwareIO\QrCode\boolean|bool $absolute * @return Generator */ public function merge(string $filepath, float $percentage = .2, bool $absolute = false): self @@ -211,8 +211,8 @@ public function merge(string $filepath, float $percentage = .2, bool $absolute = /** * Merges an image string with the center of the QrCode. * - * @param string $content - * @param float $percentage + * @param string $content + * @param float $percentage * @return Generator */ public function mergeString(string $content, float $percentage = .2): self @@ -226,7 +226,7 @@ public function mergeString(string $content, float $percentage = .2): self /** * Sets the size of the QrCode. * - * @param int $pixels + * @param int $pixels * @return Generator */ public function size(int $pixels): self @@ -239,7 +239,7 @@ public function size(int $pixels): self /** * Sets the format of the QrCode. * - * @param string $format + * @param string $format * @return Generator * @throws InvalidArgumentException */ @@ -257,10 +257,10 @@ public function format(string $format): self /** * Sets the foreground color of the QrCode. * - * @param int $red - * @param int $green - * @param int $blue - * @param null|int $alpha + * @param int $red + * @param int $green + * @param int $blue + * @param null|int $alpha * @return Generator */ public function color(int $red, int $green, int $blue, ?int $alpha = null): self @@ -273,9 +273,9 @@ public function color(int $red, int $green, int $blue, ?int $alpha = null): self /** * Sets the background color of the QrCode. * - * @param int $red - * @param int $green - * @param int $blue + * @param int $red + * @param int $green + * @param int $blue * @param null|int $alpha * @return Generator */ @@ -289,13 +289,13 @@ public function backgroundColor(int $red, int $green, int $blue, ?int $alpha = n /** * Sets the eye color for the provided eye index. * - * @param int $eyeNumber - * @param int $innerRed - * @param int $innerGreen - * @param int $innerBlue - * @param int $outterRed - * @param int $outterGreen - * @param int $outterBlue + * @param int $eyeNumber + * @param int $innerRed + * @param int $innerGreen + * @param int $innerBlue + * @param int $outterRed + * @param int $outterGreen + * @param int $outterBlue * @return Generator * @throws InvalidArgumentException */ @@ -328,8 +328,9 @@ public function gradient($startRed, $startGreen, $startBlue, $endRed, $endGreen, /** * Sets the eye style. * - * @param string $style + * @param string $style * @return Generator + * * @throws InvalidArgumentException */ public function eye(string $style): self @@ -347,14 +348,15 @@ public function eye(string $style): self /** * Sets the style of the blocks for the QrCode. * - * @param string $style - * @param float $size + * @param string $style + * @param float $size * @return Generator + * * @throws InvalidArgumentException */ public function style(string $style, float $size = 0.5): self { - if (!in_array($style, ['square', 'dot', 'round']) && !is_subclass_of($style, ModuleInterface::class)) { + if (! in_array($style, ['square', 'dot', 'round']) && ! is_subclass_of($style, ModuleInterface::class)) { throw new InvalidArgumentException("\$style must be square, dot, or round. {$style} is not a valid."); } @@ -377,7 +379,7 @@ public function style(string $style, float $size = 0.5): self * SHIFT-JIS, WINDOWS-1250, WINDOWS-1251, WINDOWS-1252, WINDOWS-1256, * UTF-16BE, UTF-8, ASCII, GBK, EUC-KR. * - * @param string $encoding + * @param string $encoding * @return Generator */ public function encoding(string $encoding): self @@ -394,7 +396,7 @@ public function encoding(string $encoding): self * Q: 25% loss. * H: 30% loss. * - * @param string $errorCorrection + * @param string $errorCorrection * @return Generator */ public function errorCorrection(string $errorCorrection): self @@ -408,7 +410,7 @@ public function errorCorrection(string $errorCorrection): self /** * Sets the margin of the QrCode. * - * @param int $margin + * @param int $margin * @return Generator */ public function margin(int $margin): self @@ -421,7 +423,7 @@ public function margin(int $margin): self /** * Fetches the Writer. * - * @param ImageRenderer $renderer + * @param ImageRenderer $renderer * @return Writer */ public function getWriter(ImageRenderer $renderer): Writer @@ -466,10 +468,10 @@ public function getFormatter(): ImageBackEndInterface } if ($this->format === 'eps') { - return new EpsImageBackEnd; + return new EpsImageBackEnd(); } - return new SvgImageBackEnd; + return new SvgImageBackEnd(); } /** @@ -543,10 +545,10 @@ public function getFill(): Fill /** * Creates a RGB or Alpha channel color. - * @param int $red - * @param int $green - * @param int $blue - * @param null|int $alpha + * @param int $red + * @param int $green + * @param int $blue + * @param null|int $alpha * @return ColorInterface */ public function createColor(int $red, int $green, int $blue, ?int $alpha = null): ColorInterface @@ -561,7 +563,7 @@ public function createColor(int $red, int $green, int $blue, ?int $alpha = null) /** * Creates a new DataType class dynamically. * - * @param string $method + * @param string $method * @return DataTypeInterface */ protected function createClass(string $method): DataTypeInterface From 6de38f076e2be823cdd60fdfd5fa6d87e7ea1640 Mon Sep 17 00:00:00 2001 From: Austin Drummond Date: Thu, 8 Sep 2022 00:29:19 -0400 Subject: [PATCH 3/5] another style fix --- src/Generator.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Generator.php b/src/Generator.php index 4feefeb..a2ea9d2 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -163,6 +163,7 @@ public function __call($method, array $arguments) * @param string $text * @param string|null$filename * @return void|\Illuminate\Support\HtmlString|string + * * @throws WriterException * @throws InvalidArgumentException */ @@ -241,6 +242,7 @@ public function size(int $pixels): self * * @param string $format * @return Generator + * * @throws InvalidArgumentException */ public function format(string $format): self @@ -276,7 +278,7 @@ public function color(int $red, int $green, int $blue, ?int $alpha = null): self * @param int $red * @param int $green * @param int $blue - * @param null|int $alpha + * @param null|int $alpha * @return Generator */ public function backgroundColor(int $red, int $green, int $blue, ?int $alpha = null): self @@ -297,6 +299,7 @@ public function backgroundColor(int $red, int $green, int $blue, ?int $alpha = n * @param int $outterGreen * @param int $outterBlue * @return Generator + * * @throws InvalidArgumentException */ public function eyeColor(int $eyeNumber, int $innerRed, int $innerGreen, int $innerBlue, int $outterRed = 0, int $outterGreen = 0, int $outterBlue = 0): self @@ -330,13 +333,13 @@ public function gradient($startRed, $startGreen, $startBlue, $endRed, $endGreen, * * @param string $style * @return Generator - * + * * @throws InvalidArgumentException */ public function eye(string $style): self { $isSubclass = is_subclass_of($style, EyeInterface::class) && is_subclass_of($style, Singleton::class); - if (!in_array($style, ['square', 'circle']) && !$isSubclass) { + if (! in_array($style, ['square', 'circle']) && ! $isSubclass) { throw new InvalidArgumentException("\$style must be square or circle. {$style} is not a valid eye style."); } @@ -351,7 +354,7 @@ public function eye(string $style): self * @param string $style * @param float $size * @return Generator - * + * * @throws InvalidArgumentException */ public function style(string $style, float $size = 0.5): self From e08f349964cd052df8c8382a05a1d4062159bd81 Mon Sep 17 00:00:00 2001 From: Austin Drummond Date: Thu, 8 Sep 2022 00:29:52 -0400 Subject: [PATCH 4/5] another style fix --- src/Generator.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Generator.php b/src/Generator.php index a2ea9d2..6c86678 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -548,6 +548,7 @@ public function getFill(): Fill /** * Creates a RGB or Alpha channel color. + * * @param int $red * @param int $green * @param int $blue From 7af657e107bfee93fd9c48707f331f3deb7ff72e Mon Sep 17 00:00:00 2001 From: Austin Drummond Date: Thu, 8 Sep 2022 00:30:43 -0400 Subject: [PATCH 5/5] add new line --- src/Generator.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Generator.php b/src/Generator.php index 6c86678..8998c95 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -486,6 +486,7 @@ public function getModule(): ModuleInterface { if (is_subclass_of($this->style, ModuleInterface::class)) { $style = $this->style; + return new $style($this->styleSize); }