1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace EmailValidator\Validator; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Abstract base class for validators that check against provider lists |
9
|
|
|
* |
10
|
|
|
* This abstract class provides functionality for validators that need to check |
11
|
|
|
* email addresses against lists of providers (e.g., disposable or free email providers). |
12
|
|
|
* It handles fetching and parsing provider lists from various sources and formats. |
13
|
|
|
*/ |
14
|
|
|
abstract class AProviderValidator extends AValidator |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Array of provider list sources and their formats |
18
|
|
|
* |
19
|
|
|
* @var array<array{format: string, url: string}> |
20
|
|
|
*/ |
21
|
|
|
protected static array $providers = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Gets and merges provider lists from various sources |
25
|
|
|
* |
26
|
|
|
* Fetches public lists of provider domains and merges them together into one array. |
27
|
|
|
* If a custom list is provided, it is merged into the new list. |
28
|
|
|
* |
29
|
|
|
* @param bool $checkLocalOnly If true, only use the provided list and skip external sources |
30
|
|
|
* @param array<string> $list Custom list of provider domains to merge with external lists |
31
|
|
|
* @return array<string> Merged and deduplicated list of provider domains |
32
|
|
|
*/ |
33
|
|
|
public function getList(bool $checkLocalOnly = false, array $list = []): array |
34
|
|
|
{ |
35
|
|
|
$providers = []; |
36
|
|
|
if (!$checkLocalOnly) { |
37
|
|
|
foreach (static::$providers as $provider) { |
38
|
|
|
if (filter_var($provider['url'], FILTER_VALIDATE_URL)) { |
39
|
|
|
$content = @file_get_contents($provider['url']); |
40
|
|
|
if ($content) { |
41
|
|
|
$providers[] = $this->getExternalList($content, $provider['format']); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
return array_values(array_filter(array_unique(array_merge($list, ...$providers)), 'is_string')); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Parses a provider list based on its format |
51
|
|
|
* |
52
|
|
|
* Supports JSON and plain text formats for provider lists. |
53
|
|
|
* |
54
|
|
|
* @param string $content The content of the provider list |
55
|
|
|
* @param string $type The format of the list ('json' or 'txt') |
56
|
|
|
* @return array<string> Parsed list of provider domains |
57
|
|
|
*/ |
58
|
|
|
protected function getExternalList(string $content, string $type): array |
59
|
|
|
{ |
60
|
|
|
if (empty($content)) { |
61
|
|
|
return []; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
switch ($type) { |
65
|
|
|
case 'json': |
66
|
|
|
$providers = json_decode($content, true); |
67
|
|
|
if (!is_array($providers)) { |
68
|
|
|
return []; |
69
|
|
|
} |
70
|
|
|
break; |
71
|
|
|
case 'txt': |
72
|
|
|
default: |
73
|
|
|
$providers = array_filter(explode("\n", str_replace("\r\n", "\n", $content)), 'strlen'); |
74
|
|
|
break; |
75
|
|
|
} |
76
|
|
|
return array_values(array_filter($providers, 'is_string')); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|