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')); }
/** * {@inheritdoc} */ public function __construct(array $values = []) { // Set values in the object $this->traitConstruct($values); if (is_string($this->amount)) { $this->valueObject = BaseMoney::fromString($this->amount, $this->currency); } elseif (is_float($this->amount)) { $this->valueObject = BaseMoney::fromString((string) $this->amount, $this->currency); } elseif (is_int($this->amount)) { // Maybe is int: leave to BaseMoney other checks $this->valueObject = new BaseMoney($this->amount, $this->currency); } else { throw new \InvalidArgumentException(sprintf('The amount has to be a string or a float (ex.: 35.5 Euros) or' . ' an int in the base form (355 = 35.5 Euros). %s given.', gettype($this->amount))); } }
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); }
$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);
/** * 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); }