1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NicolasBeauvais\BotScout; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
|
7
|
|
|
class BotScout |
8
|
|
|
{ |
9
|
|
|
protected $client; |
10
|
|
|
|
11
|
|
|
protected $baseUrl = 'http://botscout.com/test/'; |
12
|
|
|
|
13
|
|
|
private $apiKey; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param \GuzzleHttp\Client $client |
17
|
|
|
* @param string $apiKey |
18
|
|
|
*/ |
19
|
|
|
public function __construct(Client $client, string $apiKey) |
20
|
|
|
{ |
21
|
|
|
$this->client = $client; |
22
|
|
|
$this->apiKey = $apiKey; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Test matches all parameters at once. |
27
|
|
|
* |
28
|
|
|
* @param string $name |
29
|
|
|
* @param string $mail |
30
|
|
|
* @param string $ip |
31
|
|
|
* |
32
|
|
|
* @return BotScoutResponse |
33
|
|
|
*/ |
34
|
|
|
public function multi(string $name = null, string $mail = null, string $ip = null) |
35
|
|
|
{ |
36
|
|
|
return $this->makeRequest('multi', compact('name', 'mail', 'ip')); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Test matches a single item against all fields in the botscout database. |
41
|
|
|
* |
42
|
|
|
* @param string $all |
43
|
|
|
* |
44
|
|
|
* @return BotScoutResponse |
45
|
|
|
*/ |
46
|
|
|
public function all(string $all) |
47
|
|
|
{ |
48
|
|
|
return $this->makeRequest('multi', compact('all')); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Test matches a name. |
53
|
|
|
* |
54
|
|
|
* @param string $name |
55
|
|
|
* |
56
|
|
|
* @return BotScoutResponse |
57
|
|
|
*/ |
58
|
|
|
public function name(string $name = null) |
59
|
|
|
{ |
60
|
|
|
return $this->makeRequest(null, compact('name')); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Test matches an email. |
65
|
|
|
* |
66
|
|
|
* @param string $mail |
67
|
|
|
* |
68
|
|
|
* @return BotScoutResponse |
69
|
|
|
*/ |
70
|
|
|
public function mail(string $mail = null) |
71
|
|
|
{ |
72
|
|
|
return $this->makeRequest(null, compact('mail')); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Test matches an ip. |
77
|
|
|
* |
78
|
|
|
* @param string $ip |
79
|
|
|
* |
80
|
|
|
* @return BotScoutResponse |
81
|
|
|
*/ |
82
|
|
|
public function ip(string $ip = null) |
83
|
|
|
{ |
84
|
|
|
return $this->makeRequest(null, compact('ip')); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param array $query |
89
|
|
|
* |
90
|
|
|
* @return BotScoutResponse |
91
|
|
|
*/ |
92
|
|
|
public function makeRequest(string $type = null, array $query = []) |
93
|
|
|
{ |
94
|
|
|
if ($type) { |
|
|
|
|
95
|
|
|
$query[$type] = null; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$query['key'] = $this->apiKey; |
99
|
|
|
|
100
|
|
|
return $this->decodeResponse($this->client |
101
|
|
|
->get($this->baseUrl, compact('query')) |
102
|
|
|
->getBody() |
103
|
|
|
->getContents()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $response |
108
|
|
|
* |
109
|
|
|
* @return BotScoutResponse |
110
|
|
|
*/ |
111
|
|
|
private function decodeResponse(string $response) |
112
|
|
|
{ |
113
|
|
|
if (strpos($response, '!') !== false) { |
114
|
|
|
throw new \Exception($response); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return new BotScoutResponse($response); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: