Skip to content

Commit d64c790

Browse files
committed
feat: HttpClient wrapper class to invoke client methods statically
1 parent ad77c33 commit d64c790

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

src/Http/Client/Http.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace BitApps\WPKit\Http\Client;
4+
5+
use ArgumentCountError;
6+
use BadMethodCallException;
7+
8+
class Http
9+
{
10+
private HttpClient $_client;
11+
12+
public function __call($method, $args)
13+
{
14+
return $this->forwardCall($method, $args);
15+
}
16+
17+
public static function __callStatic($method, $args)
18+
{
19+
return forward_static_call([new static(), 'forwardCall'], $method, $args);
20+
}
21+
22+
private function forwardCall($method, $args)
23+
{
24+
if (!isset($this->_client)) {
25+
$this->_client = new HttpClient();
26+
}
27+
28+
if (method_exists($this->_client, $method)) {
29+
return $this->_client->{$method}(...$args);
30+
}
31+
32+
if (\in_array($method, ['post', 'get', 'put'])) {
33+
$argsCount = \count($args);
34+
if ($argsCount < 2) {
35+
throw new ArgumentCountError('Too few arguments to function ' . esc_html($method) . ' passed ' . $argsCount . ', At least 2 expected');
36+
}
37+
38+
$url = $args[0];
39+
unset($args[0]);
40+
41+
return $this->_client->request($url, $method, ...$args);
42+
}
43+
44+
throw new BadMethodCallException(esc_html($method) . ' method not exists in ' . __CLASS__);
45+
}
46+
}

src/Http/Client/HttpClient.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,11 @@ public function __call($method, $params)
5151
$url = $url . '?' . $query;
5252
}
5353

54-
$type = strtoupper($method);
5554
$data = $this->getPreparedPayload();
5655
$headers = $this->getHeaders();
5756
$options = $this->getOptions();
5857

59-
return $this->request($url, $type, $data, $headers, $options);
58+
return $this->request($url, $method, $data, $headers, $options);
6059
}
6160

6261
throw new BadMethodCallException($method . ' Method not found in ' . __CLASS__);
@@ -216,7 +215,7 @@ public function getBody()
216215
public function request($url, $type, $data, $headers = null, $options = null)
217216
{
218217
$defaultOptions = [
219-
'method' => $type,
218+
'method' => strtoupper($type),
220219
'headers' => empty($headers) ? $this->getHeaders() : $headers,
221220
'body' => $data,
222221
'timeout' => 30,

src/Http/Request/Request.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ protected function setQueryParams($params = [])
215215
if (!empty($params)) {
216216
$this->queryParams = $params;
217217
} elseif (isset($_GET)) {
218-
$this->queryParams = $_GET;
218+
$this->queryParams = wp_unslash($_GET);
219219
}
220220
}
221221

@@ -244,7 +244,7 @@ protected function setBody($body = [])
244244
}
245245

246246
if (!empty($_POST)) {
247-
$this->body = (array) $this->body + (array) $_POST;
247+
$this->body = (array) $this->body + (array) wp_unslash($_POST);
248248
}
249249
}
250250
}

0 commit comments

Comments
 (0)