RequestProxy.php
1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
namespace Lackoxygen\TiktokShop\Request;
use Lackoxygen\TiktokShop\Attribute\Config\Config;
use Lackoxygen\TiktokShop\Attribute\Config\Option;
use Lackoxygen\TiktokShop\Exception\ClientException;
use Lackoxygen\TiktokShop\Response\ResultSet;
use Lackoxygen\TiktokShop\Support\Json;
class RequestProxy extends Config
{
/**
* @var string
*/
protected string $request;
/**
* @param Option $config
* @param string $request
*/
protected function __construct(Option $config, string $request)
{
parent::__construct($config);
$this->request = $request;
}
/**
* @param string $request
* @param Option $config
* @return RequestProxy
*/
public static function proxy(string $request, Option $config): RequestProxy
{
return new self($config, $request);
}
/**
* @throws ClientException
*/
public function __call($name, $arguments): ResultSet
{
/**
* @var Request $request
*/
$request = new $this->request($this->config, $name);
$result = call_user_func_array([$request, $name], $arguments);
if (is_array($result)) {
return new ResultSet($result);
}
$response = $request();
$result = Json::unmarshal($response->getBody()->getContents());
return new ResultSet($result);
}
}