1 | <?php |
||||
2 | |||||
3 | namespace Fomvasss\Currency; |
||||
4 | |||||
5 | use Illuminate\Support\Arr; |
||||
6 | |||||
7 | class Currency |
||||
8 | { |
||||
9 | protected $config; |
||||
10 | |||||
11 | protected $symbol = null; |
||||
12 | |||||
13 | protected $userCurrency; |
||||
14 | |||||
15 | /** |
||||
16 | * Currency constructor. |
||||
17 | */ |
||||
18 | public function __construct(array $config = null) |
||||
19 | { |
||||
20 | if (!$config) { |
||||
21 | $config = config('currency', []); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
22 | } |
||||
23 | |||||
24 | $this->config = $config; |
||||
25 | } |
||||
26 | |||||
27 | /** |
||||
28 | * @param $value |
||||
29 | * @param null $code |
||||
0 ignored issues
–
show
|
|||||
30 | * @param null $symbol |
||||
0 ignored issues
–
show
|
|||||
31 | * @return string |
||||
32 | */ |
||||
33 | public function format($value, $code = null, $symbol = null) |
||||
34 | { |
||||
35 | $code = $this->prepareCode($code); |
||||
36 | |||||
37 | $precision = $this->config("currencies.$code.precision", 0); |
||||
38 | $decimalSeparator = $this->config("currencies.$code.decimalSeparator", ""); |
||||
39 | $thousandSeparator = $this->config("currencies.$code.thousandSeparator", ""); |
||||
40 | |||||
41 | $symbol = $symbol ?? $this->config("currencies.$code.symbol", ""); |
||||
42 | |||||
43 | $value = $this->prepareValue($value, $code); |
||||
44 | |||||
45 | $result = number_format($value, $precision, $decimalSeparator, $thousandSeparator); |
||||
46 | |||||
47 | if ($symbol) { |
||||
48 | return $this->config("currencies.$code.symbolPlacement") == 'after' ? $result.$symbol : $symbol.$result; |
||||
0 ignored issues
–
show
Are you sure
$symbol of type Illuminate\Config\Repository|array|mixed can be used in concatenation ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
49 | } |
||||
50 | |||||
51 | return $result; |
||||
52 | } |
||||
53 | |||||
54 | /** |
||||
55 | * @param $amount |
||||
56 | * @param null $from |
||||
0 ignored issues
–
show
|
|||||
57 | * @param null $to |
||||
0 ignored issues
–
show
|
|||||
58 | * @param bool $format |
||||
59 | * @return float|int|string|null |
||||
60 | */ |
||||
61 | public function convert($amount, $from = null, $to = null, $format = true) |
||||
62 | { |
||||
63 | // Get currencies involved |
||||
64 | $from = $from ?: $this->config('default'); |
||||
0 ignored issues
–
show
|
|||||
65 | $to = $to ?: $this->getUserCurrency(); |
||||
0 ignored issues
–
show
|
|||||
66 | |||||
67 | // Get exchange rates |
||||
68 | $fromRate = $this->getCurrencyProp($from, 'exchangeRate'); |
||||
0 ignored issues
–
show
It seems like
$from can also be of type array ; however, parameter $code of Fomvasss\Currency\Currency::getCurrencyProp() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
69 | $toRate = $this->getCurrencyProp($to, 'exchangeRate'); |
||||
70 | |||||
71 | // Skip invalid to currency rates |
||||
72 | if ($toRate === null) { |
||||
0 ignored issues
–
show
|
|||||
73 | return null; |
||||
74 | } |
||||
75 | // Convert amount |
||||
76 | if ($from === $to) { |
||||
77 | $value = $amount; |
||||
78 | } else { |
||||
79 | $value = ($amount * $toRate) / $fromRate; |
||||
80 | } |
||||
81 | // Should the result be formatted? |
||||
82 | if ($format === true) { |
||||
83 | return $this->format($value, $to); |
||||
84 | } |
||||
85 | |||||
86 | // Return value |
||||
87 | return $this->prepareValue($value, $to); |
||||
88 | } |
||||
89 | |||||
90 | protected function prepareValue($value, $code) |
||||
91 | { |
||||
92 | if ($this->config("divide_result") && ($coin = $this->config("currencies.$code.coin", 0))) { |
||||
93 | $value = $value / $coin; |
||||
94 | } |
||||
95 | |||||
96 | return $value; |
||||
97 | } |
||||
98 | |||||
99 | /** |
||||
100 | * Get the given property value from provided currency. |
||||
101 | * |
||||
102 | * @param string $code |
||||
103 | * @param string $key |
||||
104 | * @param mixed $default |
||||
105 | * |
||||
106 | * @return array |
||||
107 | */ |
||||
108 | protected function getCurrencyProp($code, $key, $default = null) |
||||
109 | { |
||||
110 | return Arr::get($this->getCurrency($code), $key, $default); |
||||
111 | } |
||||
112 | |||||
113 | /** |
||||
114 | * Return all currencies. |
||||
115 | * |
||||
116 | * @return array |
||||
117 | */ |
||||
118 | public function getCurrencies() |
||||
119 | { |
||||
120 | return $this->config('currencies', []); |
||||
0 ignored issues
–
show
|
|||||
121 | } |
||||
122 | |||||
123 | /** |
||||
124 | * |
||||
125 | * Determine if the provided currency is valid. |
||||
126 | * |
||||
127 | * @param $code |
||||
128 | * @return bool |
||||
129 | */ |
||||
130 | public function issetCurrency($code) |
||||
131 | { |
||||
132 | return array_key_exists(strtoupper($code), $this->getCurrencies()); |
||||
133 | } |
||||
134 | |||||
135 | /** |
||||
136 | * |
||||
137 | * Determine if the provided currency is active. |
||||
138 | * |
||||
139 | * @param $code |
||||
140 | * @return bool |
||||
141 | */ |
||||
142 | public function isActive($code) |
||||
143 | { |
||||
144 | return $code && (bool) Arr::get($this->getCurrency($code), 'active', false); |
||||
145 | } |
||||
146 | |||||
147 | /** |
||||
148 | * Return the current currency. |
||||
149 | * |
||||
150 | * @param null $code |
||||
0 ignored issues
–
show
|
|||||
151 | * @return mixed |
||||
152 | */ |
||||
153 | public function getCurrency($code = null) |
||||
154 | { |
||||
155 | $code = $code ?: $this->getUserCurrency(); |
||||
0 ignored issues
–
show
|
|||||
156 | |||||
157 | return Arr::get($this->getCurrencies(), strtoupper($code)); |
||||
0 ignored issues
–
show
It seems like
$code can also be of type array ; however, parameter $string of strtoupper() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
158 | } |
||||
159 | |||||
160 | public function setUserCurrency($code) |
||||
161 | { |
||||
162 | $this->userCurrency = strtoupper($code); |
||||
163 | |||||
164 | return $this; |
||||
165 | } |
||||
166 | |||||
167 | /** |
||||
168 | * @return array|\Illuminate\Config\Repository|mixed |
||||
0 ignored issues
–
show
The type
Illuminate\Config\Repository was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||||
169 | */ |
||||
170 | public function getUserCurrency() |
||||
171 | { |
||||
172 | return $this->userCurrency ?? $this->config('default'); |
||||
173 | } |
||||
174 | |||||
175 | /** |
||||
176 | * Return all active currencies. |
||||
177 | * |
||||
178 | * @return array |
||||
179 | */ |
||||
180 | public function getActiveCurrencies() |
||||
181 | { |
||||
182 | return array_filter($this->getCurrencies(), function($currency) { |
||||
183 | return $currency['active'] == true; |
||||
184 | }); |
||||
185 | } |
||||
186 | |||||
187 | /** |
||||
188 | * Return currencies config. |
||||
189 | * |
||||
190 | * @param null $key |
||||
0 ignored issues
–
show
|
|||||
191 | * @param null $default |
||||
0 ignored issues
–
show
|
|||||
192 | * @return array|\Illuminate\Config\Repository|mixed |
||||
193 | */ |
||||
194 | protected function config($key = null, $default = null) |
||||
195 | { |
||||
196 | if ($key === null) { |
||||
0 ignored issues
–
show
|
|||||
197 | return $this->config; |
||||
198 | } |
||||
199 | return Arr::get($this->config, $key, $default); |
||||
200 | } |
||||
201 | |||||
202 | /** |
||||
203 | * @param null $code |
||||
0 ignored issues
–
show
|
|||||
204 | * @return null|string |
||||
205 | */ |
||||
206 | protected function prepareCode($code = null) |
||||
207 | { |
||||
208 | if (! $code) { |
||||
0 ignored issues
–
show
|
|||||
209 | $code = $this->config("default", 'USD'); |
||||
210 | } |
||||
211 | |||||
212 | if (array_key_exists($code, $this->getCurrencies())) { |
||||
213 | return $code; |
||||
0 ignored issues
–
show
|
|||||
214 | } |
||||
215 | |||||
216 | return 'USD'; |
||||
217 | } |
||||
218 | } |