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 | | Init Functions |
||
71 | | ------------------------------------------------------------------------------------------------ |
||
72 | */ |
||
73 | /** |
||
74 | * Make a distance instance. |
||
75 | * |
||
76 | * @param float|int $value |
||
77 | * @param string $unit |
||
78 | * @param array $options |
||
79 | * |
||
80 | * @return static |
||
81 | */ |
||
82 | 384 | public static function make($value = 0, $unit = null, array $options = []) |
|
86 | |||
87 | /** |
||
88 | * Initialize the unit. |
||
89 | * |
||
90 | * @param float|int $value |
||
91 | * @param string $unit |
||
92 | * @param array $options |
||
93 | */ |
||
94 | 960 | protected function init($value, $unit, array $options) |
|
106 | |||
107 | /* ------------------------------------------------------------------------------------------------ |
||
108 | | Getters & Setters |
||
109 | | ------------------------------------------------------------------------------------------------ |
||
110 | */ |
||
111 | /** |
||
112 | * Get the unit value. |
||
113 | * |
||
114 | * @return float|int |
||
115 | */ |
||
116 | 784 | public function value() |
|
120 | |||
121 | /** |
||
122 | * Set the unit value. |
||
123 | * |
||
124 | * @param float|int $value |
||
125 | * |
||
126 | * @return static |
||
127 | */ |
||
128 | 960 | public function setValue($value) |
|
134 | |||
135 | /** |
||
136 | * Get the default units. |
||
137 | * |
||
138 | * @return array |
||
139 | */ |
||
140 | 960 | public static function units() |
|
147 | |||
148 | /** |
||
149 | * Get the unit key. |
||
150 | * |
||
151 | * @return string |
||
152 | */ |
||
153 | 544 | public function unit() |
|
157 | |||
158 | /** |
||
159 | * Set the unit key. |
||
160 | * |
||
161 | * @param string $unit |
||
162 | * |
||
163 | * @return static |
||
164 | */ |
||
165 | 960 | public function setUnit($unit) |
|
173 | |||
174 | /** |
||
175 | * Get the unit symbols. |
||
176 | * |
||
177 | * @return array |
||
178 | */ |
||
179 | 264 | public function symbols() |
|
183 | |||
184 | /** |
||
185 | * Get the default symbols. |
||
186 | * |
||
187 | * @return array |
||
188 | */ |
||
189 | 664 | protected static function defaultSymbols() |
|
193 | |||
194 | /** |
||
195 | * Set the unit symbols. |
||
196 | * |
||
197 | * @param array $symbols |
||
198 | * |
||
199 | * @return static |
||
200 | */ |
||
201 | 960 | public function setSymbols(array $symbols) |
|
211 | |||
212 | /** |
||
213 | * Get the unit symbol. |
||
214 | * |
||
215 | * @return string |
||
216 | */ |
||
217 | 144 | public function symbol() |
|
221 | |||
222 | /** |
||
223 | * Set the unit symbol. |
||
224 | * |
||
225 | * @param string $unit |
||
226 | * @param string $symbol |
||
227 | * |
||
228 | * @return static |
||
229 | */ |
||
230 | 960 | public function setSymbol($unit, $symbol) |
|
238 | |||
239 | /** |
||
240 | * Get the unit names. |
||
241 | * |
||
242 | * @return array |
||
243 | */ |
||
244 | 216 | public function names() |
|
248 | |||
249 | /** |
||
250 | * Get the default names. |
||
251 | * |
||
252 | * @return array |
||
253 | */ |
||
254 | abstract protected function defaultNames(); |
||
255 | |||
256 | /** |
||
257 | * Set the unit names. |
||
258 | * |
||
259 | * @param array $names |
||
260 | * |
||
261 | * @return static |
||
262 | */ |
||
263 | 960 | public function setNames(array $names) |
|
273 | |||
274 | /** |
||
275 | * Get the unit name. |
||
276 | * |
||
277 | * @return string |
||
278 | */ |
||
279 | 48 | public function name() |
|
283 | |||
284 | /** |
||
285 | * Get the name by a given unit. |
||
286 | * |
||
287 | * @param string $unit |
||
288 | * |
||
289 | * @return string |
||
290 | */ |
||
291 | 96 | public function getName($unit) |
|
297 | |||
298 | /** |
||
299 | * Set the unit name. |
||
300 | * |
||
301 | * @param string $unit |
||
302 | * @param string $name |
||
303 | * |
||
304 | * @return static |
||
305 | */ |
||
306 | 960 | public function setName($unit, $name) |
|
314 | |||
315 | /** |
||
316 | * Set the format. |
||
317 | * |
||
318 | * @param int $decimals |
||
319 | * @param string $decimalSeparator |
||
320 | * @param string $thousandsSeparator |
||
321 | * |
||
322 | * @return static |
||
323 | */ |
||
324 | 960 | public function setFormat($decimals = 0, $decimalSeparator = ',', $thousandsSeparator = '.') |
|
332 | |||
333 | /* ------------------------------------------------------------------------------------------------ |
||
334 | | Main Functions |
||
335 | | ------------------------------------------------------------------------------------------------ |
||
336 | */ |
||
337 | /** |
||
338 | * Convert the unit to the given unit key. |
||
339 | * |
||
340 | * @param string $to |
||
341 | * |
||
342 | * @return \Arcanedev\Units\Contracts\UnitMeasure |
||
343 | */ |
||
344 | 408 | public function to($to) |
|
360 | |||
361 | /** |
||
362 | * Convert the unit. |
||
363 | * |
||
364 | * @param string $from |
||
365 | * @param string $to |
||
366 | * @param float|int $value |
||
367 | * |
||
368 | * @return float|int |
||
369 | */ |
||
370 | 120 | public static function convert($from, $to, $value) |
|
374 | |||
375 | /** |
||
376 | * Format the unit. |
||
377 | * |
||
378 | * @param int|null $decimals |
||
379 | * @param string|null $decimalSeparator |
||
380 | * @param string|null $thousandsSeparator |
||
381 | * |
||
382 | * @return string |
||
383 | */ |
||
384 | 144 | public function format( |
|
395 | |||
396 | /** |
||
397 | * Format the unit with symbol. |
||
398 | * |
||
399 | * @param int|null $decimals |
||
400 | * @param string|null $decimalSeparator |
||
401 | * @param string|null $thousandsSeparator |
||
402 | * |
||
403 | * @return string |
||
404 | */ |
||
405 | 96 | public function formatWithSymbol( |
|
412 | |||
413 | /** |
||
414 | * Convert object to string. |
||
415 | * |
||
416 | * @return string |
||
417 | */ |
||
418 | 48 | public function __toString() |
|
422 | |||
423 | /* ------------------------------------------------------------------------------------------------ |
||
424 | | Check Functions |
||
425 | | ------------------------------------------------------------------------------------------------ |
||
426 | */ |
||
427 | /** |
||
428 | * Get the unit ratio. |
||
429 | * |
||
430 | * @param string $to |
||
431 | * @param string $from |
||
432 | * |
||
433 | * @return float|int |
||
434 | */ |
||
435 | 48 | protected static function getRatio($to, $from) |
|
446 | |||
447 | /** |
||
448 | * Get all the unit ratios. |
||
449 | * |
||
450 | * @codeCoverageIgnore |
||
451 | * |
||
452 | * @return array |
||
453 | */ |
||
454 | protected static function getRatios() |
||
458 | |||
459 | /** |
||
460 | * Check the weight unit. |
||
461 | * |
||
462 | * @param string $unit |
||
463 | */ |
||
464 | 960 | protected static function checkUnit($unit) |
|
474 | } |
||
475 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.