1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace IproSoftwareApi\Traits; |
4
|
|
|
|
5
|
|
|
use BadMethodCallException; |
6
|
|
|
use IproSoftwareApi\Exceptions\IproServerException; |
7
|
|
|
use IproSoftwareApi\Exceptions\IproSoftwareApiException; |
8
|
|
|
use IproSoftwareApi\HttpClient; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Trait HasApiMethods. |
13
|
|
|
* |
14
|
|
|
* @method ResponseInterface getSourcesList(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Sources |
15
|
|
|
* @method ResponseInterface getBookingRulesList(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Booking-Rules |
16
|
|
|
* @method ResponseInterface getBookingTagsList(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Booking-Tags |
17
|
|
|
* @method ResponseInterface getLocationsList(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Locations |
18
|
|
|
* @method ResponseInterface getAttributesList(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Attributes |
19
|
|
|
* @method ResponseInterface getContactTypesList(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Contact-Types |
20
|
|
|
* @method ResponseInterface searchContacts(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Contacts |
21
|
|
|
* @method ResponseInterface getContact(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Get-Contact |
22
|
|
|
* @method ResponseInterface getExternalContact(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/External-Contact |
23
|
|
|
* @method ResponseInterface createOrUpdateContact(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Create-Update-Contact |
24
|
|
|
* @method ResponseInterface getPropertiesList(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Properties |
25
|
|
|
* @method ResponseInterface searchProperties(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Property-Search |
26
|
|
|
* @method ResponseInterface getPropertiesReferenceLookupList(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Property-Reference-Lookup |
27
|
|
|
* @method ResponseInterface getPropertyDetails(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Property-Detail |
28
|
|
|
* @method ResponseInterface getPropertyImages(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Property-Images |
29
|
|
|
* @method ResponseInterface getPropertyExtras(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Property-Extras |
30
|
|
|
* @method ResponseInterface getPropertyRates(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Property-Rates |
31
|
|
|
* @method ResponseInterface getPropertyCustomRates(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Property-Custom-Rates |
32
|
|
|
* @method ResponseInterface getPropertyAvailability(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Property-Availabilities |
33
|
|
|
* @method ResponseInterface getPropertyDayAvailability(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Property-Day-Availabilities |
34
|
|
|
* @method ResponseInterface getPropertyBlockouts(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Property-Blockouts |
35
|
|
|
* @method ResponseInterface getPropertyRooms(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Property-Rooms |
36
|
|
|
* @method ResponseInterface getPropertyDistances(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Property-Distances |
37
|
|
|
* @method ResponseInterface getPropertyAll(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Property-All |
38
|
|
|
* @method ResponseInterface getPropertyEnquiries(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Property-Enquiries |
39
|
|
|
* @method ResponseInterface getPropertyWelcomepack(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Property-Welcome-Pack |
40
|
|
|
* @method ResponseInterface createOrUpdateProperty(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Push-Property |
41
|
|
|
* @method ResponseInterface createEnquiry(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Create-Enquiry |
42
|
|
|
* @method ResponseInterface searchBookings(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Bookings |
43
|
|
|
* @method ResponseInterface calculateBooking(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Calculate-Booking |
44
|
|
|
* @method ResponseInterface createBooking(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Create-Booking |
45
|
|
|
* @method ResponseInterface updateBooking(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Update-Booking |
46
|
|
|
* @method ResponseInterface getStatementsByOwner(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Statements |
47
|
|
|
* @method ResponseInterface getReviewsList(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Reviews |
48
|
|
|
* @method ResponseInterface createReview(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Create-Review |
49
|
|
|
* @method ResponseInterface createPayment(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Create-Payment |
50
|
|
|
* @method ResponseInterface getLateDealsList(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Late-Deals |
51
|
|
|
* @method ResponseInterface getSpecialOffersList(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Special-Offers-&-Last-Minute-Deals |
52
|
|
|
* @method ResponseInterface getVouchers(...$options) https://github.com/iprosoftware/api-csharp-client/wiki/Voucher-API---Query-vouchers-to-bring-through-validation-and-price |
53
|
|
|
*/ |
54
|
|
|
trait HasApiMethods |
55
|
|
|
{ |
56
|
|
|
/** @var string Path prefix */ |
57
|
|
|
protected string $pathPrefix = 'apis/'; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Api methods list. |
61
|
|
|
*/ |
62
|
|
|
public static array $methods = [ |
63
|
|
|
/* Settings */ |
64
|
|
|
'getSourcesList' => ['get', 'sources'], |
65
|
|
|
'getBookingRulesList' => ['get', 'bookingrules'], |
66
|
|
|
'getBookingTagsList' => ['get', 'bookingtags'], |
67
|
|
|
'getLocationsList' => ['get', 'locations'], |
68
|
|
|
'getAttributesList' => ['get', 'amenities'], |
69
|
|
|
'getContactTypesList' => ['get', 'contacttypes'], |
70
|
|
|
/* Contacts */ |
71
|
|
|
'searchContacts' => ['get', 'contacts'], |
72
|
|
|
'getContact' => ['get', 'contact/%s'], |
73
|
|
|
'getExternalContact' => ['get', 'externalcontactID'], |
74
|
|
|
'createOrUpdateContact' => ['post', 'contacts'], |
75
|
|
|
/* Properties */ |
76
|
|
|
'getPropertiesList' => ['get', 'properties'], |
77
|
|
|
'searchProperties' => ['get', 'propertysearch'], |
78
|
|
|
'searchLiteProperties' => ['get', 'propertysearchlite'], |
79
|
|
|
'getPropertiesReferenceLookupList' => ['get', 'properties/reflookup'], |
80
|
|
|
'getPropertyDayAvailabilityCheck' => ['get', 'property/dayavailabilitycheck'], |
81
|
|
|
'getPropertyDetails' => ['get', 'property/%s'], |
82
|
|
|
'getPropertyImages' => ['get', 'property/%s/images'], |
83
|
|
|
'getPropertyExtras' => ['get', 'property/%s/extras'], |
84
|
|
|
'getPropertyRates' => ['get', 'property/%s/rates'], |
85
|
|
|
'getPropertyCustomRates' => ['get', 'property/%s/customrates'], |
86
|
|
|
'getPropertyAvailability' => ['get', 'property/%s/availability'], |
87
|
|
|
'getPropertyDayAvailability' => ['get', 'property/%s/dayavailability'], |
88
|
|
|
'getPropertyBlockouts' => ['get', 'property/%s/blockouts'], |
89
|
|
|
'getPropertyRooms' => ['get', 'property/%s/rooms'], |
90
|
|
|
'getPropertyDistances' => ['get', 'property/%s/distances'], |
91
|
|
|
'getPropertyAll' => ['get', 'property/%s/all'], |
92
|
|
|
'getPropertyEnquiries' => ['get', 'property/%s/enquiries'], |
93
|
|
|
'getPropertyWelcomepack' => ['get', 'property/%s/welcomepack'], |
94
|
|
|
'createOrUpdateProperty' => ['post', 'property'], |
95
|
|
|
/* Enquires */ |
96
|
|
|
'createEnquiry' => ['post', 'enquiry'], |
97
|
|
|
/* Bookings */ |
98
|
|
|
'searchBookings' => ['get', 'bookings'], |
99
|
|
|
'calculateBooking' => ['post', 'booking/calc'], |
100
|
|
|
'createBooking' => ['post', 'booking'], |
101
|
|
|
'updateBooking' => ['post', 'booking/update'], |
102
|
|
|
'getStatementsByOwner' => ['get', 'statements'], |
103
|
|
|
/* Reviews */ |
104
|
|
|
'getReviewsList' => ['get', 'reviews'], |
105
|
|
|
'createReview' => ['post', 'reviews'], |
106
|
|
|
/* Payments */ |
107
|
|
|
'createPayment' => ['post', 'payments'], |
108
|
|
|
/* Offers & Deals */ |
109
|
|
|
'getLateDealsList' => ['get', 'latedeals'], |
110
|
|
|
'getSpecialOffersList' => ['get', 'specialoffers'], |
111
|
|
|
'getVouchers' => ['get', 'vouchers'], |
112
|
|
|
]; |
113
|
|
|
|
114
|
|
|
abstract public function httpClient(): ?\IproSoftwareApi\Contracts\HttpClient; |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Attempts to handle api method call. |
118
|
|
|
* |
119
|
|
|
* @param string $method |
120
|
|
|
* @param array $parameters |
121
|
|
|
* |
122
|
|
|
* @return object |
123
|
|
|
* @throws IproSoftwareApiException |
124
|
|
|
* @throws BadMethodCallException |
125
|
|
|
*/ |
126
|
4 |
|
public function __call($method, $parameters) |
127
|
|
|
{ |
128
|
4 |
|
if ($signature = $this->getMethodData($method)) { |
129
|
3 |
|
if (is_null($this->httpClient())) { |
130
|
1 |
|
throw new IproSoftwareApiException('Please specify HttpClient or pass credentials to client constructor', 500); |
131
|
|
|
} |
132
|
2 |
|
$pathTemplate = $this->getPathPrefix() . $signature[1]; |
133
|
2 |
|
preg_match_all('/\%/', $pathTemplate, $replacements); |
134
|
2 |
|
$replacementCount = isset($replacements[0]) ? count($replacements[0]) : 0; |
135
|
2 |
|
$replacementParams = array_splice($parameters, 0, $replacementCount); |
136
|
2 |
|
array_unshift($replacementParams, $pathTemplate); |
137
|
2 |
|
$path = call_user_func_array('sprintf', $replacementParams); |
138
|
2 |
|
array_unshift($parameters, $path); |
139
|
|
|
|
140
|
|
|
try { |
141
|
2 |
|
$response = call_user_func_array([$this->httpClient(), $signature[0]], $parameters); |
142
|
1 |
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
143
|
1 |
|
throw new IproServerException($e); |
144
|
|
|
} |
145
|
|
|
|
146
|
1 |
|
return $response; |
147
|
|
|
} |
148
|
|
|
|
149
|
1 |
|
throw new BadMethodCallException('Method ' . $method . ' not found on ' . get_class() . '.', 500); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string $pathPrefix |
154
|
|
|
* |
155
|
|
|
* @return static |
156
|
|
|
*/ |
157
|
3 |
|
public function setPathPrefix(string $pathPrefix): static |
158
|
|
|
{ |
159
|
3 |
|
$this->pathPrefix = $pathPrefix; |
160
|
|
|
|
161
|
3 |
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
3 |
|
public function getPathPrefix(): string |
168
|
|
|
{ |
169
|
3 |
|
return $this->pathPrefix; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return array |
174
|
|
|
*/ |
175
|
4 |
|
public static function getMethodsList(): array |
176
|
|
|
{ |
177
|
4 |
|
return static::$methods; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param string $method |
182
|
|
|
*/ |
183
|
1 |
|
public static function removeMethod(string $method): void |
184
|
|
|
{ |
185
|
1 |
|
if (isset(static::$methods[$method])) { |
186
|
1 |
|
unset(static::$methods[$method]); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param array $methods |
192
|
|
|
*/ |
193
|
1 |
|
public static function mergeMethods(array $methods): void |
194
|
|
|
{ |
195
|
1 |
|
static::$methods = array_merge(static::$methods, $methods); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Get method form methods list. |
200
|
|
|
* |
201
|
|
|
* @param $method |
202
|
|
|
* |
203
|
|
|
* @return array|null |
204
|
|
|
*/ |
205
|
4 |
|
protected function getMethodData($method): ?array |
206
|
|
|
{ |
207
|
4 |
|
$validMethod = isset(static::$methods[$method]) |
208
|
4 |
|
&& is_array(static::$methods[$method]) |
209
|
4 |
|
&& count(static::$methods[$method]) >= 2 |
210
|
4 |
|
&& in_array(strtoupper(static::$methods[$method][0]), HttpClient::HTTP_METHODS); |
211
|
|
|
|
212
|
4 |
|
if ($validMethod) { |
213
|
3 |
|
return static::$methods[$method]; |
214
|
|
|
} |
215
|
|
|
|
216
|
1 |
|
return null; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|