@@ -4,25 +4,25 @@ |
||
4 | 4 | * Available currencies @ https://www.tcmb.gov.tr/wps/wcm/connect/en/tcmb+en/main+page+site+area/today |
5 | 5 | */ |
6 | 6 | return [ |
7 | - 'currencies' => [ |
|
8 | - 'USD', |
|
9 | - 'AUD', |
|
10 | - 'DKK', |
|
11 | - 'EUR', |
|
12 | - 'GBP', |
|
13 | - 'CHF', |
|
14 | - 'SEK', |
|
15 | - 'CAD', |
|
16 | - 'KWD', |
|
17 | - 'NOK', |
|
18 | - 'SAR', |
|
19 | - 'JPY', |
|
20 | - 'BGN', |
|
21 | - 'RON', |
|
22 | - 'RUB', |
|
23 | - 'IRR', |
|
24 | - 'CNY', |
|
25 | - 'PKR', |
|
26 | - 'QAR' |
|
27 | - ] |
|
7 | + 'currencies' => [ |
|
8 | + 'USD', |
|
9 | + 'AUD', |
|
10 | + 'DKK', |
|
11 | + 'EUR', |
|
12 | + 'GBP', |
|
13 | + 'CHF', |
|
14 | + 'SEK', |
|
15 | + 'CAD', |
|
16 | + 'KWD', |
|
17 | + 'NOK', |
|
18 | + 'SAR', |
|
19 | + 'JPY', |
|
20 | + 'BGN', |
|
21 | + 'RON', |
|
22 | + 'RUB', |
|
23 | + 'IRR', |
|
24 | + 'CNY', |
|
25 | + 'PKR', |
|
26 | + 'QAR' |
|
27 | + ] |
|
28 | 28 | ]; |
29 | 29 | \ No newline at end of file |
@@ -11,68 +11,68 @@ |
||
11 | 11 | class Tcmb |
12 | 12 | { |
13 | 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 | - ]; |
|
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 | 34 | |
35 | - public static function fetchRates(DateTime $date) : array |
|
36 | - { |
|
37 | - $rates = []; |
|
38 | - $response = null; |
|
35 | + public static function fetchRates(DateTime $date) : array |
|
36 | + { |
|
37 | + $rates = []; |
|
38 | + $response = null; |
|
39 | 39 | $response = @simplexml_load_file('https://www.tcmb.gov.tr/kurlar/' . $date->format('Ym') . '/' . $date->format('dmY') . '.xml'); |
40 | 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 | - } |
|
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 | 51 | return $rates; |
52 | - } |
|
52 | + } |
|
53 | 53 | |
54 | - public static function convert(float $amount, $from, $to, $date = false, int $decimals = 4) : float |
|
54 | + public static function convert(float $amount, $from, $to, $date = false, int $decimals = 4) : float |
|
55 | 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'); |
|
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 | 60 | |
61 | - if (!$rates) { |
|
62 | - $rates = self::fetchRates($date); |
|
63 | - } |
|
61 | + if (!$rates) { |
|
62 | + $rates = self::fetchRates($date); |
|
63 | + } |
|
64 | 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, '.', ''); |
|
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 | 77 | } |
78 | 78 | } |