1
|
|
|
<?php namespace Arcanedev\Units\Measures; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Units\Bases\UnitMeasure; |
4
|
|
|
use Arcanedev\Units\Contracts\Weight as WeightContract; |
5
|
|
|
use Arcanedev\Units\Traits\Calculatable; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Weight |
10
|
|
|
* |
11
|
|
|
* @package Arcanedev\Units |
12
|
|
|
* @author ARCANEDEV <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class Weight extends UnitMeasure implements WeightContract |
15
|
|
|
{ |
16
|
|
|
/* ------------------------------------------------------------------------------------------------ |
17
|
|
|
| Traits |
18
|
|
|
| ------------------------------------------------------------------------------------------------ |
19
|
|
|
*/ |
20
|
|
|
use Calculatable; |
21
|
|
|
|
22
|
|
|
/* ------------------------------------------------------------------------------------------------ |
23
|
|
|
| Constructor |
24
|
|
|
| ------------------------------------------------------------------------------------------------ |
25
|
|
|
*/ |
26
|
|
|
/** |
27
|
|
|
* Weight constructor. |
28
|
|
|
* |
29
|
|
|
* @param float|int $value |
30
|
|
|
* @param string $unit |
31
|
|
|
* @param array $options |
32
|
|
|
*/ |
33
|
168 |
|
public function __construct($value = 0, $unit = self::KG, array $options = []) |
34
|
6 |
|
{ |
35
|
168 |
|
$this->setValue($value); |
36
|
168 |
|
$this->setUnit($unit); |
37
|
168 |
|
$this->setSymbols(Arr::get($options, 'symbols', [])); |
38
|
168 |
|
$this->setFormat( |
39
|
168 |
|
Arr::get($options, 'decimals', 0), |
40
|
168 |
|
Arr::get($options, 'separators.decimal', ','), |
41
|
168 |
|
Arr::get($options, 'separators.thousands', '.') |
42
|
126 |
|
); |
43
|
168 |
|
} |
44
|
|
|
|
45
|
|
|
/* ------------------------------------------------------------------------------------------------ |
46
|
|
|
| Getters & Setters |
47
|
|
|
| ------------------------------------------------------------------------------------------------ |
48
|
|
|
*/ |
49
|
|
|
/** |
50
|
|
|
* Get the symbol's names. |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
8 |
|
public static function names() |
55
|
|
|
{ |
56
|
8 |
|
return array_combine(static::units(), [ |
57
|
8 |
|
'ton', |
58
|
6 |
|
'kilogram', |
59
|
6 |
|
'gram', |
60
|
6 |
|
'milligram', |
61
|
6 |
|
]); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get the symbol name. |
66
|
|
|
* |
67
|
|
|
* @param string $unit |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
8 |
|
public static function getSymbolName($unit) |
72
|
|
|
{ |
73
|
8 |
|
static::checkUnit($unit); |
74
|
|
|
|
75
|
8 |
|
return Arr::get(static::names(), $unit); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/* ------------------------------------------------------------------------------------------------ |
79
|
|
|
| Main Functions |
80
|
|
|
| ------------------------------------------------------------------------------------------------ |
81
|
|
|
*/ |
82
|
|
|
/** |
83
|
|
|
* Make a weight instance. |
84
|
|
|
* |
85
|
|
|
* @param float|int $value |
86
|
|
|
* @param string $unit |
87
|
|
|
* @param array $options |
88
|
|
|
* |
89
|
|
|
* @return \Arcanedev\Units\Contracts\Weight |
90
|
|
|
*/ |
91
|
64 |
|
public static function make($value = 0, $unit = self::KG, array $options = []) |
92
|
|
|
{ |
93
|
64 |
|
return new static($value, $unit, $options); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Convert the weight to the given unit. |
98
|
|
|
* |
99
|
|
|
* @param string $to |
100
|
|
|
* |
101
|
|
|
* @return \Arcanedev\Units\Contracts\Weight |
102
|
|
|
*/ |
103
|
64 |
|
public function to($to) |
104
|
|
|
{ |
105
|
64 |
|
if ($to === $this->unit()) return $this; |
106
|
|
|
|
107
|
16 |
|
$value = static::convert($this->unit(), $to, $this->value()); |
108
|
|
|
|
109
|
16 |
|
return static::make($value, $to); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Convert the weight. |
114
|
|
|
* |
115
|
|
|
* @param string $from |
116
|
|
|
* @param string $to |
117
|
|
|
* @param float|int $value |
118
|
|
|
* |
119
|
|
|
* @return float|int |
120
|
|
|
*/ |
121
|
16 |
|
public static function convert($from, $to, $value) |
122
|
|
|
{ |
123
|
16 |
|
return $value * static::getRatio($to, $from); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/* ------------------------------------------------------------------------------------------------ |
127
|
|
|
| Calculation Functions |
128
|
|
|
| ------------------------------------------------------------------------------------------------ |
129
|
|
|
*/ |
130
|
|
|
/** |
131
|
|
|
* Add the weight. |
132
|
|
|
* |
133
|
|
|
* @param double|float|integer $value |
134
|
|
|
* @param string $unit |
135
|
|
|
* |
136
|
|
|
* @return \Arcanedev\Units\Contracts\Weight |
137
|
|
|
*/ |
138
|
16 |
|
public function addWeight($value, $unit = self::KG) |
139
|
|
|
{ |
140
|
16 |
|
return $this->add( |
141
|
16 |
|
self::make($value, $unit) |
142
|
12 |
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Add the weight instance. |
147
|
|
|
* |
148
|
|
|
* @param \Arcanedev\Units\Contracts\Weight $weight |
149
|
|
|
* |
150
|
|
|
* @return \Arcanedev\Units\Contracts\Weight |
151
|
|
|
*/ |
152
|
32 |
|
public function add(WeightContract $weight) |
153
|
|
|
{ |
154
|
32 |
|
return $this->setValue( |
155
|
32 |
|
static::calculate( |
156
|
32 |
|
$this->value(), '+', $weight->to($this->unit())->value() |
157
|
24 |
|
) |
158
|
24 |
|
); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Sub the weight. |
163
|
|
|
* |
164
|
|
|
* @param float|int $value |
165
|
|
|
* @param string $unit |
166
|
|
|
* |
167
|
|
|
* @return \Arcanedev\Units\Contracts\Weight |
168
|
|
|
*/ |
169
|
16 |
|
public function subWeight($value, $unit = self::KG) |
170
|
|
|
{ |
171
|
16 |
|
return $this->sub( |
172
|
16 |
|
static::make($value, $unit) |
173
|
12 |
|
); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Sub the weight instance. |
178
|
|
|
* |
179
|
|
|
* @param \Arcanedev\Units\Contracts\Weight $weight |
180
|
|
|
* |
181
|
|
|
* @return \Arcanedev\Units\Contracts\Weight |
182
|
|
|
*/ |
183
|
16 |
|
public function sub(WeightContract $weight) |
184
|
|
|
{ |
185
|
16 |
|
return $this->setValue( |
186
|
16 |
|
static::calculate( |
187
|
16 |
|
$this->value(), '-', $weight->to($this->unit())->value() |
188
|
12 |
|
) |
189
|
12 |
|
); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Multiply weight by the given number. |
194
|
|
|
* |
195
|
|
|
* @param float|int $number |
196
|
|
|
* |
197
|
|
|
* @return \Arcanedev\Units\Contracts\Weight |
198
|
|
|
*/ |
199
|
16 |
|
public function multiply($number) |
200
|
|
|
{ |
201
|
16 |
|
return $this->setValue( |
202
|
16 |
|
static::calculate($this->value(), 'x', $number) |
203
|
12 |
|
); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Divide weight by the given number. |
208
|
|
|
* |
209
|
|
|
* @param float|int $number |
210
|
|
|
* |
211
|
|
|
* @return \Arcanedev\Units\Contracts\Weight |
212
|
|
|
*/ |
213
|
16 |
|
public function divide($number) |
214
|
|
|
{ |
215
|
16 |
|
return $this->setValue( |
216
|
16 |
|
static::calculate($this->value(), '/', $number) |
217
|
6 |
|
); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/* ------------------------------------------------------------------------------------------------ |
221
|
|
|
| Other Functions |
222
|
|
|
| ------------------------------------------------------------------------------------------------ |
223
|
|
|
*/ |
224
|
|
|
/** |
225
|
|
|
* Get the weight convert ratio. |
226
|
|
|
* |
227
|
|
|
* @param string $to |
228
|
|
|
* @param string $from |
229
|
|
|
* |
230
|
|
|
* @return double|float|integer |
231
|
|
|
*/ |
232
|
16 |
|
protected static function getRatio($to, $from) |
233
|
|
|
{ |
234
|
16 |
|
static::checkUnit($from); |
235
|
16 |
|
static::checkUnit($to); |
236
|
|
|
|
237
|
16 |
|
if ($to === $from) return 1; |
238
|
|
|
|
239
|
16 |
|
$ratios = static::getRatios(); |
240
|
|
|
|
241
|
16 |
|
return $ratios[$to] / $ratios[$from]; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Get all the weight ratios. |
246
|
|
|
* |
247
|
|
|
* @return array |
248
|
|
|
*/ |
249
|
16 |
|
protected static function getRatios() |
250
|
|
|
{ |
251
|
|
|
$ratios = [ |
252
|
16 |
|
static::TON => 0, |
253
|
16 |
|
static::KG => 1, |
254
|
16 |
|
static::G => 2, |
255
|
16 |
|
static::MG => 3, |
256
|
12 |
|
]; |
257
|
|
|
|
258
|
16 |
|
return array_map(function ($ratio) { |
259
|
16 |
|
return static::calculate(1000, '^', $ratio); |
260
|
16 |
|
}, $ratios); |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|