1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CryptoMarkets\Common; |
4
|
|
|
|
5
|
|
|
use Http\Client\HttpClient; |
6
|
|
|
use Http\Message\RequestFactory; |
7
|
|
|
use Http\Discovery\HttpClientDiscovery; |
8
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
9
|
|
|
|
10
|
|
|
class Client implements RequestFactory |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The implemented HTTP client instance. |
14
|
|
|
* |
15
|
|
|
* @var \Http\Client\HttpClient |
16
|
|
|
*/ |
17
|
|
|
private $httpClient; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var RequestFactory |
21
|
|
|
*/ |
22
|
|
|
private $requestFactory; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Create a new Client instance. |
26
|
|
|
* |
27
|
|
|
* @param \Http\Client\HttpClient|null $httpClient |
28
|
|
|
* @param \Http\Message\RequestFactory|null $requestFactory |
29
|
|
|
* @return void |
|
|
|
|
30
|
|
|
*/ |
31
|
|
|
public function __construct(HttpClient $httpClient = null, RequestFactory $requestFactory = null) |
32
|
|
|
{ |
33
|
|
|
$this->httpClient = $httpClient ?: HttpClientDiscovery::find(); |
34
|
|
|
$this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Creates a new PSR-7 request. |
39
|
|
|
* |
40
|
|
|
* @param string $method |
41
|
|
|
* @param mixed $uri |
42
|
|
|
* @param array $headers |
43
|
|
|
* @param mixed $body |
44
|
|
|
* @param string $protocol |
45
|
|
|
* @return \Psr\Http\Message\RequestInterface |
46
|
|
|
*/ |
47
|
|
|
public function createRequest($method, $uri, array $headers = [], $body = null, $protocol = '1.1') |
48
|
|
|
{ |
49
|
|
|
if (is_array($body)) { |
50
|
|
|
$body = http_build_query($body, '', '&'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ($method == 'GET') { |
54
|
|
|
$uri .= ((strpos('?', $uri) === false) ? '?' : '&').$body; |
55
|
|
|
$body = ''; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $this->requestFactory->createRequest($method, $uri, $headers, $body, $protocol); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Dynamically call the default driver instance. |
63
|
|
|
* |
64
|
|
|
* @param string $method |
65
|
|
|
* @param array $parameters |
66
|
|
|
* @return mixed |
67
|
|
|
*/ |
68
|
|
|
public function __call($method, $parameters) |
69
|
|
|
{ |
70
|
|
|
return $this->httpClient->{$method}(...$parameters); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.