Skip to content

Commit

Permalink
rename second MoneyParser::parse() parameter to $fallbackCurrency (#647)
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikbosch committed May 5, 2021
1 parent 73a469f commit 59837e3
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

- **[BC break]** BC Math required as it is the default calculator
- Allow multiple arguments to `Money#isSameCurrency`
- Renamed second parameter of `Parser#parse` to `$fallbackCurrency`

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion src/MoneyParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ interface MoneyParser
*
* @throws Exception\ParserException
*/
public function parse(string $money, Currency|null $forceCurrency = null): Money;
public function parse(string $money, Currency|null $fallbackCurrency = null): Money;
}
4 changes: 2 additions & 2 deletions src/Parser/AggregateMoneyParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ public function __construct(array $parsers)
$this->parsers = $parsers;
}

public function parse(string $money, Currency|null $forceCurrency = null): Money
public function parse(string $money, Currency|null $fallbackCurrency = null): Money
{
foreach ($this->parsers as $parser) {
try {
return $parser->parse($money, $forceCurrency);
return $parser->parse($money, $fallbackCurrency);
} catch (Exception\ParserException $e) {
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Parser/BitcoinMoneyParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ public function __construct(int $fractionDigits)
$this->fractionDigits = $fractionDigits;
}

public function parse(string $money, Currency|null $forceCurrency = null): Money
public function parse(string $money, Currency|null $fallbackCurrency = null): Money
{
if (strpos($money, BitcoinCurrencies::SYMBOL) === false) {
throw new ParserException('Value cannot be parsed as Bitcoin');
}

$currency = $forceCurrency ?? new Currency(BitcoinCurrencies::CODE);
$currency = $fallbackCurrency ?? new Currency(BitcoinCurrencies::CODE);
$decimal = str_replace(BitcoinCurrencies::SYMBOL, '', $money);
$decimalSeparator = strpos($decimal, '.');

Expand Down
10 changes: 5 additions & 5 deletions src/Parser/DecimalMoneyParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ public function __construct(Currencies $currencies)
$this->currencies = $currencies;
}

public function parse(string $money, Currency|null $forceCurrency = null): Money
public function parse(string $money, Currency|null $fallbackCurrency = null): Money
{
if ($forceCurrency === null) {
if ($fallbackCurrency === null) {
throw new ParserException('DecimalMoneyParser cannot parse currency symbols. Use forceCurrency argument');
}

$decimal = trim($money);

if ($decimal === '') {
return new Money(0, $forceCurrency);
return new Money(0, $fallbackCurrency);
}

if (! preg_match(self::DECIMAL_PATTERN, $decimal, $matches) || ! isset($matches['digits'])) {
Expand All @@ -57,7 +57,7 @@ public function parse(string $money, Currency|null $forceCurrency = null): Money
$decimal = '-' . $decimal;
}

$subunit = $this->currencies->subunitFor($forceCurrency);
$subunit = $this->currencies->subunitFor($fallbackCurrency);

if (isset($matches['fraction'])) {
$fractionDigits = strlen($matches['fraction']);
Expand All @@ -84,6 +84,6 @@ public function parse(string $money, Currency|null $forceCurrency = null): Money
}

/** @psalm-var numeric-string $decimal */
return new Money($decimal, $forceCurrency);
return new Money($decimal, $fallbackCurrency);
}
}
8 changes: 4 additions & 4 deletions src/Parser/IntlLocalizedDecimalParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public function __construct(NumberFormatter $formatter, Currencies $currencies)
$this->currencies = $currencies;
}

public function parse(string $money, Currency|null $forceCurrency = null): Money
public function parse(string $money, Currency|null $fallbackCurrency = null): Money
{
if ($forceCurrency === null) {
if ($fallbackCurrency === null) {
throw new ParserException('IntlLocalizedDecimalParser cannot parse currency symbols. Use forceCurrency argument');
}

Expand All @@ -47,7 +47,7 @@ public function parse(string $money, Currency|null $forceCurrency = null): Money
}

$decimal = (string) $decimal;
$subunit = $this->currencies->subunitFor($forceCurrency);
$subunit = $this->currencies->subunitFor($fallbackCurrency);
$decimalPosition = strpos($decimal, '.');

if ($decimalPosition !== false) {
Expand Down Expand Up @@ -76,6 +76,6 @@ public function parse(string $money, Currency|null $forceCurrency = null): Money
}

/** @psalm-var numeric-string $decimal */
return new Money($decimal, $forceCurrency);
return new Money($decimal, $fallbackCurrency);
}
}
10 changes: 5 additions & 5 deletions src/Parser/IntlMoneyParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(NumberFormatter $formatter, Currencies $currencies)
$this->currencies = $currencies;
}

public function parse(string $money, Currency|null $forceCurrency = null): Money
public function parse(string $money, Currency|null $fallbackCurrency = null): Money
{
$currency = null;
$decimal = $this->formatter->parseCurrency($money, $currency);
Expand All @@ -44,14 +44,14 @@ public function parse(string $money, Currency|null $forceCurrency = null): Money
throw new ParserException('Cannot parse ' . $money . ' to Money. ' . $this->formatter->getErrorMessage());
}

if ($forceCurrency === null) {
if ($fallbackCurrency === null) {
assert(! empty($currency));

$forceCurrency = new Currency($currency);
$fallbackCurrency = new Currency($currency);
}

$decimal = (string) $decimal;
$subunit = $this->currencies->subunitFor($forceCurrency);
$subunit = $this->currencies->subunitFor($fallbackCurrency);
$decimalPosition = strpos($decimal, '.');

if ($decimalPosition !== false) {
Expand Down Expand Up @@ -80,6 +80,6 @@ public function parse(string $money, Currency|null $forceCurrency = null): Money
}

/** @psalm-var numeric-string $decimal */
return new Money($decimal, $forceCurrency);
return new Money($decimal, $fallbackCurrency);
}
}

0 comments on commit 59837e3

Please sign in to comment.