ApiResponse::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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