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.

BalanceValue.php 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace App\ValueObjects;
  3. use App\Utilities\Currency\CurrencyAccessor;
  4. use App\Utilities\Currency\CurrencyConverter;
  5. class BalanceValue
  6. {
  7. private int $value;
  8. private string $currency;
  9. public function __construct(int $value, string $currency)
  10. {
  11. $this->value = $value;
  12. $this->currency = $currency;
  13. }
  14. public function getValue(): int
  15. {
  16. return $this->value;
  17. }
  18. public function formatted(): string
  19. {
  20. return money($this->value, $this->currency)->format();
  21. }
  22. public function formattedSimple(): string
  23. {
  24. return money($this->value, $this->currency)->formatSimple();
  25. }
  26. public function formattedForDisplay(): string
  27. {
  28. $defaultCurrency = CurrencyAccessor::getDefaultCurrency();
  29. $accountCurrency = $this->currency;
  30. if ($accountCurrency === $defaultCurrency) {
  31. return $this->formatted();
  32. }
  33. $convertedBalance = CurrencyConverter::convertBalance($this->value, $defaultCurrency, $accountCurrency);
  34. return money($convertedBalance, $accountCurrency)->formatWithCode();
  35. }
  36. }