This repository has been archived by the owner on Jan 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 198
/
Client.php
803 lines (672 loc) · 23.6 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
<?php
namespace Coinbase\Wallet;
use Coinbase\Wallet\ActiveRecord\ActiveRecordContext;
use Coinbase\Wallet\Enum\Param;
use Coinbase\Wallet\Resource\Account;
use Coinbase\Wallet\Resource\Address;
use Coinbase\Wallet\Resource\Buy;
use Coinbase\Wallet\Resource\Checkout;
use Coinbase\Wallet\Resource\CurrentUser;
use Coinbase\Wallet\Resource\Deposit;
use Coinbase\Wallet\Resource\Merchant;
use Coinbase\Wallet\Resource\Order;
use Coinbase\Wallet\Resource\PaymentMethod;
use Coinbase\Wallet\Resource\Resource;
use Coinbase\Wallet\Resource\ResourceCollection;
use Coinbase\Wallet\Resource\Sell;
use Coinbase\Wallet\Resource\Transaction;
use Coinbase\Wallet\Resource\User;
use Coinbase\Wallet\Resource\Withdrawal;
/**
* A client for interacting with the Coinbase API.
*
* All methods marked as supporting pagination parameters support the following
* parameters:
*
* * limit (integer)
* * order (string)
* * starting_after (string)
* * ending_before (string)
* * fetch_all (Boolean)
*
* @link https://developers.coinbase.com/api/v2
*/
class Client
{
const VERSION = '2.2.0';
private $http;
private $mapper;
/**
* Creates a new Coinbase client.
*
* @return Client A new Coinbase client
*/
public static function create(Configuration $configuration)
{
return new static(
$configuration->createHttpClient(),
$configuration->createMapper()
);
}
public function __construct(HttpClient $http, Mapper $mapper)
{
$this->http = $http;
$this->mapper = $mapper;
}
public function getHttpClient()
{
return $this->http;
}
public function getMapper()
{
return $this->mapper;
}
/** @return array|null */
public function decodeLastResponse()
{
if ($response = $this->http->getLastResponse()) {
return $this->mapper->decode($response);
}
}
/**
* Enables active record methods on resource objects.
*/
public function enableActiveRecord()
{
ActiveRecordContext::setClient($this);
}
// data api
public function getCurrencies(array $params = [])
{
return $this->getAndMapData('/v2/currencies', $params);
}
public function getExchangeRates($currency = null, array $params = [])
{
if ($currency) {
$params['currency'] = $currency;
}
return $this->getAndMapData('/v2/exchange-rates', $params);
}
public function getBuyPrice($currency = null, array $params = [])
{
if ($currency) {
$params['currency'] = $currency;
}
return $this->getAndMapMoney('/v2/prices/buy', $params);
}
public function getSellPrice($currency = null, array $params = [])
{
if ($currency) {
$params['currency'] = $currency;
}
return $this->getAndMapMoney('/v2/prices/sell', $params);
}
public function getSpotPrice($currency = null, array $params = [])
{
if ($currency) {
$params['currency'] = $currency;
}
return $this->getAndMapMoney('/v2/prices/spot', $params);
}
public function getTime(array $params = [])
{
return $this->getAndMapData('/v2/time', $params);
}
// authentication
public function refreshAuthentication(array $params = [])
{
$this->http->refreshAuthentication($params);
}
public function revokeAuthentication(array $params = [])
{
$this->http->revokeAuthentication($params);
}
// users
/** @return User */
public function getUser($userId, array $params = [])
{
return $this->getAndMap('/v2/users/'.$userId, $params, 'toUser');
}
public function refreshUser(User $user, array $params = [])
{
$this->getAndMap($user->getResourcePath(), $params, 'toUser', $user);
}
/** @return CurrentUser */
public function getCurrentUser(array $params = [])
{
return $this->getAndMap('/v2/user', $params, 'toUser', new CurrentUser());
}
public function getCurrentAuthorization(array $params = [])
{
return $this->getAndMapData('/v2/user/auth', $params);
}
public function updateCurrentUser(CurrentUser $user, array $params = [])
{
$data = $this->mapper->fromCurrentUser($user);
$response = $this->http->put('/v2/user', $data + $params);
$this->mapper->toUser($response, $user);
}
// accounts
/**
* Lists the current user's accounts.
*
* Supports pagination parameters.
*
* @return ResourceCollection|Account[]
*/
public function getAccounts(array $params = [])
{
return $this->getAndMapCollection('/v2/accounts', $params, 'toAccounts');
}
public function loadNextAccounts(ResourceCollection $accounts, array $params = [])
{
$this->loadNext($accounts, $params, 'toAccounts');
}
/** @return Account */
public function getAccount($accountId, array $params = [])
{
return $this->getAndMap('/v2/accounts/'.$accountId, $params, 'toAccount');
}
public function refreshAccount(Account $account, array $params = [])
{
$this->getAndMap($account->getResourcePath(), $params, 'toAccount', $account);
}
public function createAccount(Account $account, array $params = [])
{
$data = $this->mapper->fromAccount($account);
$this->postAndMap('/v2/accounts', $data + $params, 'toAccount', $account);
}
/** @return Account */
public function getPrimaryAccount(array $params = [])
{
return $this->getAndMap('/v2/accounts/primary', $params, 'toAccount');
}
public function setPrimaryAccount(Account $account, array $params = [])
{
$this->postAndMap($account->getResourcePath().'/primary', $params, 'toAccount', $account);
}
public function updateAccount(Account $account, array $params = [])
{
$data = $this->mapper->fromAccount($account);
$response = $this->http->put($account->getResourcePath(), $data + $params);
$this->mapper->toAccount($response, $account);
}
public function deleteAccount(Account $account, array $params = [])
{
$this->http->delete($account->getResourcePath(), $params);
}
// addresses
/**
* Lists addresses for an account.
*
* Supports pagination parameters.
*
* @return ResourceCollection|Address[]
*/
public function getAccountAddresses(Account $account, array $params = [])
{
return $this->getAndMapCollection($account->getResourcePath().'/addresses', $params, 'toAddresses');
}
public function loadNextAddresses(ResourceCollection $addresses, array $params = [])
{
$this->loadNext($addresses, $params, 'toAddresses');
}
/** @return Address */
public function getAccountAddress(Account $account, $addressId, array $params = [])
{
$path = sprintf('%s/addresses/%s', $account->getResourcePath(), $addressId);
return $this->getAndMap($path, $params, 'toAddress');
}
public function refreshAddress(Address $address, array $params = [])
{
$this->getAndMap($address->getResourcePath(), $params, 'toAddress', $address);
}
/**
* Lists transactions for an address.
*
* Supports pagination parameters.
*
* @return ResourceCollection|Transaction[]
*/
public function getAddressTransactions(Address $address, array $params = [])
{
return $this->getAndMapCollection($address->getResourcePath().'/transactions', $params, 'toTransactions');
}
public function loadNextAddressTransactions(ResourceCollection $transactions, array $params = [])
{
$this->loadNextTransactions($transactions, $params);
}
public function createAccountAddress(Account $account, Address $address, array $params = [])
{
$data = $this->mapper->fromAddress($address);
$this->postAndMap($account->getResourcePath().'/addresses', $data + $params, 'toAddress', $address);
}
// transactions
/**
* Lists transactions for an account.
*
* Supports pagination parameters.
*
* @return ResourceCollection|Transaction[]
*/
public function getAccountTransactions(Account $account, array $params = [])
{
return $this->getAndMapCollection($account->getResourcePath().'/transactions', $params, 'toTransactions');
}
public function loadNextTransactions(ResourceCollection $transactions, array $params = [])
{
$this->loadNext($transactions, $params, 'toTransactions');
}
/** @return Transaction */
public function getAccountTransaction(Account $account, $transactionId, array $params = [])
{
$path = sprintf('%s/transactions/%s', $account->getResourcePath(), $transactionId);
return $this->getAndMap($path, $params, 'toTransaction');
}
public function refreshTransaction(Transaction $transaction, array $params = [])
{
$this->getAndMap($transaction->getResourcePath(), $params, 'toTransaction', $transaction);
}
/**
* Creates a new transaction.
*
* Supported parameters include:
*
* * skip_notifications (Boolean)
* * fee (float)
* * idem (string)
*/
public function createAccountTransaction(Account $account, Transaction $transaction, array $params = [])
{
$data = $this->mapper->fromTransaction($transaction);
$this->postAndMap($account->getResourcePath().'/transactions', $data + $params, 'toTransaction', $transaction);
}
public function completeTransaction(Transaction $transaction, array $params = [])
{
$this->http->post($transaction->getResourcePath().'/complete', $params);
}
public function resendTransaction(Transaction $transaction, array $params = [])
{
$this->http->post($transaction->getResourcePath().'/resend', $params);
}
public function cancelTransaction(Transaction $transaction, array $params = [])
{
$this->http->delete($transaction->getResourcePath(), $params);
}
// buys
/**
* Lists buys for an account.
*
* Supports pagination parameters.
*
* @return ResourceCollection|Buy[]
*/
public function getAccountBuys(Account $account, array $params = [])
{
return $this->getAndMapCollection($account->getResourcePath().'/buys', $params, 'toBuys');
}
public function loadNextBuys(ResourceCollection $buys, array $params = [])
{
$this->loadNext($buys, $params, 'toBuys');
}
/** @return Buy */
public function getAccountBuy(Account $account, $buyId, array $params = [])
{
$path = sprintf('%s/buys/%s', $account->getResourcePath(), $buyId);
return $this->getAndMap($path, $params, 'toBuy');
}
public function refreshBuy(Buy $buy, array $params = [])
{
$this->getAndMap($buy->getResourcePath(), $params, 'toBuy', $buy);
}
/**
* Buys some amount of bitcoin.
*
* Supported parameters include:
*
* * agree_btc_amount_varies (Boolean)
* * commit (Boolean)
* * quote (Boolean)
*/
public function createAccountBuy(Account $account, Buy $buy, array $params = [])
{
$data = $this->mapper->fromBuy($buy);
$this->postAndMap($account->getResourcePath().'/buys', $data + $params, 'toBuy', $buy);
}
public function commitBuy(Buy $buy, array $params = [])
{
$this->postAndMap($buy->getResourcePath().'/commit', $params, 'toBuy', $buy);
}
// sells
/**
* Lists sells for an account.
*
* Supports pagination parameters.
*
* @return ResourceCollection|Sell[]
*/
public function getSells(Account $account, array $params = [])
{
return $this->getAndMapCollection($account->getResourcePath().'/sells', $params, 'toSells');
}
public function loadNextSells(ResourceCollection $sells, array $params = [])
{
$this->loadNext($sells, $params, 'toSells');
}
/** @return Sell */
public function getAccountSell(Account $account, $sellId, array $params = [])
{
$path = sprintf('%s/sells/%s', $account->getResourcePath(), $sellId);
return $this->getAndMap($path, $params, 'toSell');
}
public function refreshSell(Sell $sell, array $params = [])
{
$this->getAndMap($sell->getResourcePath(), $params, 'toSell', $sell);
}
/**
* Sells some amount of bitcoin.
*
* Supported parameters include:
*
* * agree_btc_amount_varies (Boolean)
* * commit (Boolean)
* * quote (Boolean)
*/
public function createAccountSell(Account $account, Sell $sell, array $params = [])
{
$data = $this->mapper->fromSell($sell);
$this->postAndMap($account->getResourcePath().'/sells', $data + $params, 'toSell', $sell);
}
public function commitSell(Sell $sell, array $params = [])
{
$this->postAndMap($sell->getResourcePath().'/commit', $params, 'toSell', $sell);
}
// deposits
/**
* Lists deposits for an account.
*
* Supports pagination parameters.
*
* @return ResourceCollection|Deposit[]
*/
public function getAccountDeposits(Account $account, array $params = [])
{
return $this->getAndMapCollection($account->getResourcePath().'/deposits', $params, 'toDeposits');
}
public function loadNextDeposits(ResourceCollection $deposits, array $params = [])
{
$this->loadNext($deposits, $params, 'toDeposits');
}
/** @return Deposit */
public function getAccountDeposit(Account $account, $depositId, array $params = [])
{
$path = sprintf('%s/deposits/%s', $account->getResourcePath(), $depositId);
return $this->getAndMap($path, $params, 'toDeposit');
}
public function refreshDeposit(Deposit $deposit, array $params = [])
{
$this->getAndMap($deposit->getResourcePath(), $params, 'toDeposit', $deposit);
}
/**
* Deposits some amount of funds.
*
* Supported parameters include:
*
* * commit (Boolean)
*/
public function createAccountDeposit(Account $account, Deposit $deposit, array $params = [])
{
$data = $this->mapper->fromDeposit($deposit);
$this->postAndMap($account->getResourcePath().'/deposits', $data + $params, 'toDeposit', $deposit);
}
public function commitDeposit(Deposit $deposit, array $params = [])
{
$this->postAndMap($deposit->getResourcePath().'/commit', $params, 'toDeposit', $deposit);
}
// withdrawals
/**
* Lists withdrawals for an account.
*
* Supports pagination parameters.
*
* @return ResourceCollection|Withdrawal[]
*/
public function getAccountWithdrawals(Account $account, array $params = [])
{
return $this->getAndMapCollection($account->getResourcePath().'/withdrawals', $params, 'toWithdrawals');
}
public function loadNextWithdrawals(ResourceCollection $withdrawals, array $params = [])
{
$this->loadNext($withdrawals, $params, 'toWithdrawals');
}
/** @return Withdrawal */
public function getAccountWithdrawal(Account $account, $withdrawalId, array $params = [])
{
$path = sprintf('%s/withdrawals/%s', $account->getResourcePath(), $withdrawalId);
return $this->getAndMap($path, $params, 'toWithdrawal');
}
public function refreshWithdrawal(Withdrawal $withdrawal, array $params = [])
{
$this->getAndMap($withdrawal->getResourcePath(), $params, 'toWithdrawal', $withdrawal);
}
/**
* Withdraws some amount of funds.
*
* Supported parameters include:
*
* * commit (Boolean)
*/
public function createAccountWithdrawal(Account $account, Withdrawal $withdrawal, array $params = [])
{
$data = $this->mapper->fromWithdrawal($withdrawal);
$this->postAndMap($account->getResourcePath().'/withdrawals', $data + $params, 'toWithdrawal', $withdrawal);
}
public function commitWithdrawal(Withdrawal $withdrawal, array $params = [])
{
$this->postAndMap($withdrawal->getResourcePath().'/commit', $params, 'toWithdrawal', $withdrawal);
}
// payment methods
/**
* Lists payment methods for the current user.
*
* Supports pagination parameters.
*
* @return ResourceCollection|PaymentMethod[]
*/
public function getPaymentMethods(array $params = [])
{
return $this->getAndMapCollection('/v2/payment-methods', $params, 'toPaymentMethods');
}
public function loadNextPaymentMethods(ResourceCollection $paymentMethods, array $params = [])
{
$this->loadNext($paymentMethods, $params, 'toPaymentMethods');
}
/** @return PaymentMethod */
public function getPaymentMethod($paymentMethodId, array $params = [])
{
return $this->getAndMap('/v2/payment-methods/'.$paymentMethodId, $params, 'toPaymentMethod');
}
public function refreshPaymentMethod(PaymentMethod $paymentMethod, array $params = [])
{
$this->getAndMap($paymentMethod->getResourcePath(), $params, 'toPaymentMethod', $paymentMethod);
}
// merchant api
/** @return Merchant */
public function getMerchant($merchantId, array $params = [])
{
return $this->getAndMap('/v2/merchants/'.$merchantId, $params, 'toMerchant');
}
public function refreshMerchant(Merchant $merchant, array $params = [])
{
$this->getAndMap($merchant->getResourcePath(), $params, 'toMerchant', $merchant);
}
/**
* Lists orders for the current user.
*
* Supports pagination parameters.
*
* @return ResourceCollection|Order[]
*/
public function getOrders(array $params = [])
{
return $this->getAndMapCollection('/v2/orders', $params, 'toOrders');
}
public function loadNextOrders(ResourceCollection $orders, array $params = [])
{
$this->loadNext($orders, $params, 'toOrders');
}
/** @return Order */
public function getOrder($orderId, array $params = [])
{
return $this->getAndMap('/v2/orders/'.$orderId, $params, 'toOrder');
}
public function refreshOrder(Order $order, array $params = [])
{
$this->getAndMap($order->getResourcePath(), $params, 'toOrder', $order);
}
public function createOrder(Order $order, array $params = [])
{
$data = $this->mapper->fromOrder($order);
$this->postAndMap('/v2/orders', $data + $params, 'toOrder', $order);
}
/**
* Refunds an order.
*
* Supported parameters include:
*
* * mispayment (string)
* * refund_address (string)
*/
public function refundOrder(Order $order, $currency, array $params = [])
{
$params['currency'] = $currency;
$this->postAndMap($order->getResourcePath().'/refund', $params, 'toOrder', $order);
}
/**
* Lists checkouts for the current user.
*
* Supports pagination parameters.
*
* @return ResourceCollection|Checkout[]
*/
public function getCheckouts(array $params = [])
{
return $this->getAndMapCollection('/v2/checkouts', $params, 'toCheckouts');
}
public function loadNextCheckouts(ResourceCollection $checkouts, array $params = [])
{
$this->loadNext($checkouts, $params, 'toCheckouts');
}
/** @return Checkout */
public function getCheckout($checkoutId, array $params = [])
{
return $this->getAndMap('/v2/checkouts/'.$checkoutId, $params, 'toCheckout');
}
public function refreshCheckout(Checkout $checkout, array $params = [])
{
$this->getAndMap($checkout->getResourcePath(), $params, 'toCheckout', $checkout);
}
public function createCheckout(Checkout $checkout, array $params = [])
{
$data = $this->mapper->fromCheckout($checkout);
$this->postAndMap('/v2/checkouts', $data + $params, 'toCheckout', $checkout);
}
/**
* Verifies the authenticity of a merchant callback from Coinbase
*
* @return Boolean
*/
public function verifyCallback($body, $signature)
{
$signature_buffer = base64_decode( $signature );
return (1 == openssl_verify($body, $signature_buffer, self::getCallbackPublicKey(), OPENSSL_ALGO_SHA256));
}
/**
* Return the PEM encoded public RSA key for merchant callbacks
*
* @return String
*/
public static function getCallbackPublicKey()
{
$key = <<<EOD
-----BEGIN PUBLIC KEY-----
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA9MsJBuXzFGIh/xkAA9Cy
QdZKRerV+apyOAWY7sEYV/AJg+AX/tW2SHeZj+3OilNYm5DlBi6ZzDboczmENrFn
mUXQsecsR5qjdDWb2qYqBkDkoZP02m9o9UmKObR8coKW4ZBw0hEf3fP9OEofG2s7
Z6PReWFyQffnnecwXJoN22qjjsUtNNKOOo7/l+IyGMVmdzJbMWQS4ybaU9r9Ax0J
4QUJSS/S4j4LP+3Z9i2DzIe4+PGa4Nf7fQWLwE45UUp5SmplxBfvEGwYNEsHvmRj
usIy2ZunSO2CjJ/xGGn9+/57W7/SNVzk/DlDWLaN27hUFLEINlWXeYLBPjw5GGWp
ieXGVcTaFSLBWX3JbOJ2o2L4MxinXjTtpiKjem9197QXSVZ/zF1DI8tRipsgZWT2
/UQMqsJoVRXHveY9q9VrCLe97FKAUiohLsskr0USrMCUYvLU9mMw15hwtzZlKY8T
dMH2Ugqv/CPBuYf1Bc7FAsKJwdC504e8kAUgomi4tKuUo25LPZJMTvMTs/9IsRJv
I7ibYmVR3xNsVEpupdFcTJYGzOQBo8orHKPFn1jj31DIIKociCwu6m8ICDgLuMHj
7bUHIlTzPPT7hRPyBQ1KdyvwxbguqpNhqp1hG2sghgMr0M6KMkUEz38JFElsVrpF
4z+EqsFcIZzjkSG16BjjjTkCAwEAAQ==
-----END PUBLIC KEY-----
EOD;
return $key;
}
/**
* Lists orders for a checkout.
*
* Supports pagination parameters.
*
* @return ResourceCollection|Order[]
*/
public function getCheckoutOrders(Checkout $checkout, array $params = [])
{
return $this->getAndMapCollection($checkout->getResourcePath().'/orders', $params, 'toOrders');
}
public function loadNextCheckoutOrders(ResourceCollection $orders, array $params = [])
{
$this->loadNextOrders($orders, $params);
}
/** @return Order */
public function createNewCheckoutOrder(Checkout $checkout, array $params = [])
{
return $this->postAndMap($checkout->getResourcePath().'/orders', $params, 'toOrder');
}
// private
private function getAndMapData($path, array $params = [])
{
$response = $this->http->get($path, $params);
return $this->mapper->toData($response);
}
private function getAndMapMoney($path, array $params = [])
{
$response = $this->http->get($path, $params);
return $this->mapper->toMoney($response);
}
/** @return ResourceCollection|Resource[] */
private function getAndMapCollection($path, array $params, $mapperMethod)
{
$fetchAll = isset($params[Param::FETCH_ALL]) ? $params[Param::FETCH_ALL] : false;
unset($params[Param::FETCH_ALL]);
$response = $this->http->get($path, $params);
/** @var ResourceCollection $collection */
$collection = $this->mapper->$mapperMethod($response);
if ($fetchAll) {
while ($collection->hasNextPage()) {
$this->loadNext($collection, $params, $mapperMethod);
}
}
return $collection;
}
/** @return Resource */
private function getAndMap($path, array $params, $mapperMethod, Resource $resource = null)
{
$response = $this->http->get($path, $params);
return $this->mapper->$mapperMethod($response, $resource);
}
private function postAndMap($path, array $params, $mapperMethod, Resource $resource = null)
{
$response = $this->http->post($path, $params);
return $this->mapper->$mapperMethod($response, $resource);
}
private function loadNext(ResourceCollection $collection, array $params, $mapperMethod)
{
$response = $this->http->get($collection->getNextUri(), $params);
$nextPage = $this->mapper->$mapperMethod($response);
$collection->mergeNextPage($nextPage);
}
}