|
1
|
|
|
<?php namespace Arcanedev\Units\Measures; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\Units\Bases\UnitMeasure; |
|
4
|
|
|
use Arcanedev\Units\Contracts\Measures\Weight as WeightContract; |
|
5
|
|
|
use Arcanedev\Units\Traits\Calculatable; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class Weight |
|
9
|
|
|
* |
|
10
|
|
|
* @package Arcanedev\Units |
|
11
|
|
|
* @author ARCANEDEV <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class Weight extends UnitMeasure implements WeightContract |
|
14
|
|
|
{ |
|
15
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
16
|
|
|
| Traits |
|
17
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
18
|
|
|
*/ |
|
19
|
|
|
use Calculatable; |
|
20
|
|
|
|
|
21
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
22
|
|
|
| Constructor |
|
23
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
24
|
|
|
*/ |
|
25
|
|
|
/** |
|
26
|
|
|
* Weight constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param float|int $value |
|
29
|
|
|
* @param string $unit |
|
30
|
|
|
* @param array $options |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct($value = 0, $unit = self::KG, array $options = []) |
|
33
|
176 |
|
{ |
|
34
|
|
|
$this->init($value, $unit, $options); |
|
35
|
176 |
|
} |
|
36
|
176 |
|
|
|
37
|
176 |
|
/* ------------------------------------------------------------------------------------------------ |
|
38
|
176 |
|
| Getters & Setters |
|
39
|
176 |
|
| ------------------------------------------------------------------------------------------------ |
|
40
|
176 |
|
*/ |
|
41
|
176 |
|
/** |
|
42
|
132 |
|
* Get the default names. |
|
43
|
176 |
|
* |
|
44
|
|
|
* @return array |
|
45
|
|
|
*/ |
|
46
|
|
|
public function defaultNames() |
|
47
|
|
|
{ |
|
48
|
|
|
return array_combine(static::units(), [ |
|
49
|
|
|
'ton', |
|
50
|
|
|
'kilogram', |
|
51
|
|
|
'gram', |
|
52
|
|
|
'milligram', |
|
53
|
|
|
]); |
|
54
|
8 |
|
} |
|
55
|
|
|
|
|
56
|
8 |
|
/* ------------------------------------------------------------------------------------------------ |
|
57
|
8 |
|
| Main Functions |
|
58
|
6 |
|
| ------------------------------------------------------------------------------------------------ |
|
59
|
6 |
|
*/ |
|
60
|
6 |
|
/** |
|
61
|
6 |
|
* Make a weight instance. |
|
62
|
|
|
* |
|
63
|
|
|
* @param float|int $value |
|
64
|
|
|
* @param string $unit |
|
65
|
|
|
* @param array $options |
|
66
|
|
|
* |
|
67
|
|
|
* @return self |
|
68
|
|
|
*/ |
|
69
|
|
|
public static function make($value = 0, $unit = self::KG, array $options = []) |
|
70
|
|
|
{ |
|
71
|
8 |
|
return new static($value, $unit, $options); |
|
72
|
|
|
} |
|
73
|
8 |
|
|
|
74
|
|
|
/** |
|
75
|
8 |
|
* Convert the weight to the given unit. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $to |
|
78
|
|
|
* |
|
79
|
|
|
* @return self |
|
80
|
|
|
*/ |
|
81
|
|
|
public function to($to) |
|
82
|
|
|
{ |
|
83
|
|
|
if ($to === $this->unit()) return $this; |
|
84
|
|
|
|
|
85
|
|
|
$value = static::convert($this->unit(), $to, $this->value()); |
|
86
|
|
|
|
|
87
|
|
|
return static::make($value, $to, [ |
|
88
|
|
|
'symbols' => $this->symbols(), |
|
89
|
|
|
'names' => $this->names(), |
|
90
|
|
|
'format' => [ |
|
91
|
64 |
|
'decimals' => $this->decimals, |
|
92
|
|
|
'decimal-separator' => $this->decimalSeparator, |
|
93
|
64 |
|
'thousands-separator' => $this->thousandsSeparator, |
|
94
|
|
|
], |
|
95
|
|
|
]); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Convert the weight. |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $from |
|
102
|
|
|
* @param string $to |
|
103
|
64 |
|
* @param float|int $value |
|
104
|
|
|
* |
|
105
|
64 |
|
* @return float|int |
|
106
|
|
|
*/ |
|
107
|
16 |
|
public static function convert($from, $to, $value) |
|
108
|
|
|
{ |
|
109
|
16 |
|
return $value * static::getRatio($to, $from); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
113
|
|
|
| Calculation Functions |
|
114
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
115
|
|
|
*/ |
|
116
|
|
|
/** |
|
117
|
|
|
* Add the weight. |
|
118
|
|
|
* |
|
119
|
|
|
* @param float|int $value |
|
120
|
|
|
* @param string $unit |
|
121
|
16 |
|
* |
|
122
|
|
|
* @return self |
|
123
|
16 |
|
*/ |
|
124
|
|
|
public function addWeight($value, $unit = self::KG) |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->add(self::make($value, $unit)); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Sub the weight. |
|
131
|
|
|
* |
|
132
|
|
|
* @param float|int $value |
|
133
|
|
|
* @param string $unit |
|
134
|
|
|
* |
|
135
|
|
|
* @return self |
|
136
|
|
|
*/ |
|
137
|
|
|
public function subWeight($value, $unit = self::KG) |
|
138
|
16 |
|
{ |
|
139
|
|
|
return $this->sub(static::make($value, $unit)); |
|
140
|
16 |
|
} |
|
141
|
|
|
|
|
142
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
143
|
|
|
| Other Functions |
|
144
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
145
|
|
|
*/ |
|
146
|
|
|
/** |
|
147
|
|
|
* Get the weight convert ratio. |
|
148
|
|
|
* |
|
149
|
|
|
* @param string $to |
|
150
|
|
|
* @param string $from |
|
151
|
16 |
|
* |
|
152
|
|
|
* @return double|float|integer |
|
153
|
16 |
|
*/ |
|
154
|
|
|
protected static function getRatio($to, $from) |
|
155
|
|
|
{ |
|
156
|
|
|
static::checkUnit($from); |
|
157
|
|
|
static::checkUnit($to); |
|
158
|
|
|
|
|
159
|
|
|
if ($to === $from) return 1; |
|
160
|
|
|
|
|
161
|
|
|
$ratios = static::getRatios(); |
|
162
|
|
|
|
|
163
|
|
|
return $ratios[$to] / $ratios[$from]; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Get all the weight ratios. |
|
168
|
16 |
|
* |
|
169
|
|
|
* @return array |
|
170
|
16 |
|
*/ |
|
171
|
16 |
|
protected static function getRatios() |
|
172
|
|
|
{ |
|
173
|
16 |
|
$rate = 1000; |
|
174
|
|
|
$ratios = [ |
|
175
|
16 |
|
static::TON => 0, |
|
176
|
|
|
static::KG => 1, |
|
177
|
16 |
|
static::G => 2, |
|
178
|
|
|
static::MG => 3, |
|
179
|
|
|
]; |
|
180
|
|
|
|
|
181
|
|
|
return array_map(function ($ratio) use ($rate) { |
|
182
|
|
|
return static::calculate($rate, '^', $ratio); |
|
183
|
|
|
}, $ratios); |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|