1 | <?php |
||
20 | class QuantityFormatter extends ValueFormatterBase { |
||
21 | |||
22 | /** |
||
23 | * Option key for enabling or disabling output of the uncertainty margin (e.g. "+/-5"). |
||
24 | * Per default, the uncertainty margin is included in the output. |
||
25 | * This option must have a boolean value. |
||
26 | */ |
||
27 | const OPT_SHOW_UNCERTAINTY_MARGIN = 'showQuantityUncertaintyMargin'; |
||
28 | |||
29 | /** |
||
30 | * Option key for determining what level of rounding to apply to the numbers |
||
31 | * included in the output. The value of this option must be an integer or a boolean. |
||
32 | * |
||
33 | * If an integer is given, this is the exponent of the last significant decimal digits |
||
34 | * - that is, -2 would round to two digits after the decimal point, and 1 would round |
||
35 | * to two digits before the decimal point. 0 would indicate rounding to integers. |
||
36 | * |
||
37 | * If the value is a boolean, false means no rounding at all (useful e.g. in diffs), |
||
38 | * and true means automatic rounding based on what $quantity->getOrderOfUncertainty() |
||
39 | * returns. |
||
40 | */ |
||
41 | const OPT_APPLY_ROUNDING = 'applyRounding'; |
||
42 | |||
43 | /** |
||
44 | * Option key controlling whether the quantity's unit of measurement should be included |
||
45 | * in the output. |
||
46 | * |
||
47 | * @since 0.5 |
||
48 | */ |
||
49 | const OPT_APPLY_UNIT = 'applyUnit'; |
||
50 | |||
51 | /** |
||
52 | * @var DecimalMath |
||
53 | */ |
||
54 | private $decimalMath; |
||
55 | |||
56 | /** |
||
57 | * @var DecimalFormatter |
||
58 | */ |
||
59 | private $decimalFormatter; |
||
60 | |||
61 | /** |
||
62 | * @var ValueFormatter|null |
||
63 | */ |
||
64 | private $vocabularyUriFormatter; |
||
65 | |||
66 | /** |
||
67 | * @var string |
||
68 | */ |
||
69 | private $quantityWithUnitFormat; |
||
70 | |||
71 | /** |
||
72 | * @since 0.6 |
||
73 | * |
||
74 | * @param FormatterOptions|null $options |
||
75 | * @param DecimalFormatter|null $decimalFormatter |
||
76 | * @param ValueFormatter|null $vocabularyUriFormatter |
||
77 | * @param string|null $quantityWithUnitFormat Format string with two placeholders, $1 for the |
||
78 | * number and $2 for the unit. Warning, this must be under the control of the application, not |
||
79 | * under the control of the user, because it allows HTML injections in subclasses that return |
||
80 | * HTML. |
||
81 | */ |
||
82 | public function __construct( |
||
101 | |||
102 | /** |
||
103 | * @since 0.6 |
||
104 | * |
||
105 | * @return string |
||
106 | */ |
||
107 | final protected function getQuantityWithUnitFormat() { |
||
110 | |||
111 | /** |
||
112 | * @see ValueFormatter::format |
||
113 | * |
||
114 | * @param UnboundedQuantityValue $value |
||
115 | * |
||
116 | * @throws InvalidArgumentException |
||
117 | * @return string Text |
||
118 | */ |
||
119 | public function format( $value ) { |
||
126 | |||
127 | /** |
||
128 | * @since 0.6 |
||
129 | * |
||
130 | * @param UnboundedQuantityValue $quantity |
||
131 | * |
||
132 | * @return string Text |
||
133 | */ |
||
134 | protected function formatQuantityValue( UnboundedQuantityValue $quantity ) { |
||
150 | |||
151 | /** |
||
152 | * @since 0.6 |
||
153 | * |
||
154 | * @param UnboundedQuantityValue $quantity |
||
155 | * |
||
156 | * @return string Text |
||
157 | */ |
||
158 | protected function formatNumber( UnboundedQuantityValue $quantity ) { |
||
184 | |||
185 | /** |
||
186 | * Returns the rounding exponent based on the given $quantity |
||
187 | * and the @see QuantityFormatter::OPT_APPLY_ROUNDING option. |
||
188 | * |
||
189 | * @param QuantityValue $quantity |
||
190 | * |
||
191 | * @return int|null |
||
192 | */ |
||
193 | private function getRoundingExponent( UnboundedQuantityValue $quantity ) { |
||
206 | |||
207 | /** |
||
208 | * @param DecimalValue $decimal |
||
209 | * |
||
210 | * @return string |
||
211 | */ |
||
212 | private function formatMinimalDecimal( DecimalValue $decimal ) { |
||
218 | |||
219 | /** |
||
220 | * @param DecimalValue $margin |
||
221 | * @param int $roundingExponent |
||
222 | * |
||
223 | * @return string|null Text |
||
224 | */ |
||
225 | private function formatMargin( DecimalValue $margin, $roundingExponent ) { |
||
237 | |||
238 | /** |
||
239 | * @since 0.6 |
||
240 | * |
||
241 | * @param string $unit URI |
||
242 | * |
||
243 | * @return string|null Text |
||
244 | */ |
||
245 | protected function formatUnit( $unit ) { |
||
256 | |||
257 | } |
||
258 |