Пример #1
0
 public function testFilter()
 {
     // (1) italian - italy
     // (2) default currency code for it_IT
     // (3) correctness of fraction digits not mandatory
     // (4) correctness (and presence) of currency not mandatory
     // (5) breaking spaces allowed
     $filter = new Uncurrency('it_IT', null);
     $filter->setScaleCorrectness(false);
     $filter->setCurrencyCorrectness(false);
     $filter->setBreakingSpaceAllowed(true);
     $formatter = $filter->getFormatter();
     // ALLOWED //
     $this->assertSame((double) 123, $filter->filter($formatter->format((double) 123)));
     // 123 €
     $this->assertSame(1234.61, $filter->filter($formatter->format(1234.61)));
     // 1.234,61 €
     // pattern correct, fraction digit correct, currency absence
     $this->assertSame(1234.61, $filter->filter('1.234,61'));
     // pattern not correct (no grouping separator), fraction digit correct, currency absence
     $this->assertSame(1234.61, $filter->filter('1234,61'));
     // pattern not correct (no grouping separator), fraction digit correct, currency code as currency
     if (version_compare($GLOBALS['INTL_ICU_VERSION'], '4.8.1.1') > 0) {
         $this->assertSame(1234.61, $filter->filter('1234,61 EUR'));
     }
     // pattern correct, fraction digit correct, display name as currency
     $this->assertSame(1234.61, $filter->filter('1.234,61 EURO'));
     // pattern not correct (no grouping separator), fraction digit correct, display name as currency
     $this->assertSame(1234.61, $filter->filter('1234,61 EURO'));
     // wrong number of decimals, currency absence
     $this->assertSame(1234.619, $filter->filter('1234,619'));
     // wrong number of decimals, currency
     $this->assertSame(1234.619, $filter->filter('1234,619 €'));
     // negative
     $this->assertSame((double) -2.5, $filter->filter($formatter->format((double) -2.5)));
     // -2,50 €
     // negative, pattern not correct (currecny symbol without prefixing space)
     $this->assertSame(-0.01, $filter->filter('-0,01€'));
     // negative, currecny absence
     $this->assertSame(-0.5, $filter->filter('-0,5'));
     // NOT ALLOWED //
     // not existent display name
     $this->assertSame('1234,61 EUROOO', $filter->filter('1234,61 EUROOO'));
     // exponential notation
     $this->assertSame('1E-2 €', $filter->filter('1E-2 €'));
     // (3) correct number of decimal places will now be mandatory
     $filter->setScaleCorrectness(true);
     // NO MORE ALLOWED //
     $this->assertSame('1.234,619 €', $filter->filter('1.234,619 €'));
     $this->assertSame('1.234,619', $filter->filter('1.234,619'));
     $this->assertSame('1.234,1 €', $filter->filter('1.234,1 €'));
     // ALLOWED //
     $this->assertSame(1234.1, $filter->filter('1.234,10 €'));
     // (4) currency correctness (and so its presence) will no be mandatory
     $filter->setCurrencyCorrectness(true);
     // NO MORE ALLOWED //
     // currency absence
     $this->assertSame('1234,61', $filter->filter('1234,61'));
     // currency pattern part malformed (no prefixing space)
     $this->assertSame('1234,61€', $filter->filter('1234,61€'));
     // ALLOWED //
     $this->assertSame(1234.61, $filter->filter($formatter->format(1234.61)));
     // 1234,61 €
     $this->assertSame(-0.01, $filter->filter($formatter->format(-0.01)));
     // -0,01 €
     // (1) bengali - bangladesh
     // (2) default currency code
     // (2) correct number of decimal places not mandatory
     // (3) currency not mandatory
     $filter = new Uncurrency('bn_BD', null);
     $filter->setScaleCorrectness(false);
     $filter->setCurrencyCorrectness(false);
     $formatter = $filter->getFormatter();
     // Allowed
     $this->assertSame((double) 0, $filter->filter($formatter->format((double) 0)));
     // '০.০০৳' (w/ currency symbol)
     $this->assertSame((double) 0, $filter->filter('০.০'));
     $this->assertSame(0.01, $filter->filter($formatter->format(0.01)));
     // '০.০১৳' (w/ currency symbol)
     $this->assertSame(0.01, $filter->filter('০.০১০'));
     // $this->assertSame((float) 0, $filter->filter('(০.০)')); // FIXME? negative zero allowed
     $this->assertSame(-0.01, $filter->filter($formatter->format(-0.01)));
     // (০.০১৳) (w/ currency symbol)
     $this->assertSame(-0.01, $filter->filter('(০.০১)'));
 }
Пример #2
0
 /**
  * Returns true if and only if $value is a currency amount well-formatted for the given locale
  *
  * It validates according to the specified options, i.e. whether to consider valid:
  * a negative currency amount,
  * a currency different than the locale's default,
  * a currency string that could not contain currency symbol,
  * a currency string that could contain an inexact number of decimal places.
  *
  * @param  string $value
  * @return bool
  * @throws I18nException\InvalidArgumentException
  */
 public function isValid($value)
 {
     $this->setValue($value);
     if (!is_string($value)) {
         $this->error(self::INVALID);
         return false;
     }
     // Setup filter
     $filter = new Uncurrency($this->getLocale(), $this->getCurrencyCode());
     $filter->setScaleCorrectness($this->getScaleCorrectness());
     $filter->setCurrencyCorrectness($this->getCurrencyCorrectness());
     $filter->setBreakingSpaceAllowed($this->isBreakingSpaceAllowed());
     // Filtering
     $result = $filter->filter($this->getValue());
     // Retrieve updated currency code
     $this->currencyCode = $filter->getCurrencyCode();
     if ($result !== $this->getValue()) {
         // Filter succedeed
         if (!$this->isNegativeAllowed() && $result < 0) {
             $this->error(self::NOT_POSITIVE);
             return false;
         }
         return true;
     }
     $this->pattern = $filter->getFormatter()->getPattern();
     $this->error(self::NOT_CURRENCY);
     return false;
 }