1
|
|
|
<?php namespace Arcanedev\Units\Bases; |
2
|
|
|
use Illuminate\Support\Arr; |
3
|
|
|
use InvalidArgumentException; |
4
|
|
|
use ReflectionClass; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class UnitMeasure |
8
|
|
|
* |
9
|
|
|
* @package Arcanedev\Units\Base |
10
|
|
|
* @author ARCANEDEV <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
abstract class UnitMeasure |
13
|
|
|
{ |
14
|
|
|
/* ------------------------------------------------------------------------------------------------ |
15
|
|
|
| Properties |
16
|
|
|
| ------------------------------------------------------------------------------------------------ |
17
|
|
|
*/ |
18
|
|
|
/** |
19
|
|
|
* The unit. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $unit; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The value. |
27
|
|
|
* |
28
|
|
|
* @var float|int |
29
|
|
|
*/ |
30
|
|
|
protected $value; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The symbols. |
34
|
|
|
* |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $symbols = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The number of decimals to format. |
41
|
|
|
* |
42
|
|
|
* @var int |
43
|
|
|
*/ |
44
|
|
|
protected $decimals = 0; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The decimal separator. |
48
|
|
|
* |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
protected $decimalSeparator = '.'; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The thousands separator. |
55
|
|
|
* |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
protected $thousandsSeparator = ','; |
59
|
|
|
|
60
|
|
|
/* ------------------------------------------------------------------------------------------------ |
61
|
|
|
| Getters & Setters |
62
|
|
|
| ------------------------------------------------------------------------------------------------ |
63
|
|
|
*/ |
64
|
|
|
/** |
65
|
|
|
* Get the weight value. |
66
|
|
|
* |
67
|
|
|
* @return float|int |
68
|
|
|
*/ |
69
|
136 |
|
public function value() |
70
|
|
|
{ |
71
|
136 |
|
return $this->value; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Set the weight value. |
76
|
|
|
* |
77
|
|
|
* @param float|int $value |
78
|
|
|
* |
79
|
|
|
* @return static |
80
|
|
|
*/ |
81
|
168 |
|
public function setValue($value) |
82
|
|
|
{ |
83
|
168 |
|
$this->value = $value; |
84
|
|
|
|
85
|
168 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the default units. |
90
|
|
|
* |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
168 |
|
public static function units() |
94
|
|
|
{ |
95
|
168 |
|
$constants = (new ReflectionClass(get_called_class())) |
96
|
168 |
|
->getConstants(); |
97
|
|
|
|
98
|
168 |
|
return array_values($constants); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get the weight unit. |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
80 |
|
public function unit() |
107
|
|
|
{ |
108
|
80 |
|
return $this->unit; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Set the weight unit. |
113
|
|
|
* |
114
|
|
|
* @param string $unit |
115
|
|
|
* |
116
|
|
|
* @return static |
117
|
|
|
*/ |
118
|
168 |
|
public function setUnit($unit) |
119
|
|
|
{ |
120
|
168 |
|
static::checkUnit($unit); |
121
|
|
|
|
122
|
168 |
|
$this->unit = $unit; |
123
|
|
|
|
124
|
168 |
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get the available units. |
129
|
|
|
* |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
32 |
|
public function symbols() |
133
|
|
|
{ |
134
|
32 |
|
return $this->symbols; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get the symbol. |
139
|
|
|
* |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
24 |
|
public function symbol() |
143
|
|
|
{ |
144
|
24 |
|
return Arr::get($this->symbols(), $this->unit); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Set the unit symbol. |
149
|
|
|
* |
150
|
|
|
* @param string $unit |
151
|
|
|
* @param string $symbol |
152
|
|
|
* |
153
|
|
|
* @return static |
154
|
|
|
*/ |
155
|
168 |
|
public function setSymbol($unit, $symbol) |
156
|
|
|
{ |
157
|
168 |
|
static::checkUnit($unit); |
158
|
|
|
|
159
|
168 |
|
$this->symbols[$unit] = $symbol; |
160
|
|
|
|
161
|
168 |
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Get the default symbols. |
166
|
|
|
* |
167
|
|
|
* @return array |
168
|
|
|
*/ |
169
|
168 |
|
protected static function defaultSymbols() |
170
|
|
|
{ |
171
|
168 |
|
return array_combine(static::units(), static::units()); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Set the symbols. |
176
|
|
|
* |
177
|
|
|
* @param array $symbols |
178
|
|
|
* |
179
|
|
|
* @return static |
180
|
|
|
*/ |
181
|
168 |
|
public function setSymbols(array $symbols) |
182
|
|
|
{ |
183
|
168 |
|
if (empty($symbols)) $symbols = static::defaultSymbols(); |
184
|
|
|
|
185
|
168 |
|
foreach ($symbols as $unit => $symbol) { |
186
|
168 |
|
$this->setSymbol($unit, $symbol); |
187
|
126 |
|
} |
188
|
|
|
|
189
|
168 |
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Set the format. |
194
|
|
|
* |
195
|
|
|
* @param int $decimals |
196
|
|
|
* @param string $decimalSeparator |
197
|
|
|
* @param string $thousandsSeparator |
198
|
|
|
* |
199
|
|
|
* @return static |
200
|
|
|
*/ |
201
|
168 |
|
public function setFormat($decimals = 0, $decimalSeparator = ',', $thousandsSeparator = '.') |
202
|
|
|
{ |
203
|
168 |
|
$this->decimals = $decimals; |
204
|
168 |
|
$this->decimalSeparator = $decimalSeparator; |
205
|
168 |
|
$this->thousandsSeparator = $thousandsSeparator; |
206
|
|
|
|
207
|
168 |
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/* ------------------------------------------------------------------------------------------------ |
211
|
|
|
| Main Functions |
212
|
|
|
| ------------------------------------------------------------------------------------------------ |
213
|
|
|
*/ |
214
|
|
|
/** |
215
|
|
|
* Format the weight. |
216
|
|
|
* |
217
|
|
|
* @param int|null $decimals |
218
|
|
|
* @param string|null $decimalSeparator |
219
|
|
|
* @param string|null $thousandsSeparator |
220
|
|
|
* |
221
|
|
|
* @return string |
222
|
|
|
*/ |
223
|
24 |
|
public function format( |
224
|
|
|
$decimals = null, |
225
|
|
|
$decimalSeparator = null, |
226
|
|
|
$thousandsSeparator = null |
227
|
|
|
) { |
228
|
24 |
|
return number_format($this->value(), |
229
|
24 |
|
is_null($decimals) ? $this->decimals : $decimals, |
230
|
24 |
|
is_null($decimalSeparator) ? $this->decimalSeparator : $decimalSeparator, |
231
|
24 |
|
is_null($thousandsSeparator) ? $this->thousandsSeparator : $thousandsSeparator |
232
|
18 |
|
); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Format the weight with symbol. |
237
|
|
|
* |
238
|
|
|
* @param int|null $decimals |
239
|
|
|
* @param string|null $decimalSeparator |
240
|
|
|
* @param string|null $thousandsSeparator |
241
|
|
|
* |
242
|
|
|
* @return string |
243
|
|
|
*/ |
244
|
16 |
|
public function formatWithSymbol( |
245
|
|
|
$decimals = null, |
246
|
|
|
$decimalSeparator = null, |
247
|
|
|
$thousandsSeparator = null |
248
|
|
|
) { |
249
|
16 |
|
return $this->format($decimals, $decimalSeparator, $thousandsSeparator).' '.$this->symbol(); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Convert object to string. |
254
|
|
|
* |
255
|
|
|
* @return string |
256
|
|
|
*/ |
257
|
8 |
|
public function __toString() |
258
|
|
|
{ |
259
|
8 |
|
return $this->formatWithSymbol(); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/* ------------------------------------------------------------------------------------------------ |
263
|
|
|
| Check Functions |
264
|
|
|
| ------------------------------------------------------------------------------------------------ |
265
|
|
|
*/ |
266
|
|
|
/** |
267
|
|
|
* Check the weight unit. |
268
|
|
|
* |
269
|
|
|
* @param string $unit |
270
|
|
|
*/ |
271
|
168 |
|
protected static function checkUnit($unit) |
272
|
|
|
{ |
273
|
168 |
|
if ( ! in_array($unit, static::units())) { |
274
|
8 |
|
throw new InvalidArgumentException( |
275
|
8 |
|
"The weight unit [{$unit}] is invalid." |
276
|
6 |
|
); |
277
|
|
|
} |
278
|
168 |
|
} |
279
|
|
|
} |
280
|
|
|
|