Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 62 additions & 41 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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
*/
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.");
}

Expand All @@ -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.");
}

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -465,10 +471,10 @@ public function getFormatter(): ImageBackEndInterface
}

if ($this->format === 'eps') {
return new EpsImageBackEnd;
return new EpsImageBackEnd();
}

return new SvgImageBackEnd;
return new SvgImageBackEnd();
}

/**
Expand All @@ -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);
}
Expand All @@ -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();
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions src/Singleton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace SimpleSoftwareIO\QrCode;

interface Singleton
{
/**
* Returns the instance of the class.
*
* @return self
*/
public static function instance();
}