mallardduck/extended-validator-laravel

View on GitHub
src/Rules/HexColor.php

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
<?php

declare(strict_types=1);

namespace MallardDuck\ExtendedValidator\Rules;

final class HexColor extends BaseRule
{
    public function __construct()
    {
        parent::__construct(
            static function (
                string $attribute,
                $value
            ) {
                return (bool) preg_match(
                    '/^#?(?:[0-9a-fA-F]{3}){1,2}$/',
                    $value,
                ) !== false;
            },
            'The :attribute field must be a valid 3 or 6 character HEX color code.'
        );
    }
}