mirror of
https://github.com/RobThree/TwoFactorAuth.git
synced 2026-08-18 22:23:26 +00:00
🐛 be more specific with hex colour handling
This commit is contained in:
@@ -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);
|
||||
});
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user