From 147e4a29d5b0aea49e6afc6cb4f90350d8830b13 Mon Sep 17 00:00:00 2001 From: IggsGrey Date: Fri, 9 Aug 2024 22:06:48 +0000 Subject: [PATCH 1/5] Update bacon/bacon-qr-code package version to 3.0 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 8594a91..073aa31 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ "require": { "php": ">=7.2|^8.0", "ext-gd": "*", - "bacon/bacon-qr-code": "^2.0" + "bacon/bacon-qr-code": "^3.0" }, "require-dev": { "mockery/mockery": "~1", From 5c48086dc35a1b3ba0efc9ea5a015fbf7ca81c5d Mon Sep 17 00:00:00 2001 From: IggsGrey Date: Fri, 9 Aug 2024 22:14:00 +0000 Subject: [PATCH 2/5] Add RoundedSquare custom eye --- src/CustomEyes/RoundedSquareEye.php | 87 +++++++++++++++++++++++++++++ src/Generator.php | 11 +++- 2 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 src/CustomEyes/RoundedSquareEye.php diff --git a/src/CustomEyes/RoundedSquareEye.php b/src/CustomEyes/RoundedSquareEye.php new file mode 100644 index 0000000..687f1e0 --- /dev/null +++ b/src/CustomEyes/RoundedSquareEye.php @@ -0,0 +1,87 @@ +move(0, $outerSize) + ->line($outerMidPoint, $outerSize) + ->curve($outerMidPoint, $outerSize, $outerSize, $outerSize, $outerSize, $outerMidPoint) + ->line($outerSize, 0) + ->line($outerSize, -$outerMidPoint) + ->curve($outerSize, -$outerMidPoint, $outerSize, -$outerSize, $outerMidPoint, -$outerSize) + ->line(0, -$outerSize) + ->line(-$outerMidPoint, -$outerSize) + ->curve(-$outerMidPoint, -$outerSize, -$outerSize, -$outerSize, -$outerSize, -$outerMidPoint) + ->line(-$outerSize, 0) + ->line(-$outerSize, $outerMidPoint) + ->curve(-$outerSize, $outerMidPoint, -$outerSize, $outerSize, -$outerMidPoint, $outerSize) + ->line(0, $outerSize) + ->close() + + ->move(0, $innerSize) + ->line($innerMidPoint, $innerSize) + ->curve($innerMidPoint, $innerSize, $innerSize, $innerSize, $innerSize, $innerMidPoint) + ->line($innerSize, 0) + ->line($innerSize, -$innerMidPoint) + ->curve($innerSize, -$innerMidPoint, $innerSize, -$innerSize, $innerMidPoint, -$innerSize) + ->line(0, -$innerSize) + ->line(-$innerMidPoint, -$innerSize) + ->curve(-$innerMidPoint, -$innerSize, -$innerSize, -$innerSize, -$innerSize, -$innerMidPoint) + ->line(-$innerSize, 0) + ->line(-$innerSize, $innerMidPoint) + ->curve(-$innerSize, $innerMidPoint, -$innerSize, $innerSize, -$innerMidPoint, $innerSize) + ->line(0, $innerSize) + ->close(); + } + + + public function getInternalPath() : Path + { + $size = 1.5; + $midPoint = 0.75; + + return (new Path()) + ->move(0, $size) + ->line($midPoint, $size) + ->curve($midPoint, $size, $size, $size, $size, $midPoint) + ->line($size, 0) + ->line($size, -$midPoint) + ->curve($size, -$midPoint, $size, -$size, $midPoint, -$size) + ->line(0, -$size) + ->line(-$midPoint, -$size) + ->curve(-$midPoint, -$size, -$size, -$size, -$size, -$midPoint) + ->line(-$size, 0) + ->line(-$size, $midPoint) + ->curve(-$size, $midPoint, -$size, $size, -$midPoint, $size) + ->line(0, $size) + ->close(); + } +} diff --git a/src/Generator.php b/src/Generator.php index 4ada416..36f4645 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -12,6 +12,7 @@ use BaconQrCode\Renderer\Eye\ModuleEye; use BaconQrCode\Renderer\Eye\SimpleCircleEye; use BaconQrCode\Renderer\Eye\SquareEye; +use SimpleSoftwareIO\QrCode\CustomEyes\RoundedSquareEye; use BaconQrCode\Renderer\Image\EpsImageBackEnd; use BaconQrCode\Renderer\Image\ImageBackEndInterface; use BaconQrCode\Renderer\Image\ImagickImageBackEnd; @@ -95,7 +96,7 @@ class Generator /** * The style to apply to the eye. - * Possible values are circle and square. + * Possible values are circle, square and rounded. * * @var string|null */ @@ -334,8 +335,8 @@ public function gradient($startRed, $startGreen, $startBlue, $endRed, $endGreen, */ public function eye(string $style): self { - if (! in_array($style, ['square', 'circle'])) { - throw new InvalidArgumentException("\$style must be square or circle. {$style} is not a valid eye style."); + if (! in_array($style, ['square', 'circle', 'rounded'])) { + throw new InvalidArgumentException("\$style must be square, rounded or circle. {$style} is not a valid eye style."); } $this->eyeStyle = $style; @@ -504,6 +505,10 @@ public function getEye(): EyeInterface return SimpleCircleEye::instance(); } + if ($this->eyeStyle === 'rounded') { + return RoundedSquareEye::instance(); + } + return new ModuleEye($this->getModule()); } From caba0762bcccf63078c5129094b2f45f9a013152 Mon Sep 17 00:00:00 2001 From: IggsGrey Date: Fri, 9 Aug 2024 22:25:18 +0000 Subject: [PATCH 3/5] Add ability to customise external eye + new externalEyeStyle (PointyEye) --- src/Generator.php | 60 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 5 deletions(-) diff --git a/src/Generator.php b/src/Generator.php index 36f4645..a492b25 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -12,6 +12,8 @@ use BaconQrCode\Renderer\Eye\ModuleEye; use BaconQrCode\Renderer\Eye\SimpleCircleEye; use BaconQrCode\Renderer\Eye\SquareEye; +use BaconQrCode\Renderer\Eye\CompositeEye; +use BaconQrCode\Renderer\Eye\PointyEye; use SimpleSoftwareIO\QrCode\CustomEyes\RoundedSquareEye; use BaconQrCode\Renderer\Image\EpsImageBackEnd; use BaconQrCode\Renderer\Image\ImageBackEndInterface; @@ -95,13 +97,21 @@ class Generator protected $styleSize = null; /** - * The style to apply to the eye. + * The style to apply to the internal eye. * Possible values are circle, square and rounded. * * @var string|null */ protected $eyeStyle = null; + /** + * The style to apply to the external eye. + * Possible values are circle, square, pointy and rounded. + * + * @var string|null + */ + protected $externalEyeStyle = null; + /** * The foreground color of the QrCode. * @@ -344,6 +354,24 @@ public function eye(string $style): self return $this; } + /** + * Sets the external eye style. + * + * @param string $style + * @return Generator + * @throws InvalidArgumentException + */ + public function externalEye(string $style): self + { + if (! in_array($style, ['square', 'circle', 'pointy', 'rounded'])) { + throw new InvalidArgumentException("\$style must be square, rounded, pointy or circle. {$style} is not a valid eye style."); + } + + $this->externalEyeStyle = $style; + + return $this; + } + /** * Sets the style of the blocks for the QrCode. * @@ -497,19 +525,41 @@ public function getModule(): ModuleInterface */ public function getEye(): EyeInterface { + // defaults + $internalEye = new ModuleEye($this->getModule()); + $externalEye = new ModuleEye($this->getModule()); + + // external eye + if ($this->externalEyeStyle === 'square') { + $externalEye = SquareEye::instance(); + } + + if ($this->externalEyeStyle === 'circle') { + $externalEye = SimpleCircleEye::instance(); + } + + if ($this->externalEyeStyle === 'pointy') { + $externalEye = PointyEye::instance(); + } + + if ($this->externalEyeStyle === 'rounded') { + $externalEye = RoundedSquareEye::instance(); + } + + // internal eye if ($this->eyeStyle === 'square') { - return SquareEye::instance(); + $internalEye = SquareEye::instance(); } if ($this->eyeStyle === 'circle') { - return SimpleCircleEye::instance(); + $internalEye = SimpleCircleEye::instance(); } if ($this->eyeStyle === 'rounded') { - return RoundedSquareEye::instance(); + $internalEye = RoundedSquareEye::instance(); } - return new ModuleEye($this->getModule()); + return new CompositeEye($externalEye, $internalEye); } /** From 2bc9006cab2ebc3bb8e392794ae66f94c180790f Mon Sep 17 00:00:00 2001 From: IggsGrey Date: Fri, 9 Aug 2024 22:27:42 +0000 Subject: [PATCH 4/5] Update tests --- tests/GeneratorTest.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/tests/GeneratorTest.php b/tests/GeneratorTest.php index d0d75be..1eac682 100644 --- a/tests/GeneratorTest.php +++ b/tests/GeneratorTest.php @@ -1,5 +1,6 @@ assertInstanceOf(Gradient::class, $generator->getFill()->getForegroundGradient()); } - public function test_eye_style_is_set() + public function test_invalid_eye_throws_an_exception() { - $generator = (new Generator)->eye('circle'); - $this->assertInstanceOf(SimpleCircleEye::class, $generator->getEye()); - - $generator = (new Generator)->eye('square'); - $this->assertInstanceOf(SquareEye::class, $generator->getEye()); + $this->expectException(InvalidArgumentException::class); + (new Generator)->eye('foo'); } - public function test_invalid_eye_throws_an_exception() + public function test_invalid_external_eye_throws_an_exception() { $this->expectException(InvalidArgumentException::class); - (new Generator)->eye('foo'); + (new Generator)->externalEye('foo'); } public function test_style_is_set() From 4fb47946338037cdba0da6544f8d804502542a7b Mon Sep 17 00:00:00 2001 From: IggsGrey Date: Fri, 9 Aug 2024 22:38:58 +0000 Subject: [PATCH 5/5] Clean code fixes after ci check --- src/CustomEyes/RoundedSquareEye.php | 10 +++++----- src/Generator.php | 6 +++--- tests/GeneratorTest.php | 4 ---- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/CustomEyes/RoundedSquareEye.php b/src/CustomEyes/RoundedSquareEye.php index 687f1e0..363cf4f 100644 --- a/src/CustomEyes/RoundedSquareEye.php +++ b/src/CustomEyes/RoundedSquareEye.php @@ -1,10 +1,10 @@