Skip to content

Commit 37a374e

Browse files
authored
fix sanitizeHex regex check to check $color (#52)
1 parent c75f7e4 commit 37a374e

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

src/Mexitek/PHPColors/Color.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ private static function sanitizeHex(string $hex): string
528528
$color = str_replace("#", "", $hex);
529529

530530
// Validate hex string
531-
if (!preg_match('/^[a-fA-F0-9]+$/', $hex)) {
531+
if (!preg_match('/^[a-fA-F0-9]+$/', $color)) {
532532
throw new Exception("HEX color does not match format");
533533
}
534534

tests/colorInput.phpt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
require __DIR__ . '/bootstrap.php';
4+
5+
use Mexitek\PHPColors\Color;
6+
use Tester\Assert;
7+
8+
// Test that a hex starting with '#' is supported as input
9+
$expected = array(
10+
"#ffffff",
11+
"#00ff00",
12+
"#000000",
13+
"#ff9a00",
14+
);
15+
16+
foreach ($expected as $input) {
17+
$color = new Color($input);
18+
Assert::same((string) $color, $input, 'Incorrect color returned.');
19+
}

0 commit comments

Comments
 (0)