diff --git a/src/Generator.php b/src/Generator.php index 4ada416..8998c95 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,9 +160,10 @@ 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 +192,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 +212,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 +227,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,8 +240,9 @@ public function size(int $pixels): self /** * Sets the format of the QrCode. * - * @param string $format + * @param string $format * @return Generator + * * @throws InvalidArgumentException */ public function format(string $format): self @@ -257,10 +259,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,10 +275,10 @@ 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 null|int $alpha + * @param int $red + * @param int $green + * @param int $blue + * @param null|int $alpha * @return Generator */ public function backgroundColor(int $red, int $green, int $blue, ?int $alpha = null): self @@ -289,14 +291,15 @@ 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 */ public function eyeColor(int $eyeNumber, int $innerRed, int $innerGreen, int $innerBlue, int $outterRed = 0, int $outterGreen = 0, int $outterBlue = 0): self @@ -328,13 +331,15 @@ 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 { - 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."); } @@ -346,14 +351,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'])) { + 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."); } @@ -376,7 +382,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 @@ -393,7 +399,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 @@ -407,7 +413,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 @@ -420,7 +426,7 @@ public function margin(int $margin): self /** * Fetches the Writer. * - * @param ImageRenderer $renderer + * @param ImageRenderer $renderer * @return Writer */ public function getWriter(ImageRenderer $renderer): Writer @@ -465,10 +471,10 @@ public function getFormatter(): ImageBackEndInterface } if ($this->format === 'eps') { - return new EpsImageBackEnd; + return new EpsImageBackEnd(); } - return new SvgImageBackEnd; + return new SvgImageBackEnd(); } /** @@ -478,6 +484,12 @@ 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 +508,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(); } @@ -529,10 +549,11 @@ 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 @@ -547,7 +568,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 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 @@ +