1 | <?php namespace Arcanedev\Units\Bases; |
||
14 | abstract class UnitMeasure implements UnitMeasureContract |
||
15 | { |
||
16 | /* ----------------------------------------------------------------- |
||
17 | | Properties |
||
18 | | ----------------------------------------------------------------- |
||
19 | */ |
||
20 | |||
21 | /** |
||
22 | * The unit. |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $unit; |
||
27 | |||
28 | /** |
||
29 | * The value. |
||
30 | * |
||
31 | * @var float|int |
||
32 | */ |
||
33 | protected $value; |
||
34 | |||
35 | /** |
||
36 | * The unit symbols. |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $symbols = []; |
||
41 | |||
42 | /** |
||
43 | * The unit names. |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $names = []; |
||
48 | |||
49 | /** |
||
50 | * The number of decimals to format. |
||
51 | * |
||
52 | * @var int |
||
53 | */ |
||
54 | protected $decimals = 0; |
||
55 | |||
56 | /** |
||
57 | * The decimal separator. |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $decimalSeparator = '.'; |
||
62 | |||
63 | /** |
||
64 | * The thousands separator. |
||
65 | * |
||
66 | * @var string |
||
67 | */ |
||
68 | protected $thousandsSeparator = ','; |
||
69 | |||
70 | /* ----------------------------------------------------------------- |
||
71 | | Constructor |
||
72 | | ----------------------------------------------------------------- |
||
73 | */ |
||
74 | |||
75 | /** |
||
76 | * UnitMeasure constructor. |
||
77 | * |
||
78 | * @param float|int $value |
||
79 | * @param string $unit |
||
80 | * @param array $options |
||
81 | */ |
||
82 | 267 | public function __construct($value = 0, $unit = '', array $options = []) |
|
86 | |||
87 | /* ----------------------------------------------------------------- |
||
88 | | Main Methods |
||
89 | | ----------------------------------------------------------------- |
||
90 | */ |
||
91 | |||
92 | /** |
||
93 | * Make a distance instance. |
||
94 | * |
||
95 | * @param float|int $value |
||
96 | * @param string $unit |
||
97 | * @param array $options |
||
98 | * |
||
99 | * @return static |
||
100 | */ |
||
101 | 105 | public static function make($value = 0, $unit = null, array $options = []) |
|
105 | |||
106 | /** |
||
107 | * Initialize the unit. |
||
108 | * |
||
109 | * @param float|int $value |
||
110 | * @param string $unit |
||
111 | * @param array $options |
||
112 | */ |
||
113 | 267 | protected function init($value, $unit, array $options) |
|
114 | { |
||
115 | 267 | $this->setValue($value); |
|
116 | 267 | $this->setUnit($unit); |
|
117 | 267 | $this->setSymbols(Arr::get($options, 'symbols', [])); |
|
118 | 267 | $this->setNames(Arr::get($options, 'names', [])); |
|
119 | 267 | $this->setFormat( |
|
120 | 267 | Arr::get($options, 'format.decimals', 0), |
|
121 | 267 | Arr::get($options, 'format.decimal-separator', ','), |
|
122 | 267 | Arr::get($options, 'format.thousands-separator', '.') |
|
123 | ); |
||
124 | 267 | } |
|
125 | |||
126 | /* ------------------------------------------------------------------------------------------------ |
||
127 | | Getters & Setters |
||
128 | | ------------------------------------------------------------------------------------------------ |
||
129 | */ |
||
130 | /** |
||
131 | * Get the unit value. |
||
132 | * |
||
133 | * @return float|int |
||
134 | */ |
||
135 | 204 | public function value() |
|
139 | |||
140 | /** |
||
141 | * Set the unit value. |
||
142 | * |
||
143 | * @param float|int $value |
||
144 | * |
||
145 | * @return static |
||
146 | */ |
||
147 | 267 | public function setValue($value) |
|
153 | |||
154 | /** |
||
155 | * Get the default units. |
||
156 | * |
||
157 | * @return array |
||
158 | */ |
||
159 | 267 | public static function units() |
|
166 | |||
167 | /** |
||
168 | * Get the unit key. |
||
169 | * |
||
170 | * @return string |
||
171 | */ |
||
172 | 144 | public function unit() |
|
176 | |||
177 | /** |
||
178 | * Set the unit key. |
||
179 | * |
||
180 | * @param string $unit |
||
181 | * |
||
182 | * @return static |
||
183 | */ |
||
184 | 267 | public function setUnit($unit) |
|
192 | |||
193 | /** |
||
194 | * Get the unit symbols. |
||
195 | * |
||
196 | * @return array |
||
197 | */ |
||
198 | 72 | public function symbols() |
|
202 | |||
203 | /** |
||
204 | * Get the default symbols. |
||
205 | * |
||
206 | * @return array |
||
207 | */ |
||
208 | 252 | protected static function defaultSymbols() |
|
212 | |||
213 | /** |
||
214 | * Set the unit symbols. |
||
215 | * |
||
216 | * @param array $symbols |
||
217 | * |
||
218 | * @return static |
||
219 | */ |
||
220 | 267 | public function setSymbols(array $symbols) |
|
221 | { |
||
222 | 267 | if (empty($symbols)) $symbols = static::defaultSymbols(); |
|
223 | |||
224 | 267 | foreach ($symbols as $unit => $symbol) { |
|
225 | 267 | $this->setSymbol($unit, $symbol); |
|
226 | } |
||
227 | |||
228 | 267 | return $this; |
|
229 | } |
||
230 | |||
231 | /** |
||
232 | * Get the unit symbol. |
||
233 | * |
||
234 | * @return string |
||
235 | */ |
||
236 | 36 | public function symbol() |
|
240 | |||
241 | /** |
||
242 | * Set the unit symbol. |
||
243 | * |
||
244 | * @param string $unit |
||
245 | * @param string $symbol |
||
246 | * |
||
247 | * @return static |
||
248 | */ |
||
249 | 267 | public function setSymbol($unit, $symbol) |
|
257 | |||
258 | /** |
||
259 | * Get the unit names. |
||
260 | * |
||
261 | * @return array |
||
262 | */ |
||
263 | 57 | public function names() |
|
267 | |||
268 | /** |
||
269 | * Get the default names. |
||
270 | * |
||
271 | * @return array |
||
272 | */ |
||
273 | abstract public function defaultNames(); |
||
274 | |||
275 | /** |
||
276 | * Set the unit names. |
||
277 | * |
||
278 | * @param array $names |
||
279 | * |
||
280 | * @return static |
||
281 | */ |
||
282 | 267 | public function setNames(array $names) |
|
283 | { |
||
284 | 267 | if (empty($names)) $names = $this->defaultNames(); |
|
285 | |||
286 | 267 | foreach ($names as $unit => $name) { |
|
287 | 267 | $this->setName($unit, $name); |
|
288 | } |
||
289 | |||
290 | 267 | return $this; |
|
291 | } |
||
292 | |||
293 | /** |
||
294 | * Get the unit name. |
||
295 | * |
||
296 | * @return string |
||
297 | */ |
||
298 | 12 | public function name() |
|
302 | |||
303 | /** |
||
304 | * Get the name by a given unit. |
||
305 | * |
||
306 | * @param string $unit |
||
307 | * |
||
308 | * @return string |
||
309 | */ |
||
310 | 24 | public function getName($unit) |
|
316 | |||
317 | /** |
||
318 | * Set the unit name. |
||
319 | * |
||
320 | * @param string $unit |
||
321 | * @param string $name |
||
322 | * |
||
323 | * @return static |
||
324 | */ |
||
325 | 267 | public function setName($unit, $name) |
|
333 | |||
334 | /** |
||
335 | * Set the format. |
||
336 | * |
||
337 | * @param int $decimals |
||
338 | * @param string $decimalSeparator |
||
339 | * @param string $thousandsSeparator |
||
340 | * |
||
341 | * @return static |
||
342 | */ |
||
343 | 267 | public function setFormat($decimals = 0, $decimalSeparator = ',', $thousandsSeparator = '.') |
|
351 | |||
352 | /* ------------------------------------------------------------------------------------------------ |
||
353 | | Main Functions |
||
354 | | ------------------------------------------------------------------------------------------------ |
||
355 | */ |
||
356 | /** |
||
357 | * Convert the unit to the given unit key. |
||
358 | * |
||
359 | * @param string $to |
||
360 | * |
||
361 | * @return \Arcanedev\Units\Contracts\UnitMeasure |
||
362 | */ |
||
363 | 105 | public function to($to) |
|
364 | { |
||
365 | 105 | if ($to === $this->unit()) return $this; |
|
366 | |||
367 | 33 | $value = static::convert($this->unit(), $to, $this->value()); |
|
368 | |||
369 | 33 | return static::make($value, $to, [ |
|
370 | 33 | 'symbols' => $this->symbols(), |
|
371 | 33 | 'names' => $this->names(), |
|
372 | 'format' => [ |
||
373 | 33 | 'decimals' => $this->decimals, |
|
374 | 33 | 'decimal-separator' => $this->decimalSeparator, |
|
375 | 33 | 'thousands-separator' => $this->thousandsSeparator, |
|
376 | ], |
||
377 | ]); |
||
378 | } |
||
379 | |||
380 | /** |
||
381 | * Convert the unit. |
||
382 | * |
||
383 | * @param string $from |
||
384 | * @param string $to |
||
385 | * @param float|int $value |
||
386 | * |
||
387 | * @return float|int |
||
388 | */ |
||
389 | 33 | public static function convert($from, $to, $value) |
|
393 | |||
394 | /** |
||
395 | * Format the unit. |
||
396 | * |
||
397 | * @param int|null $decimals |
||
398 | * @param string|null $decimalSeparator |
||
399 | * @param string|null $thousandsSeparator |
||
400 | * |
||
401 | * @return string |
||
402 | */ |
||
403 | 36 | public function format( |
|
404 | $decimals = null, |
||
405 | $decimalSeparator = null, |
||
406 | $thousandsSeparator = null |
||
407 | ) { |
||
408 | 36 | return number_format($this->value(), |
|
409 | 36 | is_null($decimals) ? $this->decimals : $decimals, |
|
410 | 36 | is_null($decimalSeparator) ? $this->decimalSeparator : $decimalSeparator, |
|
411 | 36 | is_null($thousandsSeparator) ? $this->thousandsSeparator : $thousandsSeparator |
|
412 | ); |
||
413 | } |
||
414 | |||
415 | /** |
||
416 | * Format the unit with symbol. |
||
417 | * |
||
418 | * @param int|null $decimals |
||
419 | * @param string|null $decimalSeparator |
||
420 | * @param string|null $thousandsSeparator |
||
421 | * |
||
422 | * @return string |
||
423 | */ |
||
424 | 24 | public function formatWithSymbol( |
|
431 | |||
432 | /** |
||
433 | * Convert object to string. |
||
434 | * |
||
435 | * @return string |
||
436 | */ |
||
437 | 12 | public function __toString() |
|
441 | |||
442 | /* ------------------------------------------------------------------------------------------------ |
||
443 | | Check Functions |
||
444 | | ------------------------------------------------------------------------------------------------ |
||
445 | */ |
||
446 | /** |
||
447 | * Get the unit ratio. |
||
448 | * |
||
449 | * @param string $to |
||
450 | * @param string $from |
||
451 | * |
||
452 | * @return float|int |
||
453 | */ |
||
454 | 18 | protected static function getRatio($to, $from) |
|
465 | |||
466 | /** |
||
467 | * Get all the unit ratios. |
||
468 | * |
||
469 | * @codeCoverageIgnore |
||
470 | * |
||
471 | * @return array |
||
472 | */ |
||
473 | protected static function getRatios() |
||
477 | |||
478 | /** |
||
479 | * Check the weight unit. |
||
480 | * |
||
481 | * @param string $unit |
||
482 | */ |
||
483 | 267 | protected static function checkUnit($unit) |
|
493 | } |
||
494 |