Commit ddcd72b6 authored by Thorsten Buss's avatar Thorsten Buss

recover compatibility of Money::parse() with $currency - ignore currency...

recover compatibility of Money::parse() with $currency - ignore currency format again and add Money::parseAfterCurrencyFormat() to bear of the currency format
parent 3c03c060
......@@ -15,7 +15,7 @@ use Illuminate\Support\Str;
/**
* Class Money
* @package Bnet\Money
*
*
* @method static Money EUR(int $amount)
* @method static Money USD(int $amount)
*/
......@@ -228,18 +228,29 @@ class Money implements \JsonSerializable, Jsonable, Arrayable {
throw new MoneyException('Formatted raw money should be string, e.g. 1.00');
}
// parsing with the format from the currency
if (!empty($currency)) {
if (!$currency instanceof Currency)
$currency = new Currency($currency);
return new static((int)self::parseStringToUnitWithCurrency($money, $currency), $currency);
}
// parsing with guessing separators
$currency = new Currency($currency);
return new static((int)self::parseStringToUnit($money), $currency);
}
/**
* Parse a Moneystring
* @param string $money
* @param string|Currency $currency
* @return static
* @throws MoneyException
*/
public static function parseAfterCurrencyFormat($money, $currency) {
if (!is_string($money)) {
throw new MoneyException('Formatted raw money should be string, e.g. 1.00');
}
// parsing with the format from the currency
if (!$currency instanceof Currency)
$currency = new Currency($currency);
return new static((int)self::parseStringToUnitWithCurrency($money, $currency), $currency);
}
/**
* @return Currency
*/
......@@ -552,4 +563,4 @@ class Money implements \JsonSerializable, Jsonable, Arrayable {
protected function dbl($amount, $currency=null) {
return new static($amount, $currency ?: $this->currency());
}
}
\ No newline at end of file
}
......@@ -225,13 +225,14 @@ class MoneyTest extends \PHPUnit_Framework_TestCase {
public function testMoneyParsing($currency, $string, $units) {
$m = $this->money($units);
try {
$this->assertEquals($m->value(), Money::parse($string, $currency)->value(), 'Value: ' . $string);
// test parsing after normal or the currency format
$parsed = $currency ? Money::parseAfterCurrencyFormat($string, $currency) : Money::parse($string);
$this->assertEquals($m->value(), $parsed->value(), 'Value: ' . $string);
} catch (\Exception $e) {
$this->fail('Exception on Value: ' . $string . ' -> ' . $e->getMessage());
}
}
public function testFactoryMethods() {
$this->assertEquals(Money::EUR(25), Money::EUR(10)->add(Money::EUR(15)));
$this->assertEquals(Money::USD(25), Money::USD(10)->add(Money::USD(15)));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment