From 2d1ec8d26519b5ad8154fc521b8736af2f2dc37b Mon Sep 17 00:00:00 2001 From: William Hall Date: Sun, 19 Dec 2021 19:44:10 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20be=20more=20specific=20with=20he?= =?UTF-8?q?x=20colour=20handling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/Providers/Qr/BaconQrCodeProvider.php | 13 ++++++++++--- testsDependency/BaconQRCodeTest.php | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/Providers/Qr/BaconQrCodeProvider.php b/lib/Providers/Qr/BaconQrCodeProvider.php index 4d5cf01..65fb977 100644 --- a/lib/Providers/Qr/BaconQrCodeProvider.php +++ b/lib/Providers/Qr/BaconQrCodeProvider.php @@ -125,12 +125,19 @@ class BaconQrCodeProvider implements IQRCodeProvider { if (is_string($colour) && $colour[0] == '#') { $hexToRGB = function ($input) { + // ensure input no longer has a # for more predictable division + // PHP 8.1 does not like implicitly casting a float to an int + $input = trim($input, '#'); + + if (strlen($input) != 3 && strlen($input) != 6) { + throw new \RuntimeException('Colour should be a 3 or 6 character value after the #'); + } + // split the array into three chunks - $split = str_split(trim($input, '#'), strlen($input) / 3); + $split = str_split($input, strlen($input) / 3); // cope with three character hex reference - // three characters plus a # = 4 - if (strlen($input) == 4) { + if (strlen($input) == 3) { array_walk($split, function (&$character) { $character = str_repeat($character, 2); }); diff --git a/testsDependency/BaconQRCodeTest.php b/testsDependency/BaconQRCodeTest.php index e30988a..9b426a7 100644 --- a/testsDependency/BaconQRCodeTest.php +++ b/testsDependency/BaconQRCodeTest.php @@ -34,4 +34,20 @@ class BaconQRCodeTest extends TestCase new BaconQrCodeProvider(1, '#000', 'not-a-colour'); } + + public function testBadTextColourHexRef() + { + $this->expectException(\RuntimeException::class); + + new BaconQrCodeProvider(1, '#AAAA', '#FFF'); + } + + public function testBadBackgroundColourHexRef() + { + $this->expectException(\RuntimeException::class); + + new BaconQrCodeProvider(1, '#000', '#AAAA'); + } + + }