1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Orkhanahmadov\LaravelCurrencylayer; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Contracts\Cache\Repository; |
7
|
|
|
use Illuminate\Support\Arr; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
use OceanApplications\currencylayer\client; |
10
|
|
|
use Orkhanahmadov\LaravelCurrencylayer\Models\Currency; |
11
|
|
|
|
12
|
|
|
class Currencylayer |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var client |
16
|
|
|
*/ |
17
|
|
|
private $client; |
18
|
|
|
/** |
19
|
|
|
* @var string|null |
20
|
|
|
*/ |
21
|
|
|
private $date = null; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Currencylayer constructor. |
25
|
|
|
* |
26
|
|
|
* @param client $client |
27
|
|
|
*/ |
28
|
|
|
public function __construct(client $client) |
29
|
|
|
{ |
30
|
|
|
$this->client = $client; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function live($source, ...$currencies) |
34
|
|
|
{ |
35
|
|
|
$currencies = Arr::flatten($currencies); |
36
|
|
|
|
37
|
|
|
$response = $this->apiRate($source, implode(',', $currencies)); |
38
|
|
|
|
39
|
|
|
$rates = []; |
40
|
|
|
$sourceCurrency = Currency::firstOrCreate(['code' => $source]); |
41
|
|
|
foreach ($response['quotes'] as $code => $rate) { |
42
|
|
|
$targetCurrency = Currency::firstOrCreate(['code' => $targetCurrencyCode = substr($code, -3)]); |
43
|
|
|
|
44
|
|
|
$createdRate = $sourceCurrency->rates()->create([ |
45
|
|
|
'target_currency_id' => $targetCurrency->id, |
46
|
|
|
'rate' => $rate, |
47
|
|
|
'timestamp' => $response['timestamp'], |
48
|
|
|
]); |
49
|
|
|
$rates[$targetCurrencyCode] = $createdRate->rate; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return count($currencies) === 1 ? array_values($rates)[0] : $rates; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function apiRate(string $source, string $currencies, ?string $date = null): array |
56
|
|
|
{ |
57
|
|
|
$client = $this->client->source($source)->currencies($currencies); |
58
|
|
|
|
59
|
|
|
return $date ? $client->date($date)->historical() : $client->live(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param Carbon|string $date |
64
|
|
|
* |
65
|
|
|
* @return Currencylayer |
66
|
|
|
*/ |
67
|
|
|
public function date($date): self |
68
|
|
|
{ |
69
|
|
|
$this->date = $date instanceof Carbon ? |
70
|
|
|
$date->format('Y-m-d') : Carbon::parse($date)->format('Y-m-d'); |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// /** |
76
|
|
|
// * @return self |
77
|
|
|
// */ |
78
|
|
|
// public function fetch(): self |
79
|
|
|
// { |
80
|
|
|
// $this->shouldFetch = true; |
81
|
|
|
// return $this; |
82
|
|
|
// } |
83
|
|
|
|
84
|
|
|
// /** |
85
|
|
|
// * @param Currency|string $source |
86
|
|
|
// * @param Currency|string $target |
87
|
|
|
// * @param Carbon|string|null $date |
88
|
|
|
// * |
89
|
|
|
// * @return float |
90
|
|
|
// */ |
91
|
|
|
// public function rate($source, $target, $date = null) |
92
|
|
|
// { |
93
|
|
|
// if (! $source instanceof Currency) { |
94
|
|
|
// $source = Currency::where('code', $source)->first(); |
95
|
|
|
// } |
96
|
|
|
// |
97
|
|
|
// if (! $target instanceof Currency) { |
98
|
|
|
// $target = Currency::where('code', $target)->first(); |
99
|
|
|
// } |
100
|
|
|
// |
101
|
|
|
// if (! $source || ! $target) { |
102
|
|
|
// throw new \InvalidArgumentException( |
103
|
|
|
// 'Source or target currency is not available. Did you fetch all currencies? ' . |
104
|
|
|
// 'Call currencies() method to fetch all available currencies.' |
105
|
|
|
// ); |
106
|
|
|
// } |
107
|
|
|
// |
108
|
|
|
// if (! $date) { |
109
|
|
|
// $this->fetch($source->code, $target->code); |
110
|
|
|
// } |
111
|
|
|
// |
112
|
|
|
// return $source->rate($target)->rate; |
113
|
|
|
// } |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
|
117
|
|
|
// public function currencies() |
118
|
|
|
// { |
119
|
|
|
// $response = $this->client->list(); |
120
|
|
|
// |
121
|
|
|
// foreach ($response['currencies'] as $code => $name) { |
122
|
|
|
// $currency = Currency::where('code', $code)->first(); |
123
|
|
|
// if (! $currency) { |
124
|
|
|
// Currency::create(['code' => $code, 'name' => $name]); |
125
|
|
|
// } |
126
|
|
|
// } |
127
|
|
|
// } |
128
|
|
|
} |
129
|
|
|
|