Total Complexity | 7 |
Total Lines | 53 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
9 | class Host implements Rule |
||
10 | { |
||
11 | private array $hosts; |
||
12 | |||
13 | /** |
||
14 | * Create a new rule instance. |
||
15 | * |
||
16 | * @param array|string $host |
||
17 | */ |
||
18 | public function __construct($host) |
||
19 | { |
||
20 | $this->hosts = Arr::wrap($host); |
||
21 | } |
||
22 | |||
23 | public static function make($host): self |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * Determine if the validation rule passes. |
||
30 | * |
||
31 | * @param string $attribute |
||
32 | * @param mixed $value |
||
33 | * @return bool |
||
34 | */ |
||
35 | public function passes($attribute, $value) |
||
36 | { |
||
37 | if (!parse_url($value, PHP_URL_SCHEME)) { |
||
38 | $value = "https://{$value}"; |
||
39 | } |
||
40 | |||
41 | $host = parse_url($value, PHP_URL_HOST); |
||
42 | |||
43 | if (!$host) { |
||
44 | return false; |
||
45 | } |
||
46 | |||
47 | if (Str::startsWith($host, 'www.')) { |
||
48 | $host = Str::after($host, 'www.'); |
||
49 | } |
||
50 | |||
51 | return in_array($host, $this->hosts); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Get the validation error message. |
||
56 | * |
||
57 | * @return string |
||
58 | */ |
||
59 | public function message() |
||
62 | } |
||
63 | } |
||
64 |