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|string $data The query data |
||
47 | * @return \MedianetDev\PConnector\PConnector |
||
48 | */ |
||
49 | public function get(string $path = '', $data = []) |
||
50 | { |
||
51 | return $this->send($path, $data, 'GET'); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Send a post request. |
||
56 | * |
||
57 | * @param string $path [EX: 'posts'] |
||
58 | * @param array|string $data The query data |
||
59 | * @return \MedianetDev\PConnector\PConnector |
||
60 | */ |
||
61 | public function post(string $path = '', $data = []) |
||
62 | { |
||
63 | return $this->send($path, $data, 'POST'); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Send a put request. |
||
68 | * |
||
69 | * @param string $path [EX: 'posts'] |
||
70 | * @param array|string $data The query data |
||
71 | * @return \MedianetDev\PConnector\PConnector |
||
72 | */ |
||
73 | public function put(string $path = '', $data = []) |
||
74 | { |
||
75 | return $this->send($path, $data, 'PUT'); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Send a patch request. |
||
80 | * |
||
81 | * @param string $path [EX: 'posts'] |
||
82 | * @param array|string $data The query data |
||
83 | * @return \MedianetDev\PConnector\PConnector |
||
84 | */ |
||
85 | public function patch(string $path = '', $data = []) |
||
86 | { |
||
87 | return $this->send($path, $data, 'PATCH'); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Send a delete request. |
||
92 | * |
||
93 | * @param string $path [EX: 'posts'] |
||
94 | * @param array|string $data The query data |
||
95 | * @return \MedianetDev\PConnector\PConnector |
||
96 | */ |
||
97 | public function delete(string $path = '', $data = []) |
||
98 | { |
||
99 | return $this->send($path, $data, 'DELETE'); |
||
100 | } |
||
101 | |||
102 | /* |
||
103 | |-------------------------------------------------------------------------- |
||
104 | | Getters |
||
105 | |-------------------------------------------------------------------------- |
||
106 | */ |
||
107 | |||
108 | public function getResponseBody() |
||
109 | { |
||
110 | return $this->response['body']; |
||
111 | } |
||
112 | |||
113 | public function getResponseStatusCode() |
||
114 | { |
||
115 | return $this->response['status_code']; |
||
116 | } |
||
117 | |||
118 | public function getResponseHeaders() |
||
119 | { |
||
120 | return $this->response['headers']; |
||
121 | } |
||
122 | |||
123 | public function getRequestUrl() |
||
124 | { |
||
125 | return $this->request['url']; |
||
126 | } |
||
127 | |||
128 | public function getRequestMethod() |
||
129 | { |
||
130 | return $this->request['method']; |
||
131 | } |
||
132 | |||
133 | public function getRequestData() |
||
134 | { |
||
135 | return $this->request['payload']; |
||
136 | } |
||
137 | |||
138 | /* |
||
139 | |-------------------------------------------------------------------------- |
||
140 | | Setters |
||
141 | |-------------------------------------------------------------------------- |
||
142 | */ |
||
143 | |||
144 | /** |
||
145 | * Add additional headers to the request. |
||
146 | * |
||
147 | * @param array $headers |
||
148 | * @return \MedianetDev\PConnector\PConnector |
||
149 | */ |
||
150 | public function withHeaders(array $headers) |
||
151 | { |
||
152 | $this->headers = array_merge($this->headers, $headers); |
||
153 | |||
154 | return $this; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Add additional header to the request. |
||
159 | * |
||
160 | * @param string $key the header name |
||
161 | * @param string $value the header value |
||
162 | * @return \MedianetDev\PConnector\PConnector |
||
163 | */ |
||
164 | public function withHeader(string $key, string $value) |
||
165 | { |
||
166 | $this->headers = array_merge($this->headers, [$key => $value]); |
||
167 | |||
168 | return $this; |
||
169 | } |
||
170 | } |
||
171 |