Passed
Push — master ( 57ebbd...f70266 )
by Shahrad
01:49
created

HttpOptions::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EasyHttp\Model;
4
5
/**
6
 * Http Options
7
 *
8
 * @link    https://github.com/shahradelahi/easy-http
9
 * @author  Shahrad Elahi (https://github.com/shahradelahi)
10
 * @license https://github.com/shahradelahi/easy-http/blob/master/LICENSE (MIT License)
11
 */
12
class HttpOptions
13
{
14
15
    /**
16
     * @var ?array
17
     */
18
    public ?array $headers = [];
19
20
    /**
21
     * @var ?array
22
     */
23
    public ?array $queries = [];
24
25
    /**
26
     * @var ?string
27
     */
28
    public ?string $body = null;
29
30
    /**
31
     * The proxy server to use
32
     *
33
     * @var ?ProxyServer
34
     */
35
    public ?ProxyServer $proxy = null;
36
37
    /**
38
     * Add specific opt to curl
39
     *
40
     * @var ?array
41
     */
42
    public ?array $curlOptions = [];
43
44
    /**
45
     * The timeout of the request
46
     *
47
     * @var ?int
48
     */
49
    public ?int $timeout = null;
50
51
    /**
52
     * Http Options constructor.
53
     *
54
     * @param array $options
55
     */
56
    public function __construct(array $options = [])
57
    {
58
        $this->setOptions($options);
59
    }
60
61
    /**
62
     * Returns the class as an array
63
     *
64
     * @return array
65
     */
66
    public function toArray(): array
67
    {
68
        return [
69
            'headers' => $this->headers,
70
            'queries' => $this->queries,
71
            'body' => $this->body,
72
            'proxy' => $this->proxy,
73
            'curlOptions' => $this->curlOptions,
74
            'timeout' => $this->timeout
75
        ];
76
    }
77
78
    /**
79
     * Set Options
80
     *
81
     * @param array $options
82
     * @return void
83
     */
84
    public function setOptions(array $options): void
85
    {
86
        foreach ($options as $key => $value) {
87
            if (method_exists($this, 'set' . ucfirst($key))) {
88
                if ($value !== null) {
89
                    $this->{'set' . ucfirst($key)}($value);
90
                }
91
            } else {
92
                if (property_exists($this, $key)) {
93
                    $this->{$key} = $value;
94
                } else {
95
                    throw new \InvalidArgumentException("Invalid option: $key");
96
                }
97
            }
98
        }
99
    }
100
101
    /**
102
     * Set Body of Http request
103
     *
104
     * @param ?string|array $body The body of the request - On array it will be converted to json
105
     * @return void
106
     */
107
    public function setBody(string|array|null $body): void
108
    {
109
        if (is_array($body)) {
0 ignored issues
show
introduced by
The condition is_array($body) is always true.
Loading history...
110
            $this->body = json_encode($body);
111
            $this->headers['Content-Type'] = 'application/json';
112
        } else {
113
            $this->body = $body;
114
        }
115
    }
116
117
    /**
118
     * Set proxy server
119
     *
120
     * @param array $proxy ["host", "port", "user", "pass"]
121
     * @return void
122
     */
123
    public function setProxy(array $proxy): void
124
    {
125
        $this->proxy = (new ProxyServer())->setProxy($proxy);
126
    }
127
128
    /**
129
     * Get Query String
130
     *
131
     * @return string
132
     */
133
    public function getQueryString(): string
134
    {
135
        return http_build_query($this->queries);
0 ignored issues
show
Bug introduced by
It seems like $this->queries can also be of type null; however, parameter $data of http_build_query() does only seem to accept array|object, 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

135
        return http_build_query(/** @scrutinizer ignore-type */ $this->queries);
Loading history...
136
    }
137
138
    /**
139
     * Set Curl Options
140
     *
141
     * @param array $options [{"CURLOPT_*": "value"}, ...]
142
     * @return void
143
     */
144
    public function setCurlOptions(array $options): void
145
    {
146
        if (count($options) > 0) {
147
            foreach ($options as $option => $value) {
148
                $this->curlOptions[$option] = $value;
149
            }
150
        }
151
    }
152
153
}