Example #1
0
 public function __construct($amount, $currency, $locale = null)
 {
     if (!empty($locale)) {
         static::$defaultLocale = $locale;
     }
     parent::__construct($amount, $currency);
 }
Example #2
0
 public function testChainAdding()
 {
     $item = new ItemInstance('20', Money::fromString('12.87', 'USD'), 2);
     $itemTwo = new ItemInstance('21', Money::fromString('1.50', 'USD'), 1);
     $this->collection->add($item)->add($itemTwo);
     $this->assertTrue($this->collection->has('20'));
     $this->assertTrue($this->collection->has('21'));
 }
 /**
  * Update the purchasable total on the purchasable holder
  * @param bool|void $saveState
  * @throws \SebastianBergmann\Money\OverflowException
  * @return void
  */
 public function updateTotal($saveState = true)
 {
     $this->total = $this->currencyService->getZeroMoney();
     if ($this->purchasables) {
         foreach ($this->purchasables as $purchasable) {
             $this->total = $this->total->add($purchasable->getTotal());
         }
     }
     $this->eventService->dispatch(Events::UPDATED);
     if ($saveState) {
         $this->saveState();
     }
 }
Example #4
0
 /**
  * Tells if a Money value is less than current Money object
  *
  * @param MoneyInterface $other
  *
  * @return boolean Current money is less than given as parameter
  */
 public function isLessThan(MoneyInterface $other)
 {
     return $this->wrappedMoney->lessThan($this->newWrappedMoneyFromMoney($other));
 }
 /**
  * @inheritdoc
  * @param Money $object
  */
 public function normalize($object, $format = null, array $context = [])
 {
     return $object->jsonSerialize();
 }
/**
 * Converts a Money value object to a string for the database reporting etc
 * @param \SebastianBergmann\Money\Money $value
 * @return string
 */
function convertMoneyToString(Money $value)
{
    return convertIntToString($value->getAmount(), $value->getCurrency());
}
Example #7
0
$stringValues = ['amount' => '12.34', 'currency' => 'EUR'];
$stringMoney = new Money($stringValues);
dump('string', $stringMoney);
echo 'Amount: ' . $stringMoney->getAmount() . "<br />\n";
echo 'Converted amount: ' . $stringMoney->getConvertedAmount() . "<br />\n";
echo 'Currency: ' . $stringMoney->getCurrency()->getCurrencyCode();
echo '<h2>Float (30.50).</h2>';
// ucfirst is applied automatically to find the right setter
$floatValues = ['amount' => 30.5, 'currency' => 'EUR'];
$floatMoney = new Money($floatValues);
dump($floatMoney);
echo 'Amount: ' . $floatMoney->getAmount() . "<br />\n";
echo 'Converted amount: ' . $floatMoney->getConvertedAmount() . "<br />\n";
echo 'Currency: ' . $floatMoney->getCurrency()->getCurrencyCode();
echo '<h2>Public methods</h2>';
// ucfirst is applied automatically to find the right setter
$values = ['amount' => 305, 'currency' => 'EUR'];
$money = new Money($values);
dump($money);
echo 'Amount: ' . $money->getAmount() . "<br />\n";
echo 'Converted amount: ' . $money->getConvertedAmount() . "<br />\n";
echo 'Currency: ' . $money->getCurrency()->getCurrencyCode();
$newValues = ['amount' => 200, 'currency' => $money->getCurrency()];
$newMoney = new Money($newValues);
$sum = $money->add($newMoney);
dump('Sum of Money objects', $sum);
$sub = $money->subtract($newMoney);
dump('Sub of Money objects', $sub);
echo '<h1>Example usage of Sebastian Money.</h1>';
$sebMoney = BaseMoney::fromString('12.34', 'EUR');
dump($sebMoney);
 /**
  * gets currency name
  *
  * @param Money $money
  * @return null|string
  */
 public function formatCurrencyAsName(Money $money)
 {
     return Intl::getCurrencyBundle()->getCurrencyName($money->getCurrency()->getCurrencyCode());
 }
Example #9
0
 /**
  * {@inheritdoc}
  */
 public function subtract(Money $other)
 {
     $toSub = new BaseMoney($other->getAmount(), $other->getCurrency());
     $result = $this->valueObject->subtract($toSub);
     return new static(['amount' => $result->getAmount(), 'currency' => $this->currency]);
 }
Example #10
0
 /**
  * @return static
  */
 public function setValue(Money $value)
 {
     $this->value = $value->getAmount();
     return $this;
 }
 /**
  * The value "exists" if it is not 0
  * @return bool
  */
 public function exists()
 {
     return $this->value->getAmount() !== 0;
 }
Example #12
0
 /**
  * @param  Money    $money
  * @param  Currency $to
  * @param  int      $roundingMode
  * @return Money
  */
 public function convert(Money $money, Currency $to, $roundingMode = PHP_ROUND_HALF_UP)
 {
     $new = new Money($money->getAmount(), $to);
     $conversionRate = $this->source->getRateBetween($money->getCurrency(), $to);
     return $new->multiply($conversionRate, $roundingMode);
 }
Example #13
0
 /**
  * Create money object
  *
  * @param $amount
  * @return Money|static
  */
 public function newMoney($amount)
 {
     if (is_string($amount) || is_float($amount)) {
         return Money::fromString((string) $amount, new Currency('ZAR'));
     }
     return new Money($amount, new Currency('ZAR'));
 }
 private function mapper(\stdClass $transaction) : Transaction
 {
     $paymentAmount = Money::fromString((string) $transaction->paymentAmount, new Currency('USD'));
     return new Transaction((int) $transaction->id, (int) $transaction->guestId, new DateTime($transaction->date), (bool) $transaction->success, $transaction->result, $paymentAmount, $transaction->stripeTransactionId);
 }
 private function mapper(\stdClass $guest) : Guest
 {
     $paymentAmount = Money::fromString((string) $guest->paymentAmount, new Currency('USD'));
     return new Guest($guest->id, $guest->firstName, $guest->lastName, $guest->emailAddress, $guest->phoneNumber, $guest->address, $paymentAmount);
 }
Example #16
0
 /**
  * Formats a Money object using PHP's built-in NumberFormatter.
  *
  * @param  \SebastianBergmann\Money\Money $money
  * @return string
  */
 public function format(Money $money)
 {
     return $this->numberFormatter->formatCurrency($money->getConvertedAmount(), $money->getCurrency()->getCurrencyCode());
 }
 /**
  * Converts amount from one currency to another using the currency's identifier
  *
  * Warning this method can lose precision!
  *
  * @param  \SebastianBergmann\Money\Money $amount
  * @param  \Heystack\Core\Identifier\IdentifierInterface $to
  * @return \SebastianBergmann\Money\Money
  * @throws \InvalidArgumentException
  */
 public function convert(Money $amount, IdentifierInterface $to)
 {
     if (!($toCurrency = $this->getCurrency($to))) {
         throw new \InvalidArgumentException("Currency not supported");
     }
     /** @var \Heystack\Ecommerce\Currency\Interfaces\CurrencyInterface $fromCurrency */
     $fromCurrency = $amount->getCurrency();
     return $amount->multiply($toCurrency->getValue() / $fromCurrency->getValue());
 }