1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EasyHttp\SymfonyLayer; |
4
|
|
|
|
5
|
|
|
use EasyHttp\LayerContracts\Contracts\HttpClientRequest; |
6
|
|
|
|
7
|
|
|
class SymfonyRequest implements HttpClientRequest |
8
|
|
|
{ |
9
|
|
|
protected string $method; |
10
|
|
|
|
11
|
|
|
protected string $uri; |
12
|
|
|
|
13
|
|
|
protected array $headers = []; |
14
|
|
|
|
15
|
|
|
protected array $json = []; |
16
|
|
|
|
17
|
|
|
protected array $query = []; |
18
|
|
|
|
19
|
|
|
protected int $timeout = 10; |
20
|
|
|
|
21
|
|
|
protected bool $ssl = false; |
22
|
|
|
|
23
|
|
|
protected array $options; |
24
|
|
|
|
25
|
|
|
protected array $basicAuth = []; |
26
|
|
|
|
27
|
14 |
|
public function __construct(string $method, string $uri, array $options = []) |
28
|
|
|
{ |
29
|
14 |
|
$this->method = $method; |
30
|
14 |
|
$this->uri = $uri; |
31
|
14 |
|
$this->options = $options; |
32
|
14 |
|
} |
33
|
|
|
|
34
|
14 |
|
public function getMethod(): string |
35
|
|
|
{ |
36
|
14 |
|
return $this->method; |
37
|
|
|
} |
38
|
|
|
|
39
|
14 |
|
public function getUri(): string |
40
|
|
|
{ |
41
|
14 |
|
return $this->uri; |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
public function getHeader(string $key) |
45
|
|
|
{ |
46
|
1 |
|
return $this->headers[$key] ?? null; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getHeaders(): array |
50
|
|
|
{ |
51
|
|
|
return $this->headers; |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
public function getJson(): array |
55
|
|
|
{ |
56
|
1 |
|
return $this->json; |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
public function getQuery(): array |
60
|
|
|
{ |
61
|
1 |
|
return $this->query; |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
public function setMethod(string $method): self |
65
|
|
|
{ |
66
|
1 |
|
$this->method = $method; |
67
|
|
|
|
68
|
1 |
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
public function setUri(string $uri): self |
72
|
|
|
{ |
73
|
1 |
|
$this->uri = $uri; |
74
|
|
|
|
75
|
1 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
2 |
|
public function setHeader(string $key, string $value): self |
79
|
|
|
{ |
80
|
2 |
|
$this->headers[$key] = $value; |
81
|
|
|
|
82
|
2 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
public function setJson(array $json): self |
86
|
|
|
{ |
87
|
1 |
|
$this->json = $json; |
88
|
|
|
|
89
|
1 |
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
3 |
|
public function setQuery(array $query): self |
93
|
|
|
{ |
94
|
3 |
|
$this->query = $query; |
95
|
|
|
|
96
|
3 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
2 |
|
public function setBasicAuth(string $username, string $password): self |
100
|
|
|
{ |
101
|
2 |
|
$this->basicAuth = [$username, $password]; |
102
|
|
|
|
103
|
2 |
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function ssl(bool $ssl): void |
107
|
|
|
{ |
108
|
|
|
$this->ssl = $ssl; |
109
|
|
|
} |
110
|
|
|
|
111
|
14 |
|
public function options(): array |
112
|
|
|
{ |
113
|
|
|
$options = [ |
114
|
14 |
|
'timeout' => $this->timeout, |
115
|
14 |
|
'verify_peer' => $this->ssl, |
116
|
14 |
|
'headers' => $this->headers, |
117
|
|
|
]; |
118
|
|
|
|
119
|
14 |
|
if ($this->json) { |
|
|
|
|
120
|
1 |
|
$options['headers'] = array_merge(['Content-Type' => 'application/json;charset=UTF-8'], $this->headers); |
121
|
1 |
|
$options['json'] = $this->json; |
122
|
|
|
} |
123
|
|
|
|
124
|
14 |
|
if ($this->query) { |
|
|
|
|
125
|
3 |
|
$options['query'] = $this->query; |
126
|
|
|
} |
127
|
|
|
|
128
|
14 |
|
if ($this->basicAuth) { |
|
|
|
|
129
|
2 |
|
$options['auth_basic'] = $this->basicAuth; |
130
|
|
|
} |
131
|
|
|
|
132
|
14 |
|
return $options; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.