ColorRule extends Rule
Rule: Ensures that a given value matches a valid color expression (e.g. "#ff0000").
This rule checks whether a value matches a configurable regular expression pattern representing a color value. By default, it validates 6-digit hexadecimal color codes starting with a '#' prefix.
Example:
$rule = new ColorRule();
$rule->check('#ff00ff'); // true
$rule->check('ff00ff'); // false
$custom = new ColorRule(['prefix' => '', 'pattern' => '/^%s[A-F0-9]{6}$/']);
$custom->check('FF00FF'); // true
Tags
Table of Contents
Constants
- DEFAULT_PATTERN = '/^%s[a-fA-F0-9]{6}$/'
- Default regex pattern format (supports prefix substitution via sprintf).
- NAME = 'color'
- The rule name.
- PATTERN = 'pattern'
- The 'pattern' parameter key.
- PREFIX = 'prefix'
- The 'prefix' parameter key.
Properties
- $fillableParams : array<string|int, mixed>|array<string|int, string>
- $message : string
- The default error message used when validation fails.
Methods
- __construct() : mixed
- Creates a new ColorRule instance.
- check() : bool
- Validates whether the given value matches the defined color pattern.
- pattern() : static
- Sets the regex pattern used to validate color expressions.
- prefix() : static
- Sets the prefix of the color expression (default '#').
Constants
DEFAULT_PATTERN
Default regex pattern format (supports prefix substitution via sprintf).
public
mixed
DEFAULT_PATTERN
= '/^%s[a-fA-F0-9]{6}$/'
The "%s" placeholder will be replaced by the prefix value.
NAME
The rule name.
public
mixed
NAME
= 'color'
PATTERN
The 'pattern' parameter key.
public
mixed
PATTERN
= 'pattern'
PREFIX
The 'prefix' parameter key.
public
mixed
PREFIX
= 'prefix'
Properties
$fillableParams
protected
array<string|int, mixed>|array<string|int, string>
$fillableParams
= [self::PATTERN, self::PREFIX]
$message
The default error message used when validation fails.
protected
string
$message
= ":attribute must be a valid color expression, ex: :prefixff0000"
Methods
__construct()
Creates a new ColorRule instance.
public
__construct([array<string|int, mixed> $init = [] ]) : mixed
Parameters
- $init : array<string|int, mixed> = []
-
Optional initialization parameters (keys: 'pattern', 'prefix').
check()
Validates whether the given value matches the defined color pattern.
public
check(mixed $value) : bool
Parameters
- $value : mixed
-
The value to validate.
Tags
Return values
bool —True if the value matches the pattern; false otherwise.
pattern()
Sets the regex pattern used to validate color expressions.
public
pattern([string $value = self::DEFAULT_PATTERN ]) : static
The pattern should include a "%s" placeholder where the prefix will be injected. Example: "/^%s[a-fA-F0-9]{6}$/i"
Parameters
- $value : string = self::DEFAULT_PATTERN
-
The regex pattern format.
Return values
static —Returns the current instance for method chaining.
prefix()
Sets the prefix of the color expression (default '#').
public
prefix([string $value = Char::NUMBER ]) : static
Parameters
- $value : string = Char::NUMBER
-
The prefix character(s) for color expressions.
Return values
static —Returns the current instance for method chaining.