elvendor /
laravel-tcmb
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Elvendor\Tcmb; |
||
| 6 | |||
| 7 | use Elvendor\Tcmb\Models\ExchangeRate; |
||
| 8 | use DateTime; |
||
| 9 | use Exception; |
||
| 10 | |||
| 11 | class Tcmb |
||
| 12 | { |
||
| 13 | const currencies = [ |
||
| 14 | 'USD' => ['index' => 0, 'unit' => 1], |
||
| 15 | 'AUD' => ['index' => 1, 'unit' => 1], |
||
| 16 | 'DKK' => ['index' => 2, 'unit' => 1], |
||
| 17 | 'EUR' => ['index' => 3, 'unit' => 1], |
||
| 18 | 'GBP' => ['index' => 4, 'unit' => 1], |
||
| 19 | 'CHF' => ['index' => 5, 'unit' => 1], |
||
| 20 | 'SEK' => ['index' => 6, 'unit' => 1], |
||
| 21 | 'CAD' => ['index' => 7, 'unit' => 1], |
||
| 22 | 'KWD' => ['index' => 8, 'unit' => 1], |
||
| 23 | 'NOK' => ['index' => 9, 'unit' => 1], |
||
| 24 | 'SAR' => ['index' => 10, 'unit' => 1], |
||
| 25 | 'JPY' => ['index' => 11, 'unit' => 100], |
||
| 26 | 'BGN' => ['index' => 12, 'unit' => 1], |
||
| 27 | 'RON' => ['index' => 13, 'unit' => 1], |
||
| 28 | 'RUB' => ['index' => 14, 'unit' => 1], |
||
| 29 | 'IRR' => ['index' => 15, 'unit' => 100], |
||
| 30 | 'CNY' => ['index' => 16, 'unit' => 1], |
||
| 31 | 'PKR' => ['index' => 17, 'unit' => 1], |
||
| 32 | 'QAR' => ['index' => 18, 'unit' => 1] |
||
| 33 | ]; |
||
| 34 | |||
| 35 | public static function fetchRates(DateTime $date) : array |
||
| 36 | { |
||
| 37 | $rates = []; |
||
| 38 | $response = null; |
||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 39 | $response = @simplexml_load_file('https://www.tcmb.gov.tr/kurlar/' . $date->format('Ym') . '/' . $date->format('dmY') . '.xml'); |
||
| 40 | if ($response) { |
||
| 41 | $currencies = config('tcmb.currencies'); |
||
| 42 | foreach ($currencies as $currency) { |
||
| 43 | if (array_key_exists($currency, self::currencies)) { |
||
| 44 | $index = self::currencies[$currency]['index']; |
||
| 45 | data_set($rates, "{$currency}.buy", (float)$response->Currency[$index]->ForexBuying); |
||
| 46 | data_set($rates, "{$currency}.sell", (float)$response->Currency[$index]->ForexSelling); |
||
| 47 | } |
||
| 48 | } |
||
| 49 | $response = null; |
||
| 50 | } |
||
| 51 | return $rates; |
||
| 52 | } |
||
| 53 | |||
| 54 | public static function convert(float $amount, $from, $to, $date = false, int $decimals = 4) : float |
||
| 55 | { |
||
| 56 | if ($from !== $to && $amount > 0) { |
||
| 57 | $date = new DateTime($date ?: date('Y-m-d')); |
||
| 58 | $rates = ExchangeRate::actualForDate($date)->orderByDesc('date')->first(); |
||
| 59 | $rates = data_get($rates, 'rates'); |
||
| 60 | |||
| 61 | if (!$rates) { |
||
| 62 | $rates = self::fetchRates($date); |
||
| 63 | } |
||
| 64 | |||
| 65 | if ($rates && array_key_exists($to, $rates) && array_key_exists($from, $rates) && $rates[$to]['buy'] > 0 && $rates[$from]['sell'] > 0) { |
||
| 66 | $base = 'TRY'; |
||
| 67 | if ($from === $base) { |
||
| 68 | $amount = $amount / (float)$rates[$to]['buy']; |
||
| 69 | } elseif ($to === $base) { |
||
| 70 | $amount = $amount * (float)$rates[$from]['sell']; |
||
| 71 | } else { |
||
| 72 | $amount = $amount * (float)$rates[$from]['sell'] / (float)$rates[$to]['buy']; |
||
| 73 | } |
||
| 74 | } |
||
| 75 | } |
||
| 76 | return (float)number_format($amount, $decimals, '.', ''); |
||
| 77 | } |
||
| 78 | } |
||
| 79 |