1
|
|
|
<?php |
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
|
|
|
namespace Kristuff\AbuseIPDB; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Trait Errors |
24
|
|
|
* |
25
|
|
|
*/ |
26
|
|
|
trait ErrorsTrait |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Check and print errors in API response. Null response object is considered as no errors |
30
|
|
|
* |
31
|
|
|
* @access protected |
32
|
|
|
* @static |
33
|
|
|
* @param object $response |
34
|
|
|
* |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
|
|
protected static function printPlainTextErrors(object $response) |
38
|
|
|
{ |
39
|
|
|
foreach ($response->errors as $err){ |
40
|
|
|
$text = 'Error: '; |
41
|
|
|
$text .= self::getErrorDetail($err, 'title'); |
|
|
|
|
42
|
|
|
$text .= self::getErrorDetail($err, 'statuts'); |
|
|
|
|
43
|
|
|
$text .= self::getErrorDetail($err, 'parameter', 'source'); |
|
|
|
|
44
|
|
|
$text .= self::getErrorDetail($err, 'detail'); |
|
|
|
|
45
|
|
|
$text .= PHP_EOL; |
46
|
|
|
echo $text; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get error property if exist |
52
|
|
|
* |
53
|
|
|
* @access protected |
54
|
|
|
* @static |
55
|
|
|
* @param object $error |
56
|
|
|
* @param string $field |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
private static function getErrorDetail(object $error, string $field, ?string $parent = null) |
61
|
|
|
{ |
62
|
|
|
if (!empty($parent)){ |
63
|
|
|
return !empty($error->$parent) && !empty($error->$parent->$field) ? ' ' . $field . ': ' . $error->$parent->$field : ''; |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return !empty($error->$field) ? ' ' . $field . ': ' . $error->$field : ''; |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Check and print errors in API response. |
71
|
|
|
* |
72
|
|
|
* @access protected |
73
|
|
|
* @static |
74
|
|
|
* @param object $response |
75
|
|
|
* |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
protected static function checkForEmpty(object $response) |
79
|
|
|
{ |
80
|
|
|
// check for empty response ? |
81
|
|
|
if ( empty($response) || empty($response->data) ){ |
82
|
|
|
self::error('An unexpected error occurred.'); |
83
|
|
|
return true; |
84
|
|
|
} |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
} |
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.