forceFull = $forceFull; $this->allowAlpha = $allowAlpha; } /** * Run the validation rule. * * @param string $attribute * @param mixed $value * @param \Closure(string):\Illuminate\Translation\PotentiallyTranslatedString $fail * @return void * @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceBeforeLastUsed * @phpcs:disable Squiz.Commenting.FunctionComment.IncorrectTypeHint */ public function validate(string $attribute, mixed $value, \Closure $fail): void { $pattern = '/^#([a-fA-F0-9]{6}'; if (!$this->forceFull) { $pattern .= '|[a-fA-F0-9]{3}'; } if ($this->allowAlpha) { $pattern .= '|[a-fA-F0-9]{8}'; if (!$this->forceFull) { $pattern .= '|[a-fA-F0-9]{4}'; } } $pattern .= ')$/'; if (!(bool)preg_match($pattern, $value)) { $fail(trans($this->message())); } } /** * Get the validation error message. * * @return string */ public function message(): string { return 'The hex code is not valid'; } }