FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
unirate-bundle/src/UniRateClient.php at main · UniRate-API/unirate-bundle · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
UniRate-API
/
unirate-bundle
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
unirate-bundle
/
src
/
UniRateClient.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
144 lines (120 loc) · 4.1 KB
Breadcrumbs
unirate-bundle
/
src
/
UniRateClient.php
Copy path
File metadata and controls
144 lines (120 loc) · 4.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
<?php
declare
(strict_types=
1
);
namespace
UniRateApi
\
Bundle
;
use
Symfony
\
Contracts
\
HttpClient
\
HttpClientInterface
;
final
class
UniRateClient
{
public
const
VERSION
=
'
0.1.0
'
;
private
readonly
string
$
baseUrl
;
public
function
__construct
(
private
readonly
string
$
apiKey
,
private
readonly
HttpClientInterface
$
httpClient
,
string
$
baseUrl
=
'
https://api.unirateapi.com
'
,
private
readonly
int
$
timeout
=
30
,
) {
if
(
$
apiKey
===
''
) {
throw
new
UniRateException
(
'
UniRateClient: apiKey must not be empty.
'
);
}
$
this
->
baseUrl
=
rtrim
(
$
baseUrl
,
'
/
'
);
}
/**
* Get the exchange rate for a currency pair.
*
* @return float When $to is specified.
*/
public
function
getRate
(
string
$
from
,
string
$
to
):
float
{
$
data
=
$
this
->
request
(
'
/api/rates
'
, [
'
from
'
=>
strtoupper
(
$
from
),
'
to
'
=>
strtoupper
(
$
to
),
]);
if
(!
isset
(
$
data
[
'
rate
'
])) {
throw
new
UniRateException
(
'
Malformed /api/rates response: missing "rate" key.
'
);
}
return
(
float
)
$
data
[
'
rate
'
];
}
/**
* Get all exchange rates for a base currency.
*
* @return array<string, float>
*/
public
function
getRates
(
string
$
base
=
'
USD
'
):
array
{
$
data
=
$
this
->
request
(
'
/api/rates
'
, [
'
from
'
=>
strtoupper
(
$
base
)]);
$
out
= [];
foreach
((
$
data
[
'
rates
'
] ?? [])
as
$
code
=>
$
value
) {
$
out
[(
string
)
$
code
] = (
float
)
$
value
;
}
return
$
out
;
}
/**
* Convert an amount from one currency to another.
*/
public
function
convert
(
float
$
amount
,
string
$
from
,
string
$
to
):
float
{
$
data
=
$
this
->
request
(
'
/api/convert
'
, [
'
from
'
=>
strtoupper
(
$
from
),
'
to
'
=>
strtoupper
(
$
to
),
'
amount
'
=>
$
amount
,
]);
if
(!
isset
(
$
data
[
'
result
'
])) {
throw
new
UniRateException
(
'
Malformed /api/convert response: missing "result" key.
'
);
}
return
(
float
)
$
data
[
'
result
'
];
}
/**
* List all supported currency codes.
*
* @return list<string>
*/
public
function
listCurrencies
():
array
{
$
data
=
$
this
->
request
(
'
/api/currencies
'
, []);
return
array_values
(
array_map
(
'
strval
'
,
$
data
[
'
currencies
'
] ?? []));
}
/**
* Get VAT rates for all countries or a specific country.
*
* @return array<string, mixed>
*/
public
function
getVatRates
(?
string
$
country
=
null
):
array
{
$
params
= [];
if
(
$
country
!==
null
) {
$
params
[
'
country
'
] =
strtoupper
(
$
country
);
}
return
$
this
->
request
(
'
/api/vat/rates
'
,
$
params
);
}
/**
* @param array<string, mixed> $query
* @return array<string, mixed>
*/
private
function
request
(
string
$
path
,
array
$
query
):
array
{
$
query
[
'
api_key
'
] =
$
this
->
apiKey
;
$
response
=
$
this
->
httpClient
->
request
(
'
GET
'
,
$
this
->
baseUrl
.
$
path
, [
'
query
'
=>
$
query
,
'
timeout
'
=>
$
this
->
timeout
,
'
headers
'
=> [
'
Accept
'
=>
'
application/json
'
,
'
User-Agent
'
=>
'
unirate-bundle/
'
.
self
::
VERSION
,
],
]);
$
status
=
$
response
->
getStatusCode
();
if
(
$
status
>=
400
) {
$
body
=
substr
(
$
response
->
getContent
(
false
),
0
,
300
);
throw
match
(
true
) {
$
status
===
401
=>
new
UniRateException
(
'
Missing or invalid API key.
'
,
$
status
),
$
status
===
403
=>
new
UniRateException
(
'
Endpoint requires a Pro subscription.
'
,
$
status
),
$
status
===
404
=>
new
UniRateException
(
'
Currency not found or endpoint unavailable.
'
,
$
status
),
$
status
===
429
=>
new
UniRateException
(
'
Rate limit exceeded.
'
,
$
status
),
default
=>
new
UniRateException
(
"
API error (HTTP
{
$
status
}
):
{
$
body
}"
,
$
status
),
};
}
$
data
=
$
response
->
toArray
();
if
(!
is_array
(
$
data
)) {
throw
new
UniRateException
(
"
Expected JSON object from
{
$
path
}
.
"
);
}
return
$
data
;
}
}
Back
|
FazBrowse Home
|
New Git URL