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 | * Value for the OPT_SHOW_UNCERTAINTY_MARGIN indicating that the uncertainty margin |
||
31 | * should not be shown. |
||
32 | */ |
||
33 | const SHOW_UNCERTAINTY_MARGIN_NEVER = 'never'; |
||
34 | |||
35 | /** |
||
36 | * Value for the OPT_SHOW_UNCERTAINTY_MARGIN indicating that the uncertainty margin |
||
37 | * should be shown if we are displaying a QuantityValue. |
||
38 | */ |
||
39 | const SHOW_UNCERTAINTY_MARGIN_IF_KNOWN = 'if-known'; |
||
40 | |||
41 | /** |
||
42 | * Value for the OPT_SHOW_UNCERTAINTY_MARGIN indicating that the uncertainty margin |
||
43 | * should be shown if we are displaying a non-exact QuantityValue. |
||
44 | */ |
||
45 | const SHOW_UNCERTAINTY_MARGIN_IF_NOT_ZERO = 'if-not-zero'; |
||
46 | |||
47 | /** |
||
48 | * Option key for determining what level of rounding to apply to the numbers |
||
49 | * included in the output. The value of this option must be an integer or a boolean. |
||
50 | * |
||
51 | * If an integer is given, this is the exponent of the last significant decimal digits |
||
52 | * - that is, -2 would round to two digits after the decimal point, and 1 would round |
||
53 | * to two digits before the decimal point. 0 would indicate rounding to integers. |
||
54 | * |
||
55 | * If the value is a boolean, false means no rounding at all (useful e.g. in diffs), |
||
56 | * and true means automatic rounding based on what $quantity->getOrderOfUncertainty() |
||
57 | * returns. |
||
58 | */ |
||
59 | const OPT_APPLY_ROUNDING = 'applyRounding'; |
||
60 | |||
61 | /** |
||
62 | * Option key controlling whether the quantity's unit of measurement should be included |
||
63 | * in the output. |
||
64 | * |
||
65 | * @since 0.5 |
||
66 | */ |
||
67 | const OPT_APPLY_UNIT = 'applyUnit'; |
||
68 | |||
69 | /** |
||
70 | * @var DecimalMath |
||
71 | */ |
||
72 | private $decimalMath; |
||
73 | |||
74 | /** |
||
75 | * @var DecimalFormatter |
||
76 | */ |
||
77 | private $decimalFormatter; |
||
78 | |||
79 | /** |
||
80 | * @var ValueFormatter|null |
||
81 | */ |
||
82 | private $vocabularyUriFormatter; |
||
83 | |||
84 | /** |
||
85 | * @var string |
||
86 | */ |
||
87 | private $quantityWithUnitFormat; |
||
88 | |||
89 | /** |
||
90 | * @since 0.6 |
||
91 | * |
||
92 | * @param FormatterOptions|null $options |
||
93 | * @param DecimalFormatter|null $decimalFormatter |
||
94 | * @param ValueFormatter|null $vocabularyUriFormatter |
||
95 | * @param string|null $quantityWithUnitFormat Format string with two placeholders, $1 for the |
||
96 | * number and $2 for the unit. Warning, this must be under the control of the application, not |
||
97 | * under the control of the user, because it allows HTML injections in subclasses that return |
||
98 | * HTML. |
||
99 | */ |
||
100 | public function __construct( |
||
122 | |||
123 | /** |
||
124 | * @since 0.6 |
||
125 | * |
||
126 | * @return string |
||
127 | */ |
||
128 | final protected function getQuantityWithUnitFormat() { |
||
131 | |||
132 | /** |
||
133 | * @see ValueFormatter::format |
||
134 | * |
||
135 | * @param UnboundedQuantityValue $value |
||
136 | * |
||
137 | * @throws InvalidArgumentException |
||
138 | * @return string Text |
||
139 | */ |
||
140 | public function format( $value ) { |
||
147 | |||
148 | /** |
||
149 | * @since 0.6 |
||
150 | * |
||
151 | * @param UnboundedQuantityValue $quantity |
||
152 | * |
||
153 | * @return string Text |
||
154 | */ |
||
155 | protected function formatQuantityValue( UnboundedQuantityValue $quantity ) { |
||
171 | |||
172 | /** |
||
173 | * @since 0.6 |
||
174 | * |
||
175 | * @param UnboundedQuantityValue $quantity |
||
176 | * |
||
177 | * @return string Text |
||
178 | */ |
||
179 | protected function formatNumber( UnboundedQuantityValue $quantity ) { |
||
220 | |||
221 | /** |
||
222 | * Returns the rounding exponent based on the given $quantity. |
||
223 | * |
||
224 | * @param UnboundedQuantityValue $quantity |
||
225 | * @param bool|int $roundingMode The rounding exponent, or true for rounding to the order of |
||
226 | * uncertainty (significant digits) of a QuantityValue, or false to not apply |
||
227 | * rounding (that is, round to all digits). |
||
228 | * |
||
229 | * @return int|null |
||
230 | */ |
||
231 | private function getRoundingExponent( UnboundedQuantityValue $quantity, $roundingMode ) { |
||
243 | |||
244 | /** |
||
245 | * @param DecimalValue $decimal |
||
246 | * |
||
247 | * @return string |
||
248 | */ |
||
249 | private function formatMinimalDecimal( DecimalValue $decimal ) { |
||
255 | |||
256 | /** |
||
257 | * @param UnboundedQuantityValue $quantity |
||
258 | * @param string $marginMode |
||
259 | * |
||
260 | * @return string|null Text |
||
261 | */ |
||
262 | private function formatMargin( UnboundedQuantityValue $quantity, $marginMode ) { |
||
282 | |||
283 | /** |
||
284 | * @since 0.6 |
||
285 | * |
||
286 | * @param string $unit URI |
||
287 | * |
||
288 | * @return string|null Text |
||
289 | */ |
||
290 | protected function formatUnit( $unit ) { |
||
301 | |||
302 | } |
||
303 |