1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MedianetDev\PConnector\Concerns; |
4
|
|
|
|
5
|
|
|
trait Requests |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var array |
9
|
|
|
* |
10
|
|
|
* The request details |
11
|
|
|
*/ |
12
|
|
|
private $request; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
* |
17
|
|
|
* The response details |
18
|
|
|
*/ |
19
|
|
|
private $response; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var bool |
23
|
|
|
* |
24
|
|
|
* The status of the request |
25
|
|
|
*/ |
26
|
|
|
private $status; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
* |
31
|
|
|
* Error message |
32
|
|
|
*/ |
33
|
|
|
private $errorMessage; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
* |
38
|
|
|
* Additional headers |
39
|
|
|
*/ |
40
|
|
|
private $headers = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Send a get request. |
44
|
|
|
* |
45
|
|
|
* @param string $path [EX: 'posts'] |
46
|
|
|
* @param array $data The query data |
47
|
|
|
* |
48
|
|
|
* @return \MedianetDev\PConnector\PConnector |
49
|
|
|
*/ |
50
|
|
|
public function get(string $path = '', array $data = []) |
51
|
|
|
{ |
52
|
|
|
return $this->send($path, $data, 'GET'); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Send a post request. |
57
|
|
|
* |
58
|
|
|
* @param string $path [EX: 'posts'] |
59
|
|
|
* @param array $data The query data |
60
|
|
|
* |
61
|
|
|
* @return \MedianetDev\PConnector\PConnector |
62
|
|
|
*/ |
63
|
|
|
public function post(string $path = '', array $data = []) |
64
|
|
|
{ |
65
|
|
|
return $this->send($path, $data, 'POST'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Send a put request. |
70
|
|
|
* |
71
|
|
|
* @param string $path [EX: 'posts'] |
72
|
|
|
* @param array $data The query data |
73
|
|
|
* |
74
|
|
|
* @return \MedianetDev\PConnector\PConnector |
75
|
|
|
*/ |
76
|
|
|
public function put(string $path = '', array $data = []) |
77
|
|
|
{ |
78
|
|
|
return $this->send($path, $data, 'PUT'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Send a patch request. |
83
|
|
|
* |
84
|
|
|
* @param string $path [EX: 'posts'] |
85
|
|
|
* @param array $data The query data |
86
|
|
|
* |
87
|
|
|
* @return \MedianetDev\PConnector\PConnector |
88
|
|
|
*/ |
89
|
|
|
public function patch(string $path = '', array $data = []) |
90
|
|
|
{ |
91
|
|
|
return $this->send($path, $data, 'PATCH'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Send a delete request. |
96
|
|
|
* |
97
|
|
|
* @param string $path [EX: 'posts'] |
98
|
|
|
* @param array $data The query data |
99
|
|
|
* |
100
|
|
|
* @return \MedianetDev\PConnector\PConnector |
101
|
|
|
*/ |
102
|
|
|
public function delete(string $path = '', array $data = []) |
103
|
|
|
{ |
104
|
|
|
return $this->send($path, $data, 'DELETE'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/* |
108
|
|
|
|-------------------------------------------------------------------------- |
109
|
|
|
| Getters |
110
|
|
|
|-------------------------------------------------------------------------- |
111
|
|
|
*/ |
112
|
|
|
|
113
|
|
|
public function getResponseBody() |
114
|
|
|
{ |
115
|
|
|
return $this->response['body']; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function getResponseStatusCode() |
119
|
|
|
{ |
120
|
|
|
return $this->response['status_code']; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function getResponseHeaders() |
124
|
|
|
{ |
125
|
|
|
return $this->response['headers']; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function getRequestUrl() |
129
|
|
|
{ |
130
|
|
|
return $this->request['rul']; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function getRequestMethod() |
134
|
|
|
{ |
135
|
|
|
return $this->request['method']; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function getRequestData() |
139
|
|
|
{ |
140
|
|
|
return $this->request['payload']; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/* |
144
|
|
|
|-------------------------------------------------------------------------- |
145
|
|
|
| Setters |
146
|
|
|
|-------------------------------------------------------------------------- |
147
|
|
|
*/ |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Add additional headers to the request. |
151
|
|
|
* |
152
|
|
|
* @param array $headers |
153
|
|
|
* |
154
|
|
|
* @return \MedianetDev\PConnector\PConnector |
155
|
|
|
*/ |
156
|
|
|
public function withHeaders(array $headers) |
157
|
|
|
{ |
158
|
|
|
$this->headers = array_merge($this->headers, $headers); |
159
|
|
|
|
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Add additional header to the request. |
165
|
|
|
* |
166
|
|
|
* @param string $key the header name |
167
|
|
|
* @param string $value the header value |
168
|
|
|
* |
169
|
|
|
* @return \MedianetDev\PConnector\PConnector |
170
|
|
|
*/ |
171
|
|
|
public function withHeader(string $key, string $value) |
172
|
|
|
{ |
173
|
|
|
$this->headers = array_merge($this->headers, [$key => $value]); |
174
|
|
|
|
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|