NumberParser
class NumberParser (View source)
Parser for numbers.
This parser does not support full syntax of number formats as defined in CLDR. It uses parsed formats from NumbersReader class.
Constants
PATTERN_MATCH_DIGITS |
Regex pattern for matching one or more digits. |
PATTERN_MATCH_NOT_DIGITS |
Regex pattern for matching all except digits. It's used for clearing
string in lenient mode. |
Properties
protected NumbersReader | $numbersReader |
Methods
Parses number given as a string using provided format.
Parses decimal number using proper format from CLDR.
Parses percent number using proper format from CLDR.
Parses number using parsed format, in strict or lenient mode.
Parses number in strict mode.
Parses number in lenient mode.
Details
void
injectNumbersReader(NumbersReader $numbersReader)
No description
mixed
parseNumberWithCustomPattern(string $numberToParse, string $format, Locale $locale, bool $strictMode = true)
Parses number given as a string using provided format.
mixed
parseDecimalNumber(string $numberToParse, Locale $locale, string $formatLength = NumbersReader::FORMAT_LENGTH_DEFAULT, bool $strictMode = true)
Parses decimal number using proper format from CLDR.
mixed
parsePercentNumber(string $numberToParse, Locale $locale, string $formatLength = NumbersReader::FORMAT_LENGTH_DEFAULT, bool $strictMode = true)
Parses percent number using proper format from CLDR.
protected mixed
doParsingWithParsedFormat(string $numberToParse, array $parsedFormat, array $localizedSymbols, bool $strictMode)
Parses number using parsed format, in strict or lenient mode.
protected mixed
doParsingInStrictMode(string $numberToParse, array $parsedFormat, array $localizedSymbols)
Parses number in strict mode.
In strict mode parser checks all constraints of provided parsed format, and if any of them is not fullfiled, parsing fails (false is returned).
protected mixed
doParsingInLenientMode(string $numberToParse, array $parsedFormat, array $localizedSymbols)
Parses number in lenient mode.
Lenient parsing ignores everything that can be ignored, and tries to extract number from the string, even if it's not well formed.
Implementation is simple but should work more often than strict parsing.
Algorithm:
- Find first digit
- Find last digit
- Find decimal separator between first and last digit (if any)
- Remove non-digits from integer part
- Remove non-digits from decimal part (optional)
- Try to match negative prefix before first digit
- Try to match negative suffix after last digit