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 |
||
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 $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|QuantityValue $value |
||
115 | * |
||
116 | * @throws InvalidArgumentException |
||
117 | * @return string Text |
||
118 | */ |
||
119 | public function format( $value ) { |
||
120 | if ( !( $value instanceof UnboundedQuantityValue ) ) { |
||
121 | throw new InvalidArgumentException( 'Data value type mismatch. Expected an UnboundedQuantityValue.' ); |
||
122 | } |
||
123 | |||
124 | $formatted = $this->formatNumber( $value ); |
||
125 | $unit = $this->formatUnit( $value->getUnit() ); |
||
126 | |||
127 | if ( $unit !== null ) { |
||
128 | $formatted = strtr( |
||
129 | $this->quantityWithUnitFormat, |
||
130 | array( |
||
131 | '$1' => $formatted, |
||
132 | '$2' => $unit |
||
133 | ) |
||
134 | ); |
||
135 | } |
||
136 | |||
137 | return $formatted; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @since 0.8.2 |
||
142 | * |
||
143 | * @param UnboundedQuantityValue $quantity |
||
144 | * |
||
145 | * @return string Text |
||
146 | */ |
||
147 | protected function formatNumber( UnboundedQuantityValue $quantity ) { |
||
148 | return $quantity instanceof QuantityValue |
||
149 | ? $this->formatQuantityValue( $quantity ) |
||
150 | : $this->formatUnboundedQuantityValue( $quantity ); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @param UnboundedQuantityValue $quantity |
||
155 | * |
||
156 | * @return string |
||
157 | */ |
||
158 | protected function formatUnboundedQuantityValue( UnboundedQuantityValue $quantity ) { |
||
168 | |||
169 | /** |
||
170 | * @param QuantityValue $quantity |
||
171 | * |
||
172 | * @return string Text |
||
173 | */ |
||
174 | private function formatQuantityValue( QuantityValue $quantity ) { |
||
196 | |||
197 | /** |
||
198 | * Returns the rounding exponent based on the given $quantity |
||
199 | * and the @see QuantityFormatter::OPT_APPLY_ROUNDING option. |
||
200 | * |
||
201 | * @param QuantityValue $quantity |
||
202 | * |
||
203 | * @return int|null |
||
204 | */ |
||
205 | private function getRoundingExponent( QuantityValue $quantity ) { |
||
217 | |||
218 | /** |
||
219 | * @param DecimalValue $decimal |
||
220 | * |
||
221 | * @return string |
||
222 | */ |
||
223 | private function formatMinimalDecimal( DecimalValue $decimal ) { |
||
227 | |||
228 | /** |
||
229 | * @param DecimalValue $margin |
||
230 | * @param int $roundingExponent |
||
231 | * |
||
232 | * @return string|null Text |
||
233 | */ |
||
234 | private function formatMargin( DecimalValue $margin, $roundingExponent ) { |
||
245 | |||
246 | /** |
||
247 | * @since 0.6 |
||
248 | * |
||
249 | * @param string $unit URI |
||
250 | * |
||
251 | * @return string|null Text |
||
252 | */ |
||
253 | protected function formatUnit( $unit ) { |
||
263 | |||
264 | } |
||
265 |