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