1
|
|
|
<?php namespace Arcanedev\Currencies; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Currencies\Contracts\CurrencyManager as CurrencyManagerContract; |
4
|
|
|
use Arcanedev\Currencies\Entities\CurrencyCollection; |
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class CurrencyManager |
9
|
|
|
* |
10
|
|
|
* @package Arcanedev\Currencies |
11
|
|
|
* @author ARCANEDEV <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class CurrencyManager implements CurrencyManagerContract |
14
|
|
|
{ |
15
|
|
|
/* ------------------------------------------------------------------------------------------------ |
16
|
|
|
| Properties |
17
|
|
|
| ------------------------------------------------------------------------------------------------ |
18
|
|
|
*/ |
19
|
|
|
/** |
20
|
|
|
* Default currency. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $default; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Supported currencies. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $supported = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Non ISO Currencies included. |
35
|
|
|
* |
36
|
|
|
* @var bool |
37
|
|
|
*/ |
38
|
|
|
protected $nonIsoIncluded = false; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The currencies collection. |
42
|
|
|
* |
43
|
|
|
* @var \Arcanedev\Currencies\Entities\CurrencyCollection |
44
|
|
|
*/ |
45
|
|
|
protected $currencies; |
46
|
|
|
|
47
|
|
|
/* ------------------------------------------------------------------------------------------------ |
48
|
|
|
| Constructor |
49
|
|
|
| ------------------------------------------------------------------------------------------------ |
50
|
|
|
*/ |
51
|
|
|
/** |
52
|
|
|
* CurrencyManager constructor. |
53
|
|
|
* |
54
|
|
|
* @param array $configs |
55
|
|
|
*/ |
56
|
540 |
|
public function __construct(array $configs) |
57
|
|
|
{ |
58
|
540 |
|
$this->default = Arr::get($configs, 'default', 'USD'); |
59
|
540 |
|
$this->supported = Arr::get($configs, 'supported', ['USD']); |
|
|
|
|
60
|
540 |
|
$this->nonIsoIncluded = Arr::get($configs, 'include-non-iso', false); |
61
|
540 |
|
$this->currencies = new CurrencyCollection; |
62
|
540 |
|
} |
63
|
|
|
|
64
|
|
|
/* ------------------------------------------------------------------------------------------------ |
65
|
|
|
| Getters & Setters |
66
|
|
|
| ------------------------------------------------------------------------------------------------ |
67
|
|
|
*/ |
68
|
|
|
/** |
69
|
|
|
* Get the default currency iso code. |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
48 |
|
public function getDefault() |
74
|
|
|
{ |
75
|
48 |
|
return $this->default; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get the default currency entity. |
80
|
|
|
* |
81
|
|
|
* @return \Arcanedev\Currencies\Contracts\Entities\Currency |
82
|
|
|
*/ |
83
|
12 |
|
public function getDefaultCurrency() |
84
|
|
|
{ |
85
|
12 |
|
return $this->currencies->get($this->getDefault()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get supported currencies (iso codes). |
90
|
|
|
* |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
48 |
|
public function getSupported() |
94
|
|
|
{ |
95
|
48 |
|
return $this->supported; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Check if non ISO Currencies included. |
100
|
|
|
* |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
12 |
|
public function isNonIsoIncluded() |
104
|
|
|
{ |
105
|
12 |
|
return $this->nonIsoIncluded; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get the currencies collection. |
110
|
|
|
* |
111
|
|
|
* @return \Arcanedev\Currencies\Entities\CurrencyCollection |
112
|
|
|
*/ |
113
|
540 |
|
public function currencies() |
114
|
|
|
{ |
115
|
540 |
|
return $this->currencies; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/* ------------------------------------------------------------------------------------------------ |
119
|
|
|
| Main Functions |
120
|
|
|
| ------------------------------------------------------------------------------------------------ |
121
|
|
|
*/ |
122
|
|
|
/** |
123
|
|
|
* Load the currencies. |
124
|
|
|
* |
125
|
|
|
* @param array $currencies |
126
|
|
|
* |
127
|
|
|
* @return self |
128
|
|
|
*/ |
129
|
540 |
|
public function load(array $currencies) |
130
|
|
|
{ |
131
|
540 |
|
$this->currencies()->load($currencies, $this->nonIsoIncluded); |
132
|
|
|
|
133
|
540 |
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get a currency from the collection by iso code. |
138
|
|
|
* |
139
|
|
|
* @param string $iso |
140
|
|
|
* @param mixed|null $default |
141
|
|
|
* |
142
|
|
|
* @return \Arcanedev\Currencies\Contracts\Entities\Currency |
143
|
|
|
*/ |
144
|
12 |
|
public function get($iso, $default = null) |
145
|
|
|
{ |
146
|
12 |
|
return $this->currencies()->get($iso, $default); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get a currency or fail if not exists. |
151
|
|
|
* |
152
|
|
|
* @param string $iso |
153
|
|
|
* |
154
|
|
|
* @return \Arcanedev\Currencies\Contracts\Entities\Currency |
155
|
|
|
* |
156
|
|
|
* @throws \Arcanedev\Currencies\Exceptions\CurrencyNotFoundException |
157
|
|
|
*/ |
158
|
60 |
|
public function findOrFail($iso) |
159
|
|
|
{ |
160
|
60 |
|
return $this->currencies()->findOrFails($iso); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Get the supported currencies collection. |
165
|
|
|
* |
166
|
|
|
* @return \Arcanedev\Currencies\Entities\CurrencyCollection |
167
|
|
|
*/ |
168
|
12 |
|
public function getSupportedCurrencies() |
169
|
|
|
{ |
170
|
12 |
|
$currencies = $this->currencies(); |
171
|
12 |
|
$supported = array_map(function ($iso) use ($currencies) { |
172
|
12 |
|
return $currencies->findOrFails($iso); |
173
|
12 |
|
}, array_combine($this->getSupported(), $this->getSupported())); |
174
|
|
|
|
175
|
12 |
|
return CurrencyCollection::make($supported); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Format the amount. |
180
|
|
|
* |
181
|
|
|
* @param string $iso |
182
|
|
|
* @param double|int $amount |
183
|
|
|
* @param int $decimals |
184
|
|
|
* |
185
|
|
|
* @return string |
186
|
|
|
* |
187
|
|
|
* @throws \Arcanedev\Currencies\Exceptions\CurrencyNotFoundException |
188
|
|
|
*/ |
189
|
24 |
|
public function format($iso, $amount, $decimals = 2) |
190
|
|
|
{ |
191
|
24 |
|
return $this->findOrFail($iso)->format($amount, $decimals); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Format the amount by the default iso. |
196
|
|
|
* |
197
|
|
|
* @param double|int $amount |
198
|
|
|
* @param int $decimals |
199
|
|
|
* |
200
|
|
|
* @return string |
201
|
|
|
* |
202
|
|
|
* @throws \Arcanedev\Currencies\Exceptions\CurrencyNotFoundException |
203
|
|
|
*/ |
204
|
12 |
|
public function formatDefault($amount, $decimals = 2) |
205
|
|
|
{ |
206
|
12 |
|
return $this->format($this->getDefault(), $amount, $decimals); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Get the currency symbol by iso code. |
211
|
|
|
* |
212
|
|
|
* @param string $iso |
213
|
|
|
* |
214
|
|
|
* @return string |
215
|
|
|
*/ |
216
|
12 |
|
public function symbol($iso) |
217
|
|
|
{ |
218
|
12 |
|
return $this->findOrFail($iso)->symbol; |
|
|
|
|
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..