1 | <?php |
||
17 | class DecimalParser extends StringValueParser { |
||
18 | |||
19 | const FORMAT_NAME = 'decimal'; |
||
20 | |||
21 | /** |
||
22 | * @var DecimalMath |
||
23 | */ |
||
24 | private $math; |
||
25 | |||
26 | /** |
||
27 | * @var null|NumberUnlocalizer |
||
28 | */ |
||
29 | private $unlocalizer; |
||
30 | |||
31 | /** |
||
32 | * @since 0.1 |
||
33 | * |
||
34 | * @param ParserOptions|null $options |
||
35 | * @param NumberUnlocalizer|null $unlocalizer |
||
36 | */ |
||
37 | 62 | public function __construct( ParserOptions $options = null, NumberUnlocalizer $unlocalizer = null ) { |
|
42 | |||
43 | /** |
||
44 | * @return DecimalMath |
||
45 | */ |
||
46 | 8 | private function getMath() { |
|
53 | |||
54 | /** |
||
55 | * Splits the exponent from the scientific notation of a decimal number. |
||
56 | * |
||
57 | * @since 0.5 |
||
58 | * |
||
59 | * @example splitDecimalExponent( '1.2' ) is [ '1.2', 0 ] |
||
60 | * @example splitDecimalExponent( '1.2e3' ) is [ '1.2', 3 ] |
||
61 | * @example splitDecimalExponent( '1.2e-2' ) is [ '1.2', -2 ] |
||
62 | * |
||
63 | * @param string $valueString A decimal string, possibly using scientific notation. |
||
64 | * |
||
65 | * @return array list( $decimal, $exponent ) A pair of the decimal value without the |
||
66 | * decimal exponent, and the decimal exponent as an integer. If $valueString |
||
67 | * does not use scientific notation, $exponent will be 0. |
||
68 | */ |
||
69 | 52 | public function splitDecimalExponent( $valueString ) { |
|
77 | |||
78 | /** |
||
79 | * Applies a decimal exponent, by shifting the decimal point in the decimal string |
||
80 | * representation of the value. |
||
81 | * |
||
82 | * @since 0.5 |
||
83 | * |
||
84 | * @example applyDecimalExponent( new DecimalValue( '1.2' ), 0 ) is new DecimalValue( '1.2' ) |
||
85 | * @example applyDecimalExponent( new DecimalValue( '1.2' ), 3 ) is new DecimalValue( '1200' ) |
||
86 | * @example applyDecimalExponent( new DecimalValue( '1.2' ), -2 ) is new DecimalValue( '0.012' ) |
||
87 | * |
||
88 | * @param DecimalValue $decimal |
||
89 | * @param int $exponent |
||
90 | * |
||
91 | * @return DecimalValue |
||
92 | */ |
||
93 | 34 | public function applyDecimalExponent( DecimalValue $decimal, $exponent ) { |
|
101 | |||
102 | /** |
||
103 | * Creates a DecimalValue from a given string. |
||
104 | * |
||
105 | * The decimal notation for the value is based on ISO 31-0, with some modifications: |
||
106 | * - the decimal separator is '.' (period). Comma is not used anywhere. |
||
107 | * - leading and trailing as well as any internal whitespace is ignored |
||
108 | * - the following characters are ignored: comma (","), apostrophe ("'"). |
||
109 | * - scientific (exponential) notation is supported using the pattern /e[-+]\d+/ |
||
110 | * - the number may start (or end) with a decimal point. |
||
111 | * - leading zeroes are stripped, except directly before the decimal point |
||
112 | * - trailing zeroes are stripped, except directly after the decimal point |
||
113 | * - zero is always positive. |
||
114 | * |
||
115 | * @see StringValueParser::stringParse |
||
116 | * |
||
117 | * @since 0.1 |
||
118 | * |
||
119 | * @param string $value |
||
120 | * |
||
121 | * @return DecimalValue |
||
122 | * @throws ParseException |
||
123 | */ |
||
124 | 40 | protected function stringParse( $value ) { |
|
147 | |||
148 | /** |
||
149 | * Normalize a decimal string. |
||
150 | * |
||
151 | * @param string $number |
||
152 | * |
||
153 | * @return string |
||
154 | */ |
||
155 | 40 | private function normalizeDecimal( $number ) { |
|
176 | |||
177 | } |
||
178 |