1 | <?php |
||
19 | class QuantityParser extends StringValueParser { |
||
20 | |||
21 | public const FORMAT_NAME = 'quantity'; |
||
22 | |||
23 | /** |
||
24 | * The unit of the value to parse. If this option is given, it's illegal to also specify |
||
25 | * a unit in the input string. |
||
26 | * |
||
27 | * @since 0.5 |
||
28 | */ |
||
29 | public const OPT_UNIT = 'unit'; |
||
30 | |||
31 | /** |
||
32 | * @var DecimalParser |
||
33 | */ |
||
34 | private $decimalParser; |
||
35 | |||
36 | /** |
||
37 | * @var NumberUnlocalizer |
||
38 | */ |
||
39 | private $unlocalizer; |
||
40 | |||
41 | /** |
||
42 | * @since 0.1 |
||
43 | * |
||
44 | * @param ParserOptions|null $options |
||
45 | * @param NumberUnlocalizer|null $unlocalizer |
||
46 | */ |
||
47 | 99 | public function __construct( ParserOptions $options = null, NumberUnlocalizer $unlocalizer = null ) { |
|
55 | |||
56 | /** |
||
57 | * @see StringValueParser::stringParse |
||
58 | * |
||
59 | * @since 0.1 |
||
60 | * |
||
61 | * @param string $value |
||
62 | * |
||
63 | * @return UnboundedQuantityValue|QuantityValue |
||
64 | * @throws ParseException |
||
65 | */ |
||
66 | 92 | protected function stringParse( $value ) { |
|
84 | |||
85 | /** |
||
86 | * @return string|null |
||
87 | */ |
||
88 | 59 | private function getUnitFromOptions() { |
|
92 | |||
93 | /** |
||
94 | * Constructs a QuantityValue from the given parts. |
||
95 | * |
||
96 | * @see splitQuantityString |
||
97 | * |
||
98 | * @param string $amount decimal representation of the amount |
||
99 | * @param string|null $exactness either '!' to indicate an exact value, |
||
100 | * or '~' for "automatic", or null if $margin should be used. |
||
101 | * @param string|null $margin decimal representation of the uncertainty margin |
||
102 | * @param string $unit the unit identifier (use "1" for unitless quantities). |
||
103 | * |
||
104 | * @throws ParseException if one of the decimals could not be parsed. |
||
105 | * @throws IllegalValueException if the QuantityValue could not be constructed |
||
106 | * @return UnboundedQuantityValue|QuantityValue |
||
107 | */ |
||
108 | 56 | private function newQuantityFromParts( $amount, $exactness, $margin, $unit ) { |
|
133 | |||
134 | /** |
||
135 | * Splits a quantity string into its syntactic parts. |
||
136 | * |
||
137 | * @see newQuantityFromParts |
||
138 | * |
||
139 | * @param string $value |
||
140 | * |
||
141 | * @throws ParseException If $value does not match the expected pattern |
||
142 | * @return array list( $amount, $exactness, $margin, $unit ). |
||
143 | * Parts not present in $value will be null |
||
144 | */ |
||
145 | 92 | private function splitQuantityString( $value ) { |
|
177 | |||
178 | /** |
||
179 | * Returns a QuantityValue representing the given amount. |
||
180 | * The amount is assumed to be absolutely exact, that is, |
||
181 | * the upper and lower bound will be the same as the amount. |
||
182 | * |
||
183 | * @param DecimalValue $amount |
||
184 | * @param string $unit The quantity's unit (use "1" for unit-less quantities) |
||
185 | * |
||
186 | * @return QuantityValue |
||
187 | */ |
||
188 | 5 | private function newExactQuantity( DecimalValue $amount, $unit = '1' ) { |
|
191 | |||
192 | /** |
||
193 | * Returns a QuantityValue representing the given amount, automatically assuming |
||
194 | * a level of uncertainty based on the digits given. |
||
195 | * |
||
196 | * The upper and lower bounds are determined automatically from the given |
||
197 | * digits by increasing resp. decreasing the least significant digit. |
||
198 | * E.g. "+0.01" would have upperBound "+0.02" and lowerBound "+0.01", |
||
199 | * while "-100" would have upperBound "-99" and lowerBound "-101". |
||
200 | * |
||
201 | * @param DecimalValue $amount The quantity |
||
202 | * @param string $unit The quantity's unit (use "1" for unit-less quantities) |
||
203 | * @param DecimalValue $margin |
||
204 | * |
||
205 | * @return QuantityValue |
||
206 | */ |
||
207 | 13 | private function newUncertainQuantityFromMargin( DecimalValue $amount, DecimalValue $margin, $unit = '1' ) { |
|
216 | |||
217 | /** |
||
218 | * Returns a QuantityValue representing the given amount, automatically assuming |
||
219 | * a level of uncertainty based on the digits given. |
||
220 | * |
||
221 | * The upper and lower bounds are determined automatically from the given |
||
222 | * digits by adding/subtracting half the order of magnitude of the least |
||
223 | * significant digit. Trailing zeros before the decimal point are considered |
||
224 | * significant. |
||
225 | * |
||
226 | * E.g. "+0.01" would have upperBound "+0.015" and lowerBound "+0.005", |
||
227 | * while "-100" would have upperBound "-99.5" and lowerBound "-100.5". |
||
228 | * |
||
229 | * @param DecimalValue $amount The quantity |
||
230 | * @param string $unit The quantity's unit (use "1" for unit-less quantities) |
||
231 | * @param int $exponent Decimal exponent to apply |
||
232 | * |
||
233 | * @return QuantityValue |
||
234 | */ |
||
235 | 4 | private function newUncertainQuantityFromDigits( DecimalValue $amount, $unit = '1', $exponent = 0 ) { |
|
257 | |||
258 | } |
||
259 |