@@ -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 |
@@ -13,7 +13,7 @@ |
||
13 | 13 | */ |
14 | 14 | public function up() |
15 | 15 | { |
16 | - Schema::create('exchange_rates', function (Blueprint $table) { |
|
16 | + Schema::create('exchange_rates', function(Blueprint $table) { |
|
17 | 17 | $table->date('date')->unique(); |
18 | 18 | $table->json('rates'); |
19 | 19 | }); |
@@ -22,12 +22,12 @@ discard block |
||
22 | 22 | $begin = new DateTime($this->argument('date') ?? date('Y-m-d')); |
23 | 23 | $end = new DateTime($this->argument('endDate') ?? date('Y-m-d')); |
24 | 24 | |
25 | - if($begin != $end) { |
|
26 | - $period = new DatePeriod($begin, DateInterval::createFromDateString('1 day'), $end); |
|
27 | - foreach ($period as $dt){ |
|
25 | + if ($begin != $end) { |
|
26 | + $period = new DatePeriod($begin, DateInterval::createFromDateString('1 day'), $end); |
|
27 | + foreach ($period as $dt) { |
|
28 | 28 | $this->syncRates($dt); |
29 | 29 | } |
30 | - }else{ |
|
30 | + }else { |
|
31 | 31 | $this->syncRates($begin); |
32 | 32 | } |
33 | 33 | ExchangeRate::insertOrIgnore($this->rates); |
@@ -36,10 +36,10 @@ discard block |
||
36 | 36 | private function syncRates(DateTime $dt) : void |
37 | 37 | { |
38 | 38 | $rates = Tcmb::fetchRates($dt); |
39 | - if($rates){ |
|
39 | + if ($rates) { |
|
40 | 40 | $this->info($dt->format('d.m.Y') . ' synced'); |
41 | 41 | array_push($this->rates, ['date' => $dt->format('Y-m-d'), 'rates' => json_encode($rates)]); |
42 | - }else{ |
|
42 | + }else { |
|
43 | 43 | $this->error($dt->format('d.m.Y') . ' not synced'); |
44 | 44 | } |
45 | 45 | } |
@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | foreach ($period as $dt){ |
28 | 28 | $this->syncRates($dt); |
29 | 29 | } |
30 | - }else{ |
|
30 | + } else{ |
|
31 | 31 | $this->syncRates($begin); |
32 | 32 | } |
33 | 33 | ExchangeRate::insertOrIgnore($this->rates); |
@@ -39,7 +39,7 @@ discard block |
||
39 | 39 | if($rates){ |
40 | 40 | $this->info($dt->format('d.m.Y') . ' synced'); |
41 | 41 | array_push($this->rates, ['date' => $dt->format('Y-m-d'), 'rates' => json_encode($rates)]); |
42 | - }else{ |
|
42 | + } else{ |
|
43 | 43 | $this->error($dt->format('d.m.Y') . ' not synced'); |
44 | 44 | } |
45 | 45 | } |
@@ -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 | } |
@@ -65,11 +65,11 @@ |
||
65 | 65 | if ($rates && array_key_exists($to, $rates) && array_key_exists($from, $rates) && $rates[$to]['buy'] > 0 && $rates[$from]['sell'] > 0) { |
66 | 66 | $base = 'TRY'; |
67 | 67 | if ($from === $base) { |
68 | - $amount = $amount / (float)$rates[$to]['buy']; |
|
68 | + $amount = $amount/(float)$rates[$to]['buy']; |
|
69 | 69 | } elseif ($to === $base) { |
70 | - $amount = $amount * (float)$rates[$from]['sell']; |
|
71 | - } else { |
|
72 | - $amount = $amount * (float)$rates[$from]['sell'] / (float)$rates[$to]['buy']; |
|
70 | + $amount = $amount*(float)$rates[$from]['sell']; |
|
71 | + }else { |
|
72 | + $amount = $amount*(float)$rates[$from]['sell']/(float)$rates[$to]['buy']; |
|
73 | 73 | } |
74 | 74 | } |
75 | 75 | } |
@@ -9,17 +9,17 @@ discard block |
||
9 | 9 | { |
10 | 10 | public function boot() |
11 | 11 | { |
12 | - $this->loadMigrationsFrom(__DIR__.'/../database/migrations'); |
|
13 | - $this->loadRoutesFrom(__DIR__.'/routes.php'); |
|
12 | + $this->loadMigrationsFrom(__DIR__ . '/../database/migrations'); |
|
13 | + $this->loadRoutesFrom(__DIR__ . '/routes.php'); |
|
14 | 14 | |
15 | 15 | if ($this->app->runningInConsole()) { |
16 | 16 | |
17 | 17 | $this->publishes([ |
18 | - __DIR__.'/../config/config.php' => config_path('tcmb.php'), |
|
18 | + __DIR__ . '/../config/config.php' => config_path('tcmb.php'), |
|
19 | 19 | ], 'config'); |
20 | 20 | |
21 | 21 | $this->publishes([ |
22 | - __DIR__.'/../database/migrations/' => database_path('migrations') |
|
22 | + __DIR__ . '/../database/migrations/' => database_path('migrations') |
|
23 | 23 | ], 'migrations'); |
24 | 24 | |
25 | 25 | $this->commands([ |
@@ -30,8 +30,8 @@ discard block |
||
30 | 30 | |
31 | 31 | public function register() |
32 | 32 | { |
33 | - $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'tcmb'); |
|
34 | - $this->app->singleton('tcmb', function () { |
|
33 | + $this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'tcmb'); |
|
34 | + $this->app->singleton('tcmb', function() { |
|
35 | 35 | return new Tcmb; |
36 | 36 | }); |
37 | 37 | } |