1 | <?php |
||
19 | class QuantityFormatter extends ValueFormatterBase { |
||
20 | |||
21 | /** |
||
22 | * Option key for enabling or disabling output of the uncertainty margin (e.g. "+/-5"). |
||
23 | * Per default, the uncertainty margin is included in the output. |
||
24 | * This option must have a boolean value. |
||
25 | */ |
||
26 | const OPT_SHOW_UNCERTAINTY_MARGIN = 'showQuantityUncertaintyMargin'; |
||
27 | |||
28 | /** |
||
29 | * Option key for determining what level of rounding to apply to the numbers |
||
30 | * included in the output. The value of this option must be an integer or a boolean. |
||
31 | * |
||
32 | * If an integer is given, this is the exponent of the last significant decimal digits |
||
33 | * - that is, -2 would round to two digits after the decimal point, and 1 would round |
||
34 | * to two digits before the decimal point. 0 would indicate rounding to integers. |
||
35 | * |
||
36 | * If the value is a boolean, false means no rounding at all (useful e.g. in diffs), |
||
37 | * and true means automatic rounding based on what $quantity->getOrderOfUncertainty() |
||
38 | * returns. |
||
39 | */ |
||
40 | const OPT_APPLY_ROUNDING = 'applyRounding'; |
||
41 | |||
42 | /** |
||
43 | * Option key controlling whether the quantity's unit of measurement should be included |
||
44 | * in the output. |
||
45 | * |
||
46 | * @since 0.5 |
||
47 | */ |
||
48 | const OPT_APPLY_UNIT = 'applyUnit'; |
||
49 | |||
50 | /** |
||
51 | * @var DecimalMath |
||
52 | */ |
||
53 | private $decimalMath; |
||
54 | |||
55 | /** |
||
56 | * @var DecimalFormatter |
||
57 | */ |
||
58 | private $decimalFormatter; |
||
59 | |||
60 | /** |
||
61 | * @var ValueFormatter|null |
||
62 | */ |
||
63 | private $vocabularyUriFormatter; |
||
64 | |||
65 | /** |
||
66 | * @var string |
||
67 | */ |
||
68 | private $quantityWithUnitFormat; |
||
69 | |||
70 | /** |
||
71 | * @since 0.6 |
||
72 | * |
||
73 | * @param FormatterOptions|null $options |
||
74 | * @param DecimalFormatter|null $decimalFormatter |
||
75 | * @param ValueFormatter|null $vocabularyUriFormatter |
||
76 | * @param string|null $quantityWithUnitFormat Format string with two placeholders, $1 for the |
||
77 | * number and $2 for the unit. Warning, this must be under the control of the application, not |
||
78 | * under the control of the user, because it allows HTML injections in subclasses that return |
||
79 | * HTML. |
||
80 | */ |
||
81 | public function __construct( |
||
82 | FormatterOptions $options = null, |
||
83 | DecimalFormatter $decimalFormatter = null, |
||
84 | ValueFormatter $vocabularyUriFormatter = null, |
||
85 | $quantityWithUnitFormat = null |
||
86 | ) { |
||
87 | parent::__construct( $options ); |
||
88 | |||
89 | $this->defaultOption( self::OPT_SHOW_UNCERTAINTY_MARGIN, true ); |
||
90 | $this->defaultOption( self::OPT_APPLY_ROUNDING, true ); |
||
91 | $this->defaultOption( self::OPT_APPLY_UNIT, true ); |
||
92 | |||
93 | $this->decimalFormatter = $decimalFormatter ?: new DecimalFormatter( $this->options ); |
||
94 | $this->vocabularyUriFormatter = $vocabularyUriFormatter; |
||
95 | $this->quantityWithUnitFormat = $quantityWithUnitFormat ?: '$1 $2'; |
||
96 | |||
97 | // plain composition should be sufficient |
||
98 | $this->decimalMath = new DecimalMath(); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @since 0.6 |
||
103 | * |
||
104 | * @return string |
||
105 | */ |
||
106 | final protected function getQuantityWithUnitFormat() { |
||
109 | |||
110 | /** |
||
111 | * @see ValueFormatter::format |
||
112 | * |
||
113 | * @param QuantityValue $value |
||
114 | * |
||
115 | * @throws InvalidArgumentException |
||
116 | * @return string Text |
||
117 | */ |
||
118 | public function format( $value ) { |
||
125 | |||
126 | /** |
||
127 | * @since 0.6 |
||
128 | * |
||
129 | * @param QuantityValue $quantity |
||
130 | * |
||
131 | * @return string Text |
||
132 | */ |
||
133 | protected function formatQuantityValue( QuantityValue $quantity ) { |
||
149 | |||
150 | /** |
||
151 | * @since 0.6 |
||
152 | * |
||
153 | * @param QuantityValue $quantity |
||
154 | * |
||
155 | * @return string Text |
||
156 | */ |
||
157 | protected function formatNumber( QuantityValue $quantity ) { |
||
179 | |||
180 | /** |
||
181 | * Returns the rounding exponent based on the given $quantity |
||
182 | * and the @see QuantityFormatter::OPT_APPLY_ROUNDING option. |
||
183 | * |
||
184 | * @param QuantityValue $quantity |
||
185 | * |
||
186 | * @return int|null |
||
187 | */ |
||
188 | private function getRoundingExponent( QuantityValue $quantity ) { |
||
200 | |||
201 | /** |
||
202 | * @param DecimalValue $decimal |
||
203 | * |
||
204 | * @return string |
||
205 | */ |
||
206 | private function formatMinimalDecimal( DecimalValue $decimal ) { |
||
212 | |||
213 | /** |
||
214 | * @param DecimalValue $margin |
||
215 | * @param int $roundingExponent |
||
216 | * |
||
217 | * @return string|null Text |
||
218 | */ |
||
219 | private function formatMargin( DecimalValue $margin, $roundingExponent ) { |
||
231 | |||
232 | /** |
||
233 | * @since 0.6 |
||
234 | * |
||
235 | * @param string $unit URI |
||
236 | * |
||
237 | * @return string|null Text |
||
238 | */ |
||
239 | protected function formatUnit( $unit ) { |
||
250 | |||
251 | } |
||
252 |