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