Passed
Push — master ( 707ddf...aa8b2f )
by Sebastian
04:56
created

ConvertHelper_URLFinder_DomainExtensions   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 15
c 1
b 0
f 0
dl 0
loc 50
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A nameExists() 0 5 1
A load() 0 14 3
A getNames() 0 3 1
1
<?php
2
/**
3
 * File containing the class {@see ConvertHelper_URLFinder_DomainExtensions}
4
 *
5
 * @package AppUtils
6
 * @subpackage URLFinder
7
 * @see ConvertHelper_URLFinder_DomainExtensions
8
 */
9
10
declare(strict_types=1);
11
12
namespace AppUtils;
13
14
/**
15
 * Access a list of all top level domain extensions.
16
 *
17
 * @package AppUtils
18
 * @subpackage URLFinder
19
 * @author Sebastian Mordziol <[email protected]>
20
 *
21
 * @link https://data.iana.org/TLD/tlds-alpha-by-domain.txt
22
 */
23
class ConvertHelper_URLFinder_DomainExtensions
24
{
25
    /**
26
     * @var string
27
     */
28
    private static $cacheFile = '';
29
30
    /**
31
     * @var string[]
32
     */
33
    private static $names = array();
34
35
    public function __construct()
36
    {
37
        if(empty(self::$cacheFile)) {
38
            self::$cacheFile = __DIR__.'/tlds-alpha-by-domain.txt';
39
        }
40
41
        $this->load();
42
    }
43
44
    /**
45
     * @return string[]
46
     */
47
    public function getNames() : array
48
    {
49
        return self::$names;
50
    }
51
52
    public function nameExists(string $name) : bool
53
    {
54
        $name = strtolower($name);
55
56
        return isset(self::$names[$name]);
57
    }
58
59
    private function load() : void
60
    {
61
        if(!empty(self::$names)) {
62
            return;
63
        }
64
65
        $content = strtolower(FileHelper::readContents(self::$cacheFile));
66
67
        $names = ConvertHelper::explodeTrim("\n", $content);
68
69
        // Store the names as keys in the array to allow
70
        // faster lookups (to avoid using in_array).
71
        foreach($names as $name) {
72
            self::$names[$name] = '';
73
        }
74
    }
75
}
76