File size: 623 Bytes
5f32fbd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <?php
declare(strict_types=1);
namespace App\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
class DomainRule implements ValidationRule
{
public function __construct(public bool $allowWildcard = false) {}
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if ($value === '*' && $this->allowWildcard) {
return;
}
if (! (bool) preg_match('/^(?:[a-z0-9](?:[a-z0-9-æøå]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$/iu', $value)) {
$fail('The :attribute is not a valid domain.');
}
}
}
|