1
- # PHP Currency FX
1
+ # PHP Currency FX Library
2
2
3
- A PHP Library handles Currency FX (rates & conversions) with ease. Available for Laravel too.
3
+ [ ![ codecov] ( https://codecov.io/gh/shipsaas/currency-fx/branch/main/graph/badge.svg?token=S1RA9XKU94 )] ( https://codecov.io/gh/shipsaas/currency-fx )
4
+ [ ![ Unit Test (PHP 8.1, 8.2)] ( https://github.com/shipsaas/currency-fx/actions/workflows/unit.yml/badge.svg )] ( https://github.com/shipsaas/currency-fx/actions/workflows/unit.yml )
5
+ [ ![ Integration Test] ( https://github.com/shipsaas/currency-fx/actions/workflows/integration.yml/badge.svg )] ( https://github.com/shipsaas/currency-fx/actions/workflows/integration.yml )
6
+ [ ![ E2E Test] ( https://github.com/shipsaas/currency-fx/actions/workflows/e2e.yml/badge.svg )] ( https://github.com/shipsaas/currency-fx/actions/workflows/e2e.yml )
7
+
8
+ A PHP Library handles Currency FX (rates & conversions) with ease. Battery-included 🔋🔋🔋.
9
+
10
+ Available for Laravel too.
4
11
5
12
Tired of implementing these and integrate with 3rd services? Let's CurrencyFX help you to do that. Covered by Unit Testing & battle-tested!
6
13
@@ -16,14 +23,22 @@ Tired of implementing these and integrate with 3rd services? Let's CurrencyFX he
16
23
17
24
## Dependencies
18
25
- Guzzle for API Requests
26
+ - [ NeverThrow] ( https://github.com/shipsaas/never-throw ) for straightforward OK/Error response.
19
27
20
28
## Usage
21
29
22
30
Simply initialize the class with the required params. And it is ready to use in no time.
23
31
24
32
``` php
25
33
$service = new CurrencyCloudService($host, $loginId, $apiKey);
26
- $rate = $service->getRates('USD', 'SGD'); // 1.4xxx (float)
34
+ $rateResponse = $service->getRates('USD', 'SGD');
35
+
36
+ if (!$rateResponse->isOk()) {
37
+ // failed to get the rate from third party service
38
+ // do something here
39
+ }
40
+
41
+ $rate = $rateResponse->getOkResult()->rate; // float (1.4xxx)
27
42
```
28
43
29
44
## Laravel Integration
@@ -49,20 +64,28 @@ We already defined some ENVs key for you to add 😜.
49
64
### Usage
50
65
51
66
``` php
67
+ use CurrencyFX\Services\CurrencyLayerService;
68
+ use CurrencyFX\Services\ExchangerRatesApiIoService;
69
+
52
70
// global access
53
- app(\CurrencyFX\Services\ CurrencyLayerService::class)->getRates('USD', 'EUR');
71
+ app(CurrencyLayerService::class)->getRates('USD', 'EUR');
54
72
55
73
// DI
56
74
class TransferService
57
75
{
58
76
public function __construct(
59
- private \CurrencyFX\Services\ ExchangerRatesApiIoService $rateService
77
+ private ExchangerRatesApiIoService $rateService
60
78
) {
61
79
}
62
80
63
81
public function transfer(): TransferResult
64
82
{
65
- $rates = $this->rateService->getRates('EUR', 'GBP');
83
+ $rateRes = $this->rateService->getRates('EUR', 'GBP');
84
+ if ($rateRes->isError()) {
85
+ return TransferResult::error(...);
86
+ }
87
+
88
+ $rate = $rateRes->getOkResult()->rate;
66
89
}
67
90
}
68
91
```
0 commit comments