|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CryptoMarkets\Common; |
|
4
|
|
|
|
|
5
|
|
|
abstract class Gateway implements GatewayInterface |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* The HTTP client instance. |
|
9
|
|
|
* |
|
10
|
|
|
* @var \CryptoMarkets\Common\Client |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $httpClient; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* The gateway's options. |
|
16
|
|
|
* |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $options = []; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Create a new Gateway instance. |
|
23
|
|
|
* |
|
24
|
|
|
* @param array $options |
|
25
|
|
|
* @return void |
|
|
|
|
|
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct(array $options = []) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->setOptions($options); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Merge the custom options with the defaults. |
|
34
|
|
|
* |
|
35
|
|
|
* @param array $options |
|
36
|
|
|
* @return void |
|
37
|
|
|
*/ |
|
38
|
|
|
public function setOptions(array $options = []) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->options = array_replace($this->getDefaultOptions(), $options); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Get the options. |
|
45
|
|
|
* |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getOptions() |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->options; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* et the specified option value (if any). |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $key |
|
57
|
|
|
* @param mixed $default |
|
58
|
|
|
* @return mixed |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getOption($key, $default = null) |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
return $this->options[$key]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get the default options. |
|
67
|
|
|
* |
|
68
|
|
|
* @return array |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getDefaultOptions() |
|
71
|
|
|
{ |
|
72
|
|
|
return []; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Get the gateway name. |
|
77
|
|
|
* |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
|
|
abstract public function getName(); |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Create a new request object. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $class |
|
86
|
|
|
* @param array $params |
|
87
|
|
|
* @return \CryptoMarkets\Common\Http\Request |
|
88
|
|
|
*/ |
|
89
|
|
|
public function createRequest($class, array $params = []) |
|
90
|
|
|
{ |
|
91
|
|
|
$instance = new $class($this->getHttpClient()); |
|
92
|
|
|
|
|
93
|
|
|
return $instance->configure(array_replace($this->getOptions(), $params)); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Get a instance of the HTTP client. |
|
98
|
|
|
* |
|
99
|
|
|
* @return \CryptoMarkets\Common\Client |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getHttpClient() |
|
102
|
|
|
{ |
|
103
|
|
|
if (is_null($this->httpClient)) { |
|
104
|
|
|
$this->setHttpClient(new Client); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $this->httpClient; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Set the HTTP client instance. |
|
112
|
|
|
* |
|
113
|
|
|
* @param \CryptoMarkets\Common\Client $client |
|
114
|
|
|
* @return $this |
|
115
|
|
|
*/ |
|
116
|
|
|
public function setHttpClient(Client $client) |
|
117
|
|
|
{ |
|
118
|
|
|
$this->httpClient = $client; |
|
119
|
|
|
|
|
120
|
|
|
return $this; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
Adding a
@returnannotation 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.