1
|
|
|
<?php namespace Arcanedev\Units\Bases; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Units\Contracts\UnitMeasure as UnitMeasureContract; |
4
|
|
|
use Arcanedev\Units\Exceptions\InvalidUnitException; |
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use ReflectionClass; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class UnitMeasure |
10
|
|
|
* |
11
|
|
|
* @package Arcanedev\Units\Base |
12
|
|
|
* @author ARCANEDEV <[email protected]> |
13
|
|
|
*/ |
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 Function |
71
|
|
|
| ------------------------------------------------------------------------------------------------ |
72
|
|
|
*/ |
73
|
|
|
/** |
74
|
|
|
* Initialize the unit. |
75
|
|
|
* |
76
|
|
|
* @param float|int $value |
77
|
|
|
* @param string $unit |
78
|
|
|
* @param array $options |
79
|
|
|
*/ |
80
|
528 |
|
protected function init($value, $unit, array $options) |
81
|
|
|
{ |
82
|
528 |
|
$this->setValue($value); |
83
|
528 |
|
$this->setUnit($unit); |
84
|
528 |
|
$this->setSymbols(Arr::get($options, 'symbols', [])); |
85
|
528 |
|
$this->setNames(Arr::get($options, 'names', [])); |
86
|
528 |
|
$this->setFormat( |
87
|
528 |
|
Arr::get($options, 'decimals', 0), |
88
|
528 |
|
Arr::get($options, 'separators.decimal', ','), |
89
|
528 |
|
Arr::get($options, 'separators.thousands', '.') |
90
|
396 |
|
); |
91
|
528 |
|
} |
92
|
|
|
|
93
|
|
|
/* ------------------------------------------------------------------------------------------------ |
94
|
|
|
| Getters & Setters |
95
|
|
|
| ------------------------------------------------------------------------------------------------ |
96
|
|
|
*/ |
97
|
|
|
/** |
98
|
|
|
* Get the unit value. |
99
|
|
|
* |
100
|
|
|
* @return float|int |
101
|
|
|
*/ |
102
|
408 |
|
public function value() |
103
|
|
|
{ |
104
|
408 |
|
return $this->value; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Set the unit value. |
109
|
|
|
* |
110
|
|
|
* @param float|int $value |
111
|
|
|
* |
112
|
|
|
* @return static |
113
|
|
|
*/ |
114
|
528 |
|
public function setValue($value) |
115
|
|
|
{ |
116
|
528 |
|
$this->value = $value; |
117
|
|
|
|
118
|
528 |
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get the default units. |
123
|
|
|
* |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
528 |
|
public static function units() |
127
|
|
|
{ |
128
|
528 |
|
$constants = (new ReflectionClass(get_called_class())) |
129
|
528 |
|
->getConstants(); |
130
|
|
|
|
131
|
528 |
|
return array_values($constants); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get the unit key. |
136
|
|
|
* |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
288 |
|
public function unit() |
140
|
|
|
{ |
141
|
288 |
|
return $this->unit; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Set the unit key. |
146
|
|
|
* |
147
|
|
|
* @param string $unit |
148
|
|
|
* |
149
|
|
|
* @return static |
150
|
|
|
*/ |
151
|
528 |
|
public function setUnit($unit) |
152
|
|
|
{ |
153
|
528 |
|
static::checkUnit($unit); |
154
|
|
|
|
155
|
528 |
|
$this->unit = $unit; |
156
|
|
|
|
157
|
528 |
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get the unit symbols. |
162
|
|
|
* |
163
|
|
|
* @return array |
164
|
|
|
*/ |
165
|
96 |
|
public function symbols() |
166
|
|
|
{ |
167
|
96 |
|
return $this->symbols; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Get the default symbols. |
172
|
|
|
* |
173
|
|
|
* @return array |
174
|
|
|
*/ |
175
|
504 |
|
protected static function defaultSymbols() |
176
|
|
|
{ |
177
|
504 |
|
return array_combine(static::units(), static::units()); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Set the unit symbols. |
182
|
|
|
* |
183
|
|
|
* @param array $symbols |
184
|
|
|
* |
185
|
|
|
* @return static |
186
|
|
|
*/ |
187
|
528 |
|
public function setSymbols(array $symbols) |
188
|
|
|
{ |
189
|
528 |
|
if (empty($symbols)) $symbols = static::defaultSymbols(); |
190
|
|
|
|
191
|
528 |
|
foreach ($symbols as $unit => $symbol) { |
192
|
528 |
|
$this->setSymbol($unit, $symbol); |
193
|
396 |
|
} |
194
|
|
|
|
195
|
528 |
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Get the unit symbol. |
200
|
|
|
* |
201
|
|
|
* @return string |
202
|
|
|
*/ |
203
|
72 |
|
public function symbol() |
204
|
|
|
{ |
205
|
72 |
|
return Arr::get($this->symbols(), $this->unit()); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Set the unit symbol. |
210
|
|
|
* |
211
|
|
|
* @param string $unit |
212
|
|
|
* @param string $symbol |
213
|
|
|
* |
214
|
|
|
* @return static |
215
|
|
|
*/ |
216
|
528 |
|
public function setSymbol($unit, $symbol) |
217
|
|
|
{ |
218
|
528 |
|
static::checkUnit($unit); |
219
|
|
|
|
220
|
528 |
|
$this->symbols[$unit] = $symbol; |
221
|
|
|
|
222
|
528 |
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Get the unit names. |
227
|
|
|
* |
228
|
|
|
* @return array |
229
|
|
|
*/ |
230
|
48 |
|
public function names() |
231
|
|
|
{ |
232
|
48 |
|
return $this->names; |
233
|
|
|
} |
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
|
|
|
* @param array $names |
246
|
|
|
* |
247
|
|
|
* @return static |
248
|
|
|
*/ |
249
|
528 |
|
public function setNames(array $names) |
250
|
|
|
{ |
251
|
528 |
|
if (empty($symbols)) $names = $this->defaultNames(); |
|
|
|
|
252
|
|
|
|
253
|
528 |
|
foreach ($names as $unit => $name) { |
254
|
528 |
|
$this->setName($unit, $name); |
255
|
396 |
|
} |
256
|
|
|
|
257
|
528 |
|
return $this; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Get the unit name. |
262
|
|
|
* |
263
|
|
|
* @return string |
264
|
|
|
*/ |
265
|
24 |
|
public function name() |
266
|
|
|
{ |
267
|
24 |
|
return $this->getName($this->unit()); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Get the name by a given unit. |
272
|
|
|
* |
273
|
|
|
* @param string $unit |
274
|
|
|
* |
275
|
|
|
* @return string |
276
|
|
|
*/ |
277
|
48 |
|
public function getName($unit) |
278
|
|
|
{ |
279
|
48 |
|
static::checkUnit($unit); |
280
|
|
|
|
281
|
48 |
|
return Arr::get($this->names(), $unit); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Set the unit name. |
286
|
|
|
* |
287
|
|
|
* @param string $unit |
288
|
|
|
* @param string $name |
289
|
|
|
* |
290
|
|
|
* @return static |
291
|
|
|
*/ |
292
|
528 |
|
public function setName($unit, $name) |
293
|
|
|
{ |
294
|
528 |
|
static::checkUnit($unit); |
295
|
|
|
|
296
|
528 |
|
$this->names[$unit] = $name; |
297
|
|
|
|
298
|
528 |
|
return $this; |
299
|
|
|
} |
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
|
528 |
|
public function setFormat($decimals = 0, $decimalSeparator = ',', $thousandsSeparator = '.') |
311
|
|
|
{ |
312
|
528 |
|
$this->decimals = $decimals; |
313
|
528 |
|
$this->decimalSeparator = $decimalSeparator; |
314
|
528 |
|
$this->thousandsSeparator = $thousandsSeparator; |
315
|
|
|
|
316
|
528 |
|
return $this; |
317
|
|
|
} |
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
|
72 |
|
public function format( |
333
|
|
|
$decimals = null, |
334
|
|
|
$decimalSeparator = null, |
335
|
|
|
$thousandsSeparator = null |
336
|
|
|
) { |
337
|
72 |
|
return number_format($this->value(), |
338
|
72 |
|
is_null($decimals) ? $this->decimals : $decimals, |
339
|
72 |
|
is_null($decimalSeparator) ? $this->decimalSeparator : $decimalSeparator, |
340
|
72 |
|
is_null($thousandsSeparator) ? $this->thousandsSeparator : $thousandsSeparator |
341
|
54 |
|
); |
342
|
|
|
} |
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
|
48 |
|
public function formatWithSymbol( |
354
|
|
|
$decimals = null, |
355
|
|
|
$decimalSeparator = null, |
356
|
|
|
$thousandsSeparator = null |
357
|
|
|
) { |
358
|
48 |
|
return $this->format($decimals, $decimalSeparator, $thousandsSeparator).' '.$this->symbol(); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* Convert object to string. |
363
|
|
|
* |
364
|
|
|
* @return string |
365
|
|
|
*/ |
366
|
24 |
|
public function __toString() |
367
|
|
|
{ |
368
|
24 |
|
return $this->formatWithSymbol(); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/* ------------------------------------------------------------------------------------------------ |
372
|
|
|
| Check Functions |
373
|
|
|
| ------------------------------------------------------------------------------------------------ |
374
|
|
|
*/ |
375
|
|
|
/** |
376
|
|
|
* Check the weight unit. |
377
|
|
|
* |
378
|
|
|
* @param string $unit |
379
|
|
|
*/ |
380
|
528 |
|
protected static function checkUnit($unit) |
381
|
|
|
{ |
382
|
528 |
|
if ( ! in_array($unit, static::units())) { |
383
|
24 |
|
$class = static::class; |
384
|
|
|
|
385
|
24 |
|
throw new InvalidUnitException( |
386
|
24 |
|
"Invalid unit of measurement [{$unit}] in $class." |
387
|
18 |
|
); |
388
|
|
|
} |
389
|
528 |
|
} |
390
|
|
|
} |
391
|
|
|
|
This check looks for calls to
isset(...)
orempty()
on variables that are yet undefined. These calls will always produce the same result and can be removed.This is most likely caused by the renaming of a variable or the removal of a function/method parameter.