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 997B

1234567891011121314151617181920212223242526272829
  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($balance, $oldCurrency, $newCurrency): int
  16. {
  17. return money($balance, $oldCurrency)->swapAmountFor($newCurrency);
  18. }
  19. public static function convertFormattedBalance($balance, $oldCurrency, $newCurrency): int
  20. {
  21. return money($balance, $oldCurrency)->swapFormattedAmountFor($newCurrency);
  22. }
  23. }