1 | <?php |
||
20 | class QuantityFormatter implements ValueFormatter { |
||
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 | public 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 | public 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 | public const OPT_APPLY_UNIT = 'applyUnit'; |
||
50 | |||
51 | /** |
||
52 | * @var FormatterOptions |
||
53 | */ |
||
54 | private $options; |
||
55 | |||
56 | /** |
||
57 | * @var DecimalMath |
||
58 | */ |
||
59 | private $decimalMath; |
||
60 | |||
61 | /** |
||
62 | * @var DecimalFormatter |
||
63 | */ |
||
64 | private $decimalFormatter; |
||
65 | |||
66 | /** |
||
67 | * @var ValueFormatter |
||
68 | */ |
||
69 | private $vocabularyUriFormatter; |
||
70 | |||
71 | /** |
||
72 | * @var string |
||
73 | */ |
||
74 | private $quantityWithUnitFormat; |
||
75 | |||
76 | /** |
||
77 | * @since 0.6 |
||
78 | * |
||
79 | * @param FormatterOptions|null $options |
||
80 | * @param DecimalFormatter|null $decimalFormatter |
||
81 | * @param ValueFormatter $vocabularyUriFormatter |
||
82 | 61 | * @param string|null $quantityWithUnitFormat Format string with two placeholders, $1 for the |
|
83 | * number and $2 for the unit. Warning, this must be under the control of the application, not |
||
84 | * under the control of the user, because it allows HTML injections in subclasses that return |
||
85 | * HTML. |
||
86 | */ |
||
87 | public function __construct( |
||
107 | |||
108 | /** |
||
109 | * @since 0.6 |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | final protected function getQuantityWithUnitFormat() { |
||
116 | |||
117 | /** |
||
118 | * @see ValueFormatter::format |
||
119 | 61 | * |
|
120 | 61 | * @param UnboundedQuantityValue|QuantityValue $value |
|
121 | * |
||
122 | * @throws InvalidArgumentException |
||
123 | * @return string Text |
||
124 | 61 | */ |
|
125 | 61 | public function format( $value ) { |
|
145 | |||
146 | /** |
||
147 | 61 | * @since 0.8.2 |
|
148 | 61 | * |
|
149 | 47 | * @param UnboundedQuantityValue $quantity |
|
150 | 61 | * |
|
151 | * @return string Text |
||
152 | */ |
||
153 | protected function formatNumber( UnboundedQuantityValue $quantity ) { |
||
158 | 14 | ||
159 | 14 | /** |
|
160 | 14 | * @param UnboundedQuantityValue $quantity |
|
161 | * |
||
162 | 14 | * @return string |
|
163 | 8 | */ |
|
164 | protected function formatUnboundedQuantityValue( UnboundedQuantityValue $quantity ) { |
||
174 | 47 | ||
175 | 47 | /** |
|
176 | * @param QuantityValue $quantity |
||
177 | 47 | * |
|
178 | * @return string Text |
||
179 | 47 | */ |
|
180 | 26 | private function formatQuantityValue( QuantityValue $quantity ) { |
|
202 | |||
203 | /** |
||
204 | * Returns the rounding exponent based on the given $quantity |
||
205 | 47 | * and the @see QuantityFormatter::OPT_APPLY_ROUNDING option. |
|
206 | 47 | * |
|
207 | 41 | * @param QuantityValue $quantity |
|
208 | 24 | * |
|
209 | * @return int|null |
||
210 | 41 | */ |
|
211 | 6 | private function getRoundingExponent( QuantityValue $quantity ) { |
|
223 | 26 | ||
224 | /** |
||
225 | 26 | * @param DecimalValue $decimal |
|
226 | * |
||
227 | * @return string |
||
228 | */ |
||
229 | private function formatMinimalDecimal( DecimalValue $decimal ) { |
||
233 | |||
234 | 21 | /** |
|
235 | 21 | * @param DecimalValue $margin |
|
236 | * @param int $roundingExponent |
||
237 | * |
||
238 | * @return string|null Text |
||
239 | */ |
||
240 | private function formatMargin( DecimalValue $margin, $roundingExponent ) { |
||
251 | |||
252 | /** |
||
253 | 61 | * @since 0.6 |
|
254 | 61 | * |
|
255 | 60 | * @param string $unit URI |
|
256 | 61 | * |
|
257 | * @return string|null Text |
||
258 | 47 | */ |
|
259 | protected function formatUnit( $unit ) { |
||
269 | |||
270 | } |
||
271 |