1 | <?php namespace Arcanedev\Units\Bases; |
||
14 | abstract class UnitMeasure implements UnitMeasureContract |
||
15 | { |
||
16 | /* ------------------------------------------------------------------------------------------------ |
||
17 | | Properties |
||
18 | | ------------------------------------------------------------------------------------------------ |
||
19 | */ |
||
20 | /** |
||
21 | * The unit. |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $unit; |
||
26 | |||
27 | /** |
||
28 | * The value. |
||
29 | * |
||
30 | * @var float|int |
||
31 | */ |
||
32 | protected $value; |
||
33 | |||
34 | /** |
||
35 | * The unit symbols. |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $symbols = []; |
||
40 | |||
41 | /** |
||
42 | * The unit names. |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $names = []; |
||
47 | |||
48 | /** |
||
49 | * The number of decimals to format. |
||
50 | * |
||
51 | * @var int |
||
52 | */ |
||
53 | protected $decimals = 0; |
||
54 | |||
55 | /** |
||
56 | * The decimal separator. |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $decimalSeparator = '.'; |
||
61 | |||
62 | /** |
||
63 | * The thousands separator. |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | protected $thousandsSeparator = ','; |
||
68 | |||
69 | /* ------------------------------------------------------------------------------------------------ |
||
70 | 408 | | Init Function |
|
71 | | ------------------------------------------------------------------------------------------------ |
||
72 | 408 | */ |
|
73 | /** |
||
74 | * Initialize the unit. |
||
75 | * |
||
76 | * @param float|int $value |
||
77 | * @param string $unit |
||
78 | * @param array $options |
||
79 | */ |
||
80 | protected function init($value, $unit, array $options) |
||
81 | { |
||
82 | 528 | $this->setValue($value); |
|
83 | $this->setUnit($unit); |
||
84 | 528 | $this->setSymbols(Arr::get($options, 'symbols', [])); |
|
85 | $this->setNames(Arr::get($options, 'names', [])); |
||
86 | 528 | $this->setFormat( |
|
87 | Arr::get($options, 'format.decimals', 0), |
||
88 | Arr::get($options, 'format.decimal-separator', ','), |
||
89 | Arr::get($options, 'format.thousands-separator', '.') |
||
90 | ); |
||
91 | } |
||
92 | |||
93 | /* ------------------------------------------------------------------------------------------------ |
||
94 | 528 | | Getters & Setters |
|
95 | | ------------------------------------------------------------------------------------------------ |
||
96 | 528 | */ |
|
97 | 528 | /** |
|
98 | * Get the unit value. |
||
99 | 528 | * |
|
100 | * @return float|int |
||
101 | */ |
||
102 | public function value() |
||
103 | { |
||
104 | return $this->value; |
||
105 | } |
||
106 | |||
107 | 256 | /** |
|
108 | * Set the unit value. |
||
109 | 256 | * |
|
110 | * @param float|int $value |
||
111 | * |
||
112 | * @return static |
||
113 | */ |
||
114 | public function setValue($value) |
||
115 | { |
||
116 | $this->value = $value; |
||
117 | |||
118 | return $this; |
||
119 | 528 | } |
|
120 | |||
121 | 528 | /** |
|
122 | * Get the default units. |
||
123 | 528 | * |
|
124 | * @return array |
||
125 | 528 | */ |
|
126 | public static function units() |
||
127 | { |
||
128 | $constants = (new ReflectionClass(get_called_class())) |
||
129 | ->getConstants(); |
||
130 | |||
131 | return array_values($constants); |
||
132 | } |
||
133 | 96 | ||
134 | /** |
||
135 | 96 | * Get the unit key. |
|
136 | * |
||
137 | * @return string |
||
138 | */ |
||
139 | public function unit() |
||
140 | { |
||
141 | return $this->unit; |
||
142 | } |
||
143 | 72 | ||
144 | /** |
||
145 | 72 | * Set the unit key. |
|
146 | * |
||
147 | * @param string $unit |
||
148 | * |
||
149 | * @return static |
||
150 | */ |
||
151 | public function setUnit($unit) |
||
152 | { |
||
153 | static::checkUnit($unit); |
||
154 | |||
155 | $this->unit = $unit; |
||
156 | 528 | ||
157 | return $this; |
||
158 | 528 | } |
|
159 | |||
160 | 528 | /** |
|
161 | * Get the unit symbols. |
||
162 | 528 | * |
|
163 | * @return array |
||
164 | */ |
||
165 | public function symbols() |
||
166 | { |
||
167 | return $this->symbols; |
||
168 | } |
||
169 | |||
170 | 504 | /** |
|
171 | * Get the default symbols. |
||
172 | 504 | * |
|
173 | * @return array |
||
174 | */ |
||
175 | protected static function defaultSymbols() |
||
176 | { |
||
177 | return array_combine(static::units(), static::units()); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Set the unit symbols. |
||
182 | 528 | * |
|
183 | * @param array $symbols |
||
184 | 528 | * |
|
185 | * @return static |
||
186 | 528 | */ |
|
187 | 528 | public function setSymbols(array $symbols) |
|
188 | 396 | { |
|
189 | if (empty($symbols)) $symbols = static::defaultSymbols(); |
||
190 | 528 | ||
191 | foreach ($symbols as $unit => $symbol) { |
||
192 | $this->setSymbol($unit, $symbol); |
||
193 | } |
||
194 | |||
195 | return $this; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Get the unit symbol. |
||
200 | * |
||
201 | * @return string |
||
202 | 528 | */ |
|
203 | public function symbol() |
||
204 | 528 | { |
|
205 | 528 | return Arr::get($this->symbols(), $this->unit()); |
|
206 | 528 | } |
|
207 | |||
208 | 528 | /** |
|
209 | * Set the unit symbol. |
||
210 | * |
||
211 | * @param string $unit |
||
212 | * @param string $symbol |
||
213 | * |
||
214 | * @return static |
||
215 | */ |
||
216 | public function setSymbol($unit, $symbol) |
||
217 | { |
||
218 | static::checkUnit($unit); |
||
219 | |||
220 | $this->symbols[$unit] = $symbol; |
||
221 | |||
222 | return $this; |
||
223 | } |
||
224 | 72 | ||
225 | /** |
||
226 | * Get the unit names. |
||
227 | * |
||
228 | * @return array |
||
229 | 72 | */ |
|
230 | 72 | public function names() |
|
231 | 72 | { |
|
232 | 72 | return $this->names; |
|
233 | 54 | } |
|
234 | |||
235 | /** |
||
236 | * Get the default names. |
||
237 | * |
||
238 | * @return array |
||
239 | */ |
||
240 | abstract protected function defaultNames(); |
||
241 | |||
242 | /** |
||
243 | * Set the unit names. |
||
244 | * |
||
245 | 48 | * @param array $names |
|
246 | * |
||
247 | * @return static |
||
248 | */ |
||
249 | public function setNames(array $names) |
||
250 | 48 | { |
|
251 | if (empty($names)) $names = $this->defaultNames(); |
||
252 | |||
259 | |||
260 | 24 | /** |
|
261 | * Get the unit name. |
||
262 | * |
||
263 | * @return string |
||
264 | */ |
||
265 | public function name() |
||
269 | |||
270 | /** |
||
271 | * Get the name by a given unit. |
||
272 | 528 | * |
|
273 | * @param string $unit |
||
274 | 528 | * |
|
275 | 24 | * @return string |
|
276 | */ |
||
277 | 24 | public function getName($unit) |
|
283 | |||
284 | /** |
||
285 | * Set the unit name. |
||
286 | * |
||
287 | * @param string $unit |
||
288 | * @param string $name |
||
289 | * |
||
290 | * @return static |
||
291 | */ |
||
292 | public function setName($unit, $name) |
||
300 | |||
301 | /** |
||
302 | * Set the format. |
||
303 | * |
||
304 | * @param int $decimals |
||
305 | * @param string $decimalSeparator |
||
306 | * @param string $thousandsSeparator |
||
307 | * |
||
308 | * @return static |
||
309 | */ |
||
310 | public function setFormat($decimals = 0, $decimalSeparator = ',', $thousandsSeparator = '.') |
||
318 | |||
319 | /* ------------------------------------------------------------------------------------------------ |
||
320 | | Main Functions |
||
321 | | ------------------------------------------------------------------------------------------------ |
||
322 | */ |
||
323 | /** |
||
324 | * Format the weight. |
||
325 | * |
||
326 | * @param int|null $decimals |
||
327 | * @param string|null $decimalSeparator |
||
328 | * @param string|null $thousandsSeparator |
||
329 | * |
||
330 | * @return string |
||
331 | */ |
||
332 | public function format( |
||
343 | |||
344 | /** |
||
345 | * Format the weight with symbol. |
||
346 | * |
||
347 | * @param int|null $decimals |
||
348 | * @param string|null $decimalSeparator |
||
349 | * @param string|null $thousandsSeparator |
||
350 | * |
||
351 | * @return string |
||
352 | */ |
||
353 | public function formatWithSymbol( |
||
360 | |||
361 | /** |
||
362 | * Convert object to string. |
||
363 | * |
||
364 | * @return string |
||
365 | */ |
||
366 | public function __toString() |
||
370 | |||
371 | /* ------------------------------------------------------------------------------------------------ |
||
372 | | Check Functions |
||
373 | | ------------------------------------------------------------------------------------------------ |
||
374 | */ |
||
375 | /** |
||
376 | * Check the weight unit. |
||
377 | * |
||
378 | * @param string $unit |
||
379 | */ |
||
380 | protected static function checkUnit($unit) |
||
390 | } |
||
391 |