CommerceGuys\Intl\Formatter\NumberFormatter::parseCurrency PHP Method

parseCurrency() public method

public parseCurrency ( $value, CommerceGuys\Intl\Currency\CurrencyInterface $currency )
$currency CommerceGuys\Intl\Currency\CurrencyInterface
    public function parseCurrency($value, CurrencyInterface $currency)
    {
        $replacements = [$this->numberFormat->getDecimalSeparator() => '.', $this->numberFormat->getPlusSign() => '+', $this->numberFormat->getMinusSign() => '-', $this->numberFormat->getGroupingSeparator() => '', $currency->getCurrencyCode() => '', $currency->getSymbol() => '', ' ' => '', chr(0xc2) . chr(0xa0) => ''];
        $numberingSystem = $this->numberFormat->getNumberingSystem();
        if (isset($this->digits[$numberingSystem])) {
            // Convert the localized digits back to latin.
            $replacements += array_flip($this->digits[$numberingSystem]);
        }
        $value = strtr($value, $replacements);
        if (substr($value, 0, 1) == '(' && substr($value, -1, 1) == ')') {
            // This is an accounting formatted negative number.
            $value = '-' . str_replace(['(', ')'], '', $value);
        }
        return is_numeric($value) ? $value : false;
    }

Usage Example

 /**
  * @covers ::parseCurrency
  *
  * @uses \CommerceGuys\Intl\Currency\Currency
  * @uses \CommerceGuys\Intl\Formatter\NumberFormatter::__construct
  * @uses \CommerceGuys\Intl\NumberFormat\NumberFormat
  *
  * @dataProvider formattedCurrencyProvider
  */
 public function testParseCurrency($number_format, $currency, $style, $value, $expected_value)
 {
     $formatter = new NumberFormatter($number_format, $style);
     $parsedNumber = $formatter->parseCurrency($value, $currency);
     $this->assertSame($expected_value, $parsedNumber);
 }