Skip to content

Commit

Permalink
Add setCurrency (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardogobbosouza authored Dec 9, 2024
1 parent a46113c commit e2b5a45
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ public function getAttributes()
return $this->attributes;
}

/**
* Set currency.
*
* @param \Money\Currency|string $currency
* @return \Cknow\Money\Money
*/
public function setCurrency(\Money\Currency|string $currency)
{
return new Money($this->getAmount(), static::parseCurrency($currency));
}

/**
* Divide.
*
Expand Down
8 changes: 8 additions & 0 deletions tests/MoneyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ public function testNullable()
static::assertEquals(new Money(null, new Currency('USD')), Money::USD(0));
}

public function testSetCurrency()
{
static::assertEquals(Money::USD(25)->setCurrency('EUR'), Money::EUR(25));
static::assertEquals(Money::EUR(25)->setCurrency('USD'), Money::USD(25));
static::assertEquals(Money::USD('10.00')->setCurrency(new Currency('EUR')), Money::EUR(1000));
static::assertEquals(Money::EUR(10.10)->setCurrency(new Currency('USD')), Money::USD(1010));
}

public function testConvert()
{
static::assertEquals(Money::USD(25), Money::convert(new \Money\Money(25, new Currency('USD'))));
Expand Down

0 comments on commit e2b5a45

Please sign in to comment.