コード例 #1
0
ファイル: DecimalParserTest.php プロジェクト: gmelikov/Number
 public function testUnlocalization()
 {
     $unlocalizer = $this->getMock('ValueParsers\\NumberUnlocalizer');
     $unlocalizer->expects($this->once())->method('unlocalizeNumber')->will($this->returnCallback(function ($number) {
         return str_replace('#', '', $number);
     }));
     $unlocalizer->expects($this->never())->method('getNumberRegex');
     $unlocalizer->expects($this->never())->method('getUnitRegex');
     $parser = new DecimalParser(null, $unlocalizer);
     $input = '###20#000#000###';
     $value = $parser->parse($input);
     $this->assertEquals('20000000', $value->getValue());
 }
コード例 #2
0
ファイル: QuantityParser.php プロジェクト: gmelikov/Number
 /**
  * Constructs a QuantityValue from the given parts.
  *
  * @see splitQuantityString
  *
  * @param string $amount decimal representation of the amount
  * @param string|null $exactness either '!' to indicate an exact value,
  *        or '~' for "automatic", or null if $margin should be used.
  * @param string|null $margin decimal representation of the uncertainty margin
  * @param string $unit the unit identifier (use "1" for unitless quantities).
  *
  * @throws ParseException if one of the decimals could not be parsed.
  * @throws IllegalValueException if the QuantityValue could not be constructed
  * @return QuantityValue
  */
 private function newQuantityFromParts($amount, $exactness, $margin, $unit)
 {
     list($amount, $exponent) = $this->decimalParser->splitDecimalExponent($amount);
     $amountValue = $this->decimalParser->parse($amount);
     if ($exactness === '!') {
         // the amount is an exact number
         $amountValue = $this->decimalParser->applyDecimalExponent($amountValue, $exponent);
         $quantity = $this->newExactQuantity($amountValue, $unit);
     } elseif ($margin !== null) {
         // uncertainty margin given
         // NOTE: the pattern for scientific notation is 2e3 +/- 1e2, so the exponents are treated separately.
         $marginValue = $this->decimalParser->parse($margin);
         $amountValue = $this->decimalParser->applyDecimalExponent($amountValue, $exponent);
         $quantity = $this->newUncertainQuantityFromMargin($amountValue, $unit, $marginValue);
     } else {
         // derive uncertainty from given decimals
         // NOTE: with scientific notation, the exponent applies to the uncertainty bounds, too
         $quantity = $this->newUncertainQuantityFromDigits($amountValue, $unit, $exponent);
     }
     return $quantity;
 }