|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Resova; |
|
4
|
|
|
|
|
5
|
|
|
use BadMethodCallException; |
|
6
|
|
|
use ErrorException; |
|
7
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
8
|
|
|
use Resova\Endpoints\Availability; |
|
9
|
|
|
use Resova\Endpoints\Baskets; |
|
10
|
|
|
use Resova\Endpoints\Customers; |
|
11
|
|
|
use Resova\Endpoints\GiftVouchers; |
|
12
|
|
|
use Resova\Endpoints\Items; |
|
13
|
|
|
use Resova\Endpoints\Transactions; |
|
14
|
|
|
use Resova\Endpoints\Webhooks; |
|
15
|
|
|
use Resova\Interfaces\QueryInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @property \Resova\Interfaces\AvailabilityInterface $availability Availability of time slots |
|
19
|
|
|
* @property Baskets $baskets Baskets management |
|
20
|
|
|
* @property Customers $customers Customers management |
|
21
|
|
|
* @property GiftVouchers $gift_vouchers GiftVouchers management |
|
22
|
|
|
* @property Items $items For work with list of items |
|
23
|
|
|
* @property Transactions $transactions Transactions management |
|
24
|
|
|
* @property Webhooks $webhooks Webhooks management |
|
25
|
|
|
* |
|
26
|
|
|
* @method Baskets basket(int $basket_id) |
|
27
|
|
|
* @method Customers customer(int $customer_id) |
|
28
|
|
|
* @method GiftVouchers gift_voucher(int $gift_voucher_id) |
|
29
|
|
|
* @method Items item(int $item_id) |
|
30
|
|
|
* @method Transactions transaction(int $transaction_id) |
|
31
|
|
|
* @method Webhooks webhook(int $webhook_id) |
|
32
|
|
|
* |
|
33
|
|
|
* Single entry point for all classes |
|
34
|
|
|
* |
|
35
|
|
|
* @package Resova |
|
36
|
|
|
*/ |
|
37
|
|
|
class Client implements QueryInterface |
|
38
|
|
|
{ |
|
39
|
|
|
use HttpTrait; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $namespace = __NAMESPACE__ . '\\Endpoints'; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Type of query |
|
48
|
|
|
* |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $type; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Endpoint of query |
|
55
|
|
|
* |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $endpoint; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Parameters of query |
|
62
|
|
|
* |
|
63
|
|
|
* @var mixed |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $params; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @var array |
|
69
|
|
|
*/ |
|
70
|
|
|
protected static $variables = []; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* API constructor. |
|
74
|
|
|
* |
|
75
|
|
|
* @param \Resova\Config $config |
|
76
|
|
|
* @param bool $init |
|
77
|
10 |
|
* |
|
78
|
|
|
* @throws ErrorException |
|
79
|
|
|
*/ |
|
80
|
10 |
|
public function __construct(Config $config, bool $init = true) |
|
81
|
1 |
|
{ |
|
82
|
|
|
// Save config into local variable |
|
83
|
|
|
$this->config = $config; |
|
84
|
|
|
|
|
85
|
10 |
|
// Init if need |
|
86
|
9 |
|
if ($init) { |
|
87
|
|
|
$this->client = $this->initClient($config->guzzle()); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
10 |
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get current client instance |
|
93
|
10 |
|
* |
|
94
|
10 |
|
* @return null|\GuzzleHttp\Client |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getClient(): ?\GuzzleHttp\Client |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->client; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Store the client object |
|
103
|
8 |
|
* |
|
104
|
|
|
* @param array $configs |
|
105
|
|
|
* |
|
106
|
8 |
|
* @return \GuzzleHttp\Client |
|
107
|
|
|
*/ |
|
108
|
|
|
public function initClient(array $configs = []): \GuzzleHttp\Client |
|
109
|
|
|
{ |
|
110
|
|
|
return new \GuzzleHttp\Client($configs); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Convert underscore_strings to camelCase (medial capitals). |
|
115
|
|
|
* |
|
116
|
|
|
* @param string $str |
|
117
|
8 |
|
* |
|
118
|
|
|
* @return string |
|
119
|
8 |
|
*/ |
|
120
|
3 |
|
private function snakeToPascal(string $str): string |
|
121
|
|
|
{ |
|
122
|
|
|
// Remove underscores, capitalize words, squash, lowercase first. |
|
123
|
|
|
return str_replace(' ', '', ucwords(str_replace('_', ' ', $str))); |
|
124
|
8 |
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
8 |
|
* Magic method required for call of another classes |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $name |
|
130
|
|
|
* |
|
131
|
|
|
* @return bool|object |
|
132
|
8 |
|
* @throws BadMethodCallException |
|
133
|
|
|
*/ |
|
134
|
|
|
public function __get(string $name) |
|
135
|
|
|
{ |
|
136
|
|
|
if (isset(self::$variables[$name])) { |
|
137
|
|
|
return self::$variables[$name]; |
|
138
|
|
|
} |
|
139
|
8 |
|
|
|
140
|
|
|
// By default return is empty |
|
141
|
|
|
$object = ''; |
|
|
|
|
|
|
142
|
|
|
|
|
143
|
8 |
|
// Set class name as namespace |
|
144
|
|
|
$class = $this->namespace . '\\' . $this->snakeToPascal($name); |
|
145
|
|
|
|
|
146
|
|
|
try { |
|
147
|
|
|
|
|
148
|
|
|
// Try to create object by name |
|
149
|
|
|
$object = new $class($this->config); |
|
150
|
|
|
|
|
151
|
|
|
} catch (ErrorException | ClientException $e) { |
|
152
|
|
|
echo $e->getMessage(); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
7 |
|
// If object is not created |
|
156
|
|
|
if (!is_object($object)) { |
|
157
|
|
|
throw new BadMethodCallException("Class $class could not to be loaded"); |
|
158
|
7 |
|
} |
|
159
|
|
|
|
|
160
|
|
|
return $object; |
|
161
|
7 |
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Magic method required for call of another classes |
|
165
|
|
|
* |
|
166
|
7 |
|
* @param string $name |
|
167
|
|
|
* @param array $arguments |
|
168
|
|
|
* |
|
169
|
|
|
* @return bool|object |
|
170
|
|
|
* @throws BadMethodCallException |
|
171
|
|
|
*/ |
|
172
|
|
|
public function __call(string $name, array $arguments) |
|
173
|
7 |
|
{ |
|
174
|
|
|
// By default return is empty |
|
175
|
|
|
$object = ''; |
|
|
|
|
|
|
176
|
|
|
|
|
177
|
7 |
|
// Set class name as namespace |
|
178
|
|
|
$class = $this->namespace . '\\' . $this->snakeToPascal($name) . 's'; |
|
179
|
|
|
|
|
180
|
|
|
try { |
|
181
|
|
|
|
|
182
|
|
|
// Try to create object by name |
|
183
|
|
|
$object = new $class($this->config); |
|
184
|
|
|
|
|
185
|
|
|
} catch (ErrorException | ClientException $e) { |
|
186
|
1 |
|
echo $e->getMessage(); |
|
187
|
|
|
} |
|
188
|
1 |
|
|
|
189
|
|
|
// If object is not created |
|
190
|
|
|
if (!is_object($object)) { |
|
191
|
|
|
throw new BadMethodCallException("Class $class could not to be loaded"); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
return call_user_func_array($object, $arguments); |
|
|
|
|
|
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
7 |
|
* Check if class is exist in folder |
|
199
|
|
|
* |
|
200
|
7 |
|
* @param string $name |
|
201
|
7 |
|
* |
|
202
|
|
|
* @return bool |
|
203
|
|
|
*/ |
|
204
|
|
|
public function __isset(string $name): bool |
|
205
|
|
|
{ |
|
206
|
|
|
return isset(self::$variables[$name]); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Ordinary dummy setter, it should be ignored (added to PSR reasons) |
|
211
|
|
|
* |
|
212
|
|
|
* @param string $name |
|
213
|
|
|
* @param mixed $value |
|
214
|
|
|
* |
|
215
|
|
|
* @throws BadMethodCallException |
|
216
|
|
|
*/ |
|
217
|
|
|
public function __set(string $name, $value) |
|
218
|
|
|
{ |
|
219
|
|
|
self::$variables[$name] = $value; |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|