Completed
Push — master ( 91c3c2...2b80d8 )
by Lawrence
02:02
created

Checker::whoisServers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 2
crap 1
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 +-----------------------------------------------------------------------------+
6
 | PHPPackage - Domain Checker
7
 +-----------------------------------------------------------------------------+
8
 | Copyright (c)2018 (http://github.com/phppackage/domaincheck)
9
 +-----------------------------------------------------------------------------+
10
 | This source file is subject to MIT License
11
 | that is bundled with this package in the file LICENSE.
12
 |
13
 | If you did not receive a copy of the license and are unable to
14
 | obtain it through the world-wide-web, please send an email
15
 | to [email protected] so we can send you a copy immediately.
16
 +-----------------------------------------------------------------------------+
17
 | Authors:
18
 |   Lawrence Cherone <[email protected]>
19
 +-----------------------------------------------------------------------------+
20
 */
21
22
namespace PHPPackage\DomainCheck;
23
24
class Checker
25
{
26
    /**
27
     * @var
28
     */
29
    private $tlds = [];
30
31
    /**
32
     *
33
     */
34 4
    public function __construct(array $tlds = [])
35
    {
36 4
        $this->tlds = $tlds;
37 4
    }
38
39
    /**
40
     * Checks the of the name
41
     * 
42
     * <code>
43
     * Array
44
     *   (
45
            [mynewdomainnamexyz] => Array
46
                (
47
                    [com] => 1
48
                    [it] => 
49
                    [net] => 1
50
                )
51
        
52
        )
53
     * </code>
54
     * 
55
     * @param $name
56
     * @return array
57
     */
58 2
    public function availability(string $name): array
59
    {
60 2
        $name = strtolower(trim($name));
61
        
62 2
        $whois = new Whois();
63
        
64 2
        $result = [];
65 2
        $result[$name] = [];
66 2
        foreach ($whois->servers($this->tlds) as $server) {
67 2
            if ($whois->check(
68 2
                $name.'.',
69 2
                $server['server'],
70 2
                $server['pattern']['available']
71
            )) {
72
                // domain is available
73 1
                $result[$name][$server['tld']] = true;
74
            } else {
75
                // domain is registered
76 2
                $result[$name][$server['tld']] = false;
77
            }
78
        }
79
        
80 2
        return $result;
81
    }
82
}
83