Skip to content

Commit e19c142

Browse files
authored
Merge pull request #201 from fleetbase/fix/get-currency-from-country-code-return-type
fix: resolve currency code correctly for both list and associative-map shapes
2 parents 62ce9a7 + de1ef41 commit e19c142

2 files changed

Lines changed: 51 additions & 4 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fleetbase/core-api",
3-
"version": "1.6.39",
3+
"version": "1.6.40",
44
"description": "Core Framework and Resources for Fleetbase API",
55
"keywords": [
66
"fleetbase",

src/Support/Utils.php

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,15 +1128,15 @@ public static function getCountryCodeByCurrency(?string $currencyCode, ?string $
11281128
return [
11291129
'name' => static::get($country, 'name.common'),
11301130
'iso2' => static::get($country, 'cca2'),
1131-
'currency' => static::get($country, 'currencies.0'),
1131+
'currency' => static::resolveCurrencyCode(static::get($country, 'currencies', [])),
11321132
];
11331133
})
11341134
->values()
11351135
->toArray();
11361136
$countries = collect($countries);
11371137

11381138
$data = $countries->first(function ($country) use ($currencyCode) {
1139-
return strtolower($country['currency']) === strtolower($currencyCode);
1139+
return is_string($country['currency']) && strtolower($country['currency']) === strtolower($currencyCode);
11401140
});
11411141

11421142
return static::get($data, 'iso2', $defaultValue);
@@ -1199,7 +1199,7 @@ public static function getCountryData(?string $country): ?array
11991199
'aliases' => static::get($country, 'alt_spellings', []),
12001200
'capital' => static::get($country, 'capital_rinvex'),
12011201
'geo' => static::get($country, 'geo'),
1202-
'currency' => Arr::first(static::get($country, 'currencies', [])),
1202+
'currency' => static::resolveCurrencyCode(static::get($country, 'currencies', [])),
12031203
'dial_code' => Arr::first(static::get($country, 'calling_codes', [])),
12041204
'coordinates' => [
12051205
'longitude' => $longitude,
@@ -1217,6 +1217,53 @@ public static function getCountryData(?string $country): ?array
12171217
return $data ?? null;
12181218
}
12191219

1220+
/**
1221+
* Resolve a currency code string from a currencies value returned by the PragmaRX Countries package.
1222+
*
1223+
* The package returns currencies in two distinct shapes depending on the data source:
1224+
* - A sequential list of ISO 4217 code strings, e.g. ["USD", "EUR"]
1225+
* - An associative map keyed by ISO 4217 code, e.g. {"USD": {"name": "...", "symbol": "..."}}
1226+
*
1227+
* When the value is a list, Arr::first() correctly returns the first code string.
1228+
* When the value is a map, Arr::first() returns the first *value* (an array), not the key.
1229+
* This method normalises both shapes and always returns the first currency code as a string,
1230+
* or null when no currencies are present.
1231+
*
1232+
* @param mixed $currencies the raw currencies value from the countries package
1233+
*
1234+
* @return string|null the first ISO 4217 currency code, or null if unavailable
1235+
*/
1236+
public static function resolveCurrencyCode($currencies): ?string
1237+
{
1238+
if (empty($currencies)) {
1239+
return null;
1240+
}
1241+
1242+
// Convert Coollection / Eloquent Collection objects to a plain array so that
1243+
// both array_is_list() and array_key_first() work reliably.
1244+
if (is_object($currencies) && method_exists($currencies, 'toArray')) {
1245+
$currencies = $currencies->toArray();
1246+
}
1247+
1248+
if (!is_array($currencies)) {
1249+
return null;
1250+
}
1251+
1252+
// Sequential list shape: ["USD", "EUR", ...]
1253+
// The first element is already the ISO 4217 code string.
1254+
if (array_is_list($currencies)) {
1255+
$code = Arr::first($currencies);
1256+
1257+
return is_string($code) ? $code : null;
1258+
}
1259+
1260+
// Associative map shape: {"USD": {"name": "...", "symbol": "..."}, ...}
1261+
// The keys are the ISO 4217 code strings; the values are detail objects.
1262+
$code = array_key_first($currencies);
1263+
1264+
return is_string($code) ? $code : null;
1265+
}
1266+
12201267
/**
12211268
* Retrieve currency from given ISO country code.
12221269
*

0 commit comments

Comments
 (0)