You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CurrencyConverter.php 1.1KB

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. namespace App\Utilities\Currency;
  3. class CurrencyConverter
  4. {
  5. public static function convertAndSet($newCurrency, $oldCurrency, $amount): ?string
  6. {
  7. if ($newCurrency === null || $oldCurrency === $newCurrency) {
  8. return null;
  9. }
  10. $old_attr = currency($oldCurrency);
  11. $new_attr = currency($newCurrency);
  12. $temp_balance = str_replace([$old_attr->getThousandsSeparator(), $old_attr->getDecimalMark()], ['', '.'], $amount);
  13. return number_format((float) $temp_balance, $new_attr->getPrecision(), $new_attr->getDecimalMark(), $new_attr->getThousandsSeparator());
  14. }
  15. public static function convertBalance(int $balance, string $oldCurrency, string $newCurrency): int
  16. {
  17. return money($balance, $oldCurrency)->swapAmountFor($newCurrency);
  18. }
  19. public static function prepareForMutator(int $balance, string $currency): string
  20. {
  21. return money($balance, $currency)->formatSimple();
  22. }
  23. public static function prepareForAccessor(string $balance, string $currency): int
  24. {
  25. return money($balance, $currency, true)->getAmount();
  26. }
  27. }