Skip to content

Commit 10d4ad1

Browse files
committed
Merge branch 'new-locator'
2 parents 75085c6 + 919da69 commit 10d4ad1

9 files changed

+100
-54
lines changed

CHANGELOG.md

+12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
9+
### Changed
10+
- Improved the flexibility of the WhoisServerList tests.
11+
- Refactor AbstractClient and client to use the AbstractLocator and DomainLocator.
12+
- Update AbstractClient property name - from tldLocator to whoisLocator - to better reflect usage.
13+
- Refactor tests to use DomainLocator instead of removed Locator class.
14+
15+
### Added
16+
- Create an AbstractLocator and DomainLocator for more flexibility!
17+
18+
### Removed
19+
- Cleaned up some more PHP 5.6 support code from tests.
20+
- Removed old style locator class.
921

1022
## [0.1.5] - 2018-05-01
1123
### Removed

src/AbstractClient.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
use TrueBV\Punycode;
55
use League\Uri\Components\Host;
66
use Hoa\Socket\Client as SocketClient;
7-
use MallardDuck\Whois\WhoisServerList\Locator;
7+
use MallardDuck\Whois\WhoisServerList\AbstractLocator;
8+
use MallardDuck\Whois\WhoisServerList\DomainLocator;
89
use MallardDuck\Whois\Exceptions\MissingArgException;
910

1011
/**
@@ -21,9 +22,9 @@ class AbstractClient
2122

2223
/**
2324
* The TLD Whois locator class.
24-
* @var Locator
25+
* @var AbstractLocator
2526
*/
26-
protected $tldLocator;
27+
protected $whoisLocator;
2728

2829
/**
2930
* The Unicode for IDNA.
@@ -55,7 +56,7 @@ class AbstractClient
5556
public function __construct()
5657
{
5758
$this->punycode = new Punycode();
58-
$this->tldLocator = new Locator();
59+
$this->whoisLocator = new DomainLocator();
5960
}
6061

6162
/**

src/Client.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function lookup($domain = '')
3737
$this->parseWhoisDomain($domain);
3838

3939
// Get the domains whois server.
40-
$whoisServer = $this->tldLocator->getWhoisServer($this->parsedDomain);
40+
$whoisServer = $this->whoisLocator->getWhoisServer($this->parsedDomain);
4141

4242
// Get the full output of the whois lookup.
4343
$response = $this->makeWhoisRequest($this->parsedDomain, $whoisServer);

src/WhoisServerList/Locator.php renamed to src/WhoisServerList/AbstractLocator.php

+8-24
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22
namespace MallardDuck\Whois\WhoisServerList;
33

4-
use MallardDuck\Whois\Exceptions\MissingArgException;
54
use MallardDuck\Whois\Exceptions\UnknownWhoisException;
65

76
/**
@@ -15,7 +14,7 @@
1514
*
1615
* @version 1.0.0
1716
*/
18-
class Locator
17+
abstract class AbstractLocator
1918
{
2019

2120
/**
@@ -30,33 +29,33 @@ class Locator
3029
*
3130
* @var string
3231
*/
33-
private $tldListPath = __DIR__ . '/../../blobs/tld.json';
32+
protected $whoisListPath;
3433

3534
/**
3635
* A collection of the TLDs and whois server list.
3736
*
3837
* @var \Tightenco\Collect\Support\Collection
3938
*/
40-
private $tldCollection;
39+
protected $whoisCollection;
4140

4241
/**
4342
* The results of the last looked up domain.
4443
*
4544
* @var array
4645
*/
47-
private $lastMatch;
46+
protected $lastMatch;
4847

4948
/**
5049
* Build the TLD Whois Server Locator class.
5150
*/
5251
public function __construct()
5352
{
54-
$fileData = file_get_contents($this->tldListPath);
53+
$fileData = file_get_contents($this->whoisListPath);
5554
$tldData = json_decode($fileData);
5655
if (null !== $tldData && json_last_error() === JSON_ERROR_NONE) {
5756
$this->loadStatus = true;
5857
}
59-
$this->tldCollection = collect((array) $tldData);
58+
$this->whoisCollection = collect((array) $tldData);
6059
}
6160

6261
/**
@@ -86,22 +85,7 @@ public function getLastMatch()
8685
*
8786
* @return self Returns the same instance for fluent usage.
8887
*/
89-
public function findWhoisServer($domain)
90-
{
91-
if (empty($domain)) {
92-
throw new MissingArgException("Must provide domain argument.");
93-
}
94-
95-
$tldInfo = $this->tldCollection->filter(function ($item, $key) use ($domain) {
96-
return preg_match('/'.$key.'/', $domain);
97-
});
98-
if (empty($tldInfo->all())) {
99-
throw new UnknownWhoisException("This domain doesn't have a valid TLD whois server.");
100-
}
101-
$this->lastMatch = $tldInfo->all();
102-
103-
return $this;
104-
}
88+
abstract public function findWhoisServer($domain);
10589

10690
/**
10791
* Get the Whois server of the domain provided, or previously found domain.
@@ -117,7 +101,7 @@ public function getWhoisServer($domain = '')
117101
}
118102
$server = current($this->lastMatch);
119103
if ('UNKNOWN' === strtoupper($server)) {
120-
throw new UnknownWhoisException("This domain doesn't have a valid TLD whois server.");
104+
throw new UnknownWhoisException("This domain doesn't have a valid whois server.");
121105
}
122106

123107
return $server;

src/WhoisServerList/DomainLocator.php

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
namespace MallardDuck\Whois\WhoisServerList;
3+
4+
use MallardDuck\Whois\Exceptions\MissingArgException;
5+
use MallardDuck\Whois\Exceptions\UnknownWhoisException;
6+
7+
/**
8+
* Whois Server List Locator Class
9+
*
10+
* This class loads a TLD whois list and allows for easy look up.
11+
*
12+
* @author mallardduck <[email protected]>
13+
*
14+
* @copyright lucidinternets.com 2018
15+
*
16+
* @version 1.0.0
17+
*/
18+
class DomainLocator extends AbstractLocator
19+
{
20+
21+
/**
22+
* The path where the tld json file exists.
23+
*
24+
* @var string
25+
*/
26+
protected $whoisListPath = __DIR__ . '/../../blobs/tld.json';
27+
28+
/**
29+
* Finds and returns the last match looked up.
30+
*
31+
* @param string $domain Either an ID or a username.
32+
*
33+
* @return self Returns the same instance for fluent usage.
34+
*/
35+
public function findWhoisServer($domain)
36+
{
37+
if (empty($domain)) {
38+
throw new MissingArgException("Must provide domain argument.");
39+
}
40+
41+
$tldInfo = $this->whoisCollection->filter(function ($item, $key) use ($domain) {
42+
return preg_match('/'.$key.'/', $domain);
43+
});
44+
if (empty($tldInfo->all())) {
45+
throw new UnknownWhoisException("This domain doesn't have a valid TLD whois server.");
46+
}
47+
$this->lastMatch = $tldInfo->all();
48+
49+
return $this;
50+
}
51+
}

tests/BaseTest.php

+6-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
namespace MallardDuck\Whois\Test;
33

44
use PHPUnit\Framework\TestCase;
5-
use League\Uri\Components\Host;
65

76
/**
87
* Corresponding Class to test the whois Client class
@@ -14,15 +13,14 @@
1413
*/
1514
abstract class BaseTest extends TestCase
1615
{
16+
17+
/**
18+
* [getMethod description]
19+
* @return \League\Uri\Components\Exception [description]
20+
*/
1721
public function getUriException()
1822
{
19-
$host = new Host();
20-
$isNewLib = (method_exists($host, 'getRegistrableDomain')) ? true : false;
21-
if ($isNewLib) {
22-
return \League\Uri\Components\Exception::class;
23-
}
24-
25-
return \Exception::class;
23+
return \League\Uri\Components\Exception::class;
2624
}
2725

2826
/**

tests/WhoisLocatorExceptionTest.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
namespace MallardDuck\Whois\Test;
33

44
use PHPUnit\Framework\TestCase;
5-
use MallardDuck\Whois\WhoisServerList\Locator;
5+
use MallardDuck\Whois\WhoisServerList\DomainLocator;
66
use MallardDuck\Whois\Exceptions\MissingArgException;
77
use MallardDuck\Whois\Exceptions\UnknownWhoisException;
88

@@ -24,7 +24,7 @@ public function testBlankStringThrowsException()
2424
{
2525
$this->expectException(MissingArgException::class);
2626

27-
$var = new Locator;
27+
$var = new DomainLocator;
2828
$results = $var->findWhoisServer('');
2929
unset($var, $results);
3030
}
@@ -34,7 +34,7 @@ public function testBlankStringThrowsException()
3434
*/
3535
public function testFindServerThenGetWhoisServerThenEmpty()
3636
{
37-
$var = new Locator;
37+
$var = new DomainLocator;
3838
$results = $var->findWhoisServer("com.com")->getWhoisServer();
3939
$this->assertTrue(is_string($results) && !empty($results));
4040
$this->assertTrue("whois.verisign-grs.com" === $results);
@@ -56,7 +56,7 @@ public function testNullStringThrowsException()
5656
$this->expectException(\Exception::class);
5757
}
5858

59-
$var = new Locator;
59+
$var = new DomainLocator;
6060
$results = $var->findWhoisServer(null);
6161
unset($var, $results);
6262
}
@@ -66,7 +66,7 @@ public function testNullStringThrowsException()
6666
*/
6767
public function testFindServerThenGetWhoisServerThenNull()
6868
{
69-
$var = new Locator;
69+
$var = new DomainLocator;
7070
$results = $var->findWhoisServer("com.com")->getWhoisServer();
7171
$this->assertTrue(is_string($results) && !empty($results));
7272
$this->assertTrue("whois.verisign-grs.com" === $results);
@@ -86,15 +86,15 @@ public function testFindServerThenGetWhoisServerThenNull()
8686
*/
8787
public function testGetWhoisServerDirect()
8888
{
89-
$var = new Locator;
89+
$var = new DomainLocator;
9090
$results = $var->getWhoisServer("bing.com");
9191
$this->assertTrue(is_string($results) && !empty($results));
9292
$this->assertTrue("whois.verisign-grs.com" === $results);
9393
unset($var, $results);
9494

9595
$this->expectException(MissingArgException::class);
9696

97-
$var = new Locator;
97+
$var = new DomainLocator;
9898
$results = $var->getWhoisServer();
9999
unset($var, $results);
100100
}
@@ -104,7 +104,7 @@ public function testGetWhoisServerDirect()
104104
*/
105105
public function testGetWhoisServerDirectNoException()
106106
{
107-
$var = new Locator;
107+
$var = new DomainLocator;
108108
$results = $var->getWhoisServer("bing.com");
109109
$orgResults = $results;
110110
$this->assertTrue(is_string($results) && !empty($results));
@@ -121,7 +121,7 @@ public function testGetWhoisServerDirectNoException()
121121
*/
122122
public function testGetWhoisServerDirectUnicodeException()
123123
{
124-
$var = new Locator;
124+
$var = new DomainLocator;
125125
$this->expectException(UnknownWhoisException::class);
126126

127127
$results = $var->getWhoisServer('xn--e1afmkfd.xn--80akhbyknj4f');

tests/WhoisLocatorLookupsTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
namespace MallardDuck\Whois\Test;
33

44
use PHPUnit\Framework\TestCase;
5-
use MallardDuck\Whois\WhoisServerList\Locator;
5+
use MallardDuck\Whois\WhoisServerList\DomainLocator;
66

77
/**
88
* Corresponding Class to test the Locator class
@@ -24,7 +24,7 @@ class WhoisLocatorLookupsTest extends TestCase
2424
*/
2525
public function testFindAndGetCorrectDomainWhoisServer($domain, $server)
2626
{
27-
$var = new Locator;
27+
$var = new DomainLocator;
2828
$results = $var->findWhoisServer($domain)->getWhoisServer();
2929
$this->assertTrue(is_string($results) && !empty($results));
3030
$this->assertTrue($server === $results);
@@ -39,7 +39,7 @@ public function testFindAndGetCorrectDomainWhoisServer($domain, $server)
3939
*/
4040
public function testGetCorrectDomainWhoisServer($domain, $server)
4141
{
42-
$var = new Locator;
42+
$var = new DomainLocator;
4343
$results = $var->getWhoisServer($domain);
4444
$this->assertTrue(is_string($results) && !empty($results));
4545
$this->assertTrue($server === $results);

tests/WhoisLocatorTest.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
namespace MallardDuck\Whois\Test;
33

44
use PHPUnit\Framework\TestCase;
5-
use MallardDuck\Whois\WhoisServerList\Locator;
5+
use MallardDuck\Whois\WhoisServerList\DomainLocator;
66

77
/**
88
* Corresponding Class to test the Locator class
@@ -20,7 +20,7 @@ class WhoisLocatorTest extends TestCase
2020
*/
2121
public function testIsThereAnySyntaxError()
2222
{
23-
$var = new Locator;
23+
$var = new DomainLocator;
2424
$this->assertTrue(is_object($var));
2525
unset($var);
2626
}
@@ -30,7 +30,7 @@ public function testIsThereAnySyntaxError()
3030
*/
3131
public function testLoadedListFile()
3232
{
33-
$var = new Locator;
33+
$var = new DomainLocator;
3434
$this->assertTrue(is_object($var) && $var->getLoadStatus());
3535
unset($var);
3636
}
@@ -40,13 +40,13 @@ public function testLoadedListFile()
4040
*/
4141
public function testFindWhoisServer()
4242
{
43-
$var = new Locator;
43+
$var = new DomainLocator;
4444
$var->findWhoisServer("google.com");
4545
$match = $var->getLastMatch();
4646
$this->assertTrue(is_array($match) && !empty($match) && count($match) >= 1);
4747
unset($var, $match);
4848

49-
$var = new Locator;
49+
$var = new DomainLocator;
5050
$var->findWhoisServer("danpock.xyz");
5151
$match = $var->getLastMatch();
5252
$this->assertTrue(is_array($match) && !empty($match) && count($match) >= 1);

0 commit comments

Comments
 (0)