1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* _ _ ___ ____ ____ ____ |
5
|
|
|
* / \ | |__ _ _ ___ ___|_ _| _ \| _ \| __ ) |
6
|
|
|
* / _ \ | '_ \| | | / __|/ _ \| || |_) | | | | _ \ |
7
|
|
|
* / ___ \| |_) | |_| \__ \ __/| || __/| |_| | |_) | |
8
|
|
|
* /_/ \_\_.__/ \__,_|___/\___|___|_| |____/|____/ |
9
|
|
|
* |
10
|
|
|
* This file is part of Kristuff\AbsuseIPDB. |
11
|
|
|
* |
12
|
|
|
* (c) Kristuff <[email protected]> |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
* |
17
|
|
|
* @version 0.9.10 |
18
|
|
|
* @copyright 2020-2021 Kristuff |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace Kristuff\AbuseIPDB; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class ApiResponse |
25
|
|
|
* |
26
|
|
|
*/ |
27
|
|
|
class ApiResponse |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* |
31
|
|
|
* @access protected |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $curlResponse; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* |
38
|
|
|
* @access protected |
39
|
|
|
* @var object |
40
|
|
|
*/ |
41
|
|
|
protected $decodedResponse; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructor |
45
|
|
|
* |
46
|
|
|
* @access public |
47
|
|
|
* @param string $plaintext AbuseIPDB response in plaintext |
48
|
|
|
* |
49
|
|
|
*/ |
50
|
|
|
public function __construct(?string $plaintext = null) |
51
|
|
|
{ |
52
|
|
|
$this->curlResponse = $plaintext; |
53
|
|
|
$this->decodedResponse = !empty($plaintext) ? json_decode($plaintext, false) : null; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get response as array. May return null |
58
|
|
|
* |
59
|
|
|
* @access public |
60
|
|
|
* |
61
|
|
|
* @return array|null |
62
|
|
|
*/ |
63
|
|
|
public function getArray(): ?array |
64
|
|
|
{ |
65
|
|
|
return json_decode($this->curlResponse, true); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get response as object. May return null |
70
|
|
|
* |
71
|
|
|
* @access public |
72
|
|
|
* |
73
|
|
|
* @return object|null |
74
|
|
|
*/ |
75
|
|
|
public function getObject(): ?object |
76
|
|
|
{ |
77
|
|
|
return $this->decodedResponse; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get response as plaintext. May return null |
82
|
|
|
* |
83
|
|
|
* @access public |
84
|
|
|
* |
85
|
|
|
* @return string|null |
86
|
|
|
*/ |
87
|
|
|
public function getPlaintext(): ?string |
88
|
|
|
{ |
89
|
|
|
return $this->curlResponse; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get whether the response contains error(s) |
94
|
|
|
* |
95
|
|
|
* @access public |
96
|
|
|
* |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
public function hasError(): bool |
100
|
|
|
{ |
101
|
|
|
return count($this->errors()) > 0; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get an array of errors (object) contained is response |
106
|
|
|
* |
107
|
|
|
* @access public |
108
|
|
|
* |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
public function errors(): array |
112
|
|
|
{ |
113
|
|
|
return ($this->decodedResponse && $this->decodedResponse->errors) ? $this->decodedResponse->errors : []; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get an internal error message in an ApiResponse object |
118
|
|
|
* |
119
|
|
|
* @access public |
120
|
|
|
* @static |
121
|
|
|
* @param string $message The error message |
122
|
|
|
* |
123
|
|
|
* @return ApiResponse |
124
|
|
|
*/ |
125
|
|
|
public static function createErrorResponse(string $message): ApiResponse |
126
|
|
|
{ |
127
|
|
|
$response = [ |
128
|
|
|
"errors" => [ |
129
|
|
|
[ |
130
|
|
|
"title" => "Internal Error", |
131
|
|
|
"detail" => $message |
132
|
|
|
] |
133
|
|
|
] |
134
|
|
|
]; |
135
|
|
|
|
136
|
|
|
return new ApiResponse(json_encode($response)); |
137
|
|
|
} |
138
|
|
|
} |