Passed
Push — master ( fa42da...3c5c71 )
by Kris
01:35
created

ApiResponse::hasError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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.9
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 = json_decode($plaintext, false);
0 ignored issues
show
Bug introduced by
It seems like $plaintext can also be of type null; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
        $this->decodedResponse = json_decode(/** @scrutinizer ignore-type */ $plaintext, false);
Loading history...
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
}