1 | <?php |
||
19 | class QuantityParser extends StringValueParser { |
||
20 | |||
21 | 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 | 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 | public function __construct( ParserOptions $options = null, NumberUnlocalizer $unlocalizer = null ) { |
||
48 | parent::__construct( $options ); |
||
49 | |||
50 | $this->defaultOption( self::OPT_UNIT, null ); |
||
51 | |||
52 | $this->unlocalizer = $unlocalizer ?: new BasicNumberUnlocalizer(); |
||
53 | $this->decimalParser = new DecimalParser( $this->options, $this->unlocalizer ); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @see StringValueParser::stringParse |
||
58 | * |
||
59 | * @since 0.1 |
||
60 | * |
||
61 | * @param string $value |
||
62 | * |
||
63 | * @return QuantityValue |
||
64 | * @throws ParseException |
||
65 | */ |
||
66 | protected function stringParse( $value ) { |
||
67 | list( $amount, $exactness, $margin, $unit ) = $this->splitQuantityString( $value ); |
||
68 | |||
69 | $unitOption = $this->getUnitFromOptions(); |
||
70 | |||
71 | if ( $unit === null ) { |
||
72 | $unit = $unitOption !== null ? $unitOption : '1'; |
||
73 | } elseif ( $unitOption !== null && $unit !== $unitOption ) { |
||
74 | throw new ParseException( 'Cannot specify a unit in input if a unit was fixed via options.' ); |
||
75 | } |
||
76 | |||
77 | try { |
||
78 | $quantity = $this->newQuantityFromParts( $amount, $exactness, $margin, $unit ); |
||
79 | return $quantity; |
||
80 | } catch ( IllegalValueException $ex ) { |
||
81 | throw new ParseException( $ex->getMessage(), $value, self::FORMAT_NAME ); |
||
82 | } |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @return string|null |
||
87 | */ |
||
88 | 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 QuantityValue |
||
107 | */ |
||
108 | 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 InvalidArgumentException If $value is not a string |
||
142 | * @throws ParseException If $value does not match the expected pattern |
||
143 | * @return array list( $amount, $exactness, $margin, $unit ). |
||
144 | * Parts not present in $value will be null |
||
145 | */ |
||
146 | private function splitQuantityString( $value ) { |
||
182 | |||
183 | /** |
||
184 | * Returns a QuantityValue representing the given amount. |
||
185 | * The amount is assumed to be absolutely exact, that is, |
||
186 | * the upper and lower bound will be the same as the amount. |
||
187 | * |
||
188 | * @param DecimalValue $amount |
||
189 | * @param string $unit The quantity's unit (use "1" for unit-less quantities) |
||
190 | * |
||
191 | * @return QuantityValue |
||
192 | */ |
||
193 | private function newExactQuantity( DecimalValue $amount, $unit = '1' ) { |
||
199 | |||
200 | /** |
||
201 | * Returns a QuantityValue representing the given amount, automatically assuming |
||
202 | * a level of uncertainty based on the digits given. |
||
203 | * |
||
204 | * The upper and lower bounds are determined automatically from the given |
||
205 | * digits by increasing resp. decreasing the least significant digit. |
||
206 | * E.g. "+0.01" would have upperBound "+0.02" and lowerBound "+0.01", |
||
207 | * while "-100" would have upperBound "-99" and lowerBound "-101". |
||
208 | * |
||
209 | * @param DecimalValue $amount The quantity |
||
210 | * @param string $unit The quantity's unit (use "1" for unit-less quantities) |
||
211 | * @param DecimalValue $margin |
||
212 | * |
||
213 | * @return QuantityValue |
||
214 | */ |
||
215 | private function newUncertainQuantityFromMargin( DecimalValue $amount, $unit = '1', DecimalValue $margin ) { |
||
224 | |||
225 | /** |
||
226 | * Returns a QuantityValue representing the given amount, automatically assuming |
||
227 | * a level of uncertainty based on the digits given. |
||
228 | * |
||
229 | * The upper and lower bounds are determined automatically from the given |
||
230 | * digits by increasing resp. decreasing the least significant digit. |
||
231 | * E.g. "+0.01" would have upperBound "+0.02" and lowerBound "+0.01", |
||
232 | * while "-100" would have upperBound "-99" and lowerBound "-101". |
||
233 | * |
||
234 | * @param DecimalValue $amount The quantity |
||
235 | * @param string $unit The quantity's unit (use "1" for unit-less quantities) |
||
236 | * @param int $exponent Decimal exponent to apply |
||
237 | * |
||
238 | * @return QuantityValue |
||
239 | */ |
||
240 | private function newUncertainQuantityFromDigits( DecimalValue $amount, $unit = '1', $exponent = 0 ) { |
||
257 | |||
258 | } |
||
259 |