fomvasss /
laravel-currency
| 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', []); |
||
| 22 | } |
||
| 23 | |||
| 24 | $this->config = $config; |
||
| 25 | } |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @param $value |
||
| 29 | * @param null $code |
||
| 30 | * @param null $symbol |
||
| 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; |
||
| 49 | } |
||
| 50 | |||
| 51 | return $result; |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param $amount |
||
| 56 | * @param null $from |
||
| 57 | * @param null $to |
||
| 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'); |
||
| 65 | $to = $to ?: $this->getUserCurrency(); |
||
| 66 | |||
| 67 | // Get exchange rates |
||
| 68 | $fromRate = $this->getCurrencyProp($from, 'exchangeRate'); |
||
| 69 | $toRate = $this->getCurrencyProp($to, 'exchangeRate'); |
||
| 70 | |||
| 71 | // Skip invalid to currency rates |
||
| 72 | if ($toRate === null) { |
||
| 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
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 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 |
||
| 151 | * @return mixed |
||
| 152 | */ |
||
| 153 | public function getCurrency($code = null) |
||
| 154 | { |
||
| 155 | $code = $code ?: $this->getUserCurrency(); |
||
| 156 | |||
| 157 | return Arr::get($this->getCurrencies(), strtoupper($code)); |
||
| 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 |
||
| 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 |
||
| 191 | * @param null $default |
||
| 192 | * @return array|\Illuminate\Config\Repository|mixed |
||
| 193 | */ |
||
| 194 | protected function config($key = null, $default = null) |
||
| 195 | { |
||
| 196 | if ($key === null) { |
||
| 197 | return $this->config; |
||
| 198 | } |
||
| 199 | return Arr::get($this->config, $key, $default); |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param null $code |
||
| 204 | * @return null|string |
||
| 205 | */ |
||
| 206 | protected function prepareCode($code = null) |
||
| 207 | { |
||
| 208 | if (! $code) { |
||
| 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 | } |