|
1
|
|
|
<?php namespace Arcanedev\Units\Measures; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\Units\Bases\UnitMeasure; |
|
4
|
|
|
use Arcanedev\Units\Contracts\Distance as DistanceContract; |
|
5
|
|
|
use Arcanedev\Units\Traits\Calculatable; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Distance |
|
10
|
|
|
* |
|
11
|
|
|
* @package Arcanedev\Units\Measures |
|
12
|
|
|
* @author ARCANEDEV <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class Distance extends UnitMeasure implements DistanceContract |
|
15
|
|
|
{ |
|
16
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
17
|
|
|
| Traits |
|
18
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
19
|
|
|
*/ |
|
20
|
|
|
use Calculatable; |
|
21
|
|
|
|
|
22
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
23
|
|
|
| Constructor |
|
24
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
25
|
|
|
*/ |
|
26
|
|
|
/** |
|
27
|
|
|
* Distance 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::M, array $options = []) |
|
34
|
|
|
{ |
|
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 |
|
'kilometer', |
|
58
|
6 |
|
'hectometre', |
|
59
|
6 |
|
'decametre', |
|
60
|
6 |
|
'metre', |
|
61
|
6 |
|
'decimetre', |
|
62
|
6 |
|
'centimetre', |
|
63
|
6 |
|
'millimetre', |
|
64
|
6 |
|
]); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get the symbol name. |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $unit |
|
71
|
|
|
* |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
8 |
|
public static function getSymbolName($unit) |
|
75
|
|
|
{ |
|
76
|
8 |
|
static::checkUnit($unit); |
|
77
|
|
|
|
|
78
|
8 |
|
return Arr::get(static::names(), $unit); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
82
|
|
|
| Main Functions |
|
83
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
84
|
|
|
*/ |
|
85
|
|
|
/** |
|
86
|
|
|
* Make a distance instance. |
|
87
|
|
|
* |
|
88
|
|
|
* @param float|int $value |
|
89
|
|
|
* @param string $unit |
|
90
|
|
|
* @param array $options |
|
91
|
|
|
* |
|
92
|
|
|
* @return \Arcanedev\Units\Contracts\Distance |
|
93
|
|
|
*/ |
|
94
|
72 |
|
public static function make($value = 0, $unit = self::M, array $options = []) |
|
95
|
|
|
{ |
|
96
|
72 |
|
return new static($value, $unit, $options); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Convert the weight to the given unit. |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $to |
|
103
|
|
|
* |
|
104
|
|
|
* @return \Arcanedev\Units\Contracts\Distance |
|
105
|
|
|
*/ |
|
106
|
72 |
|
public function to($to) |
|
107
|
|
|
{ |
|
108
|
72 |
|
if ($to === $this->unit()) return $this; |
|
109
|
|
|
|
|
110
|
24 |
|
$value = static::convert($this->unit(), $to, $this->value()); |
|
111
|
|
|
|
|
112
|
24 |
|
return static::make($value, $to); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Convert the weight. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $from |
|
119
|
|
|
* @param string $to |
|
120
|
|
|
* @param float|int $value |
|
121
|
|
|
* |
|
122
|
|
|
* @return float|int |
|
123
|
|
|
*/ |
|
124
|
24 |
|
public static function convert($from, $to, $value) |
|
125
|
|
|
{ |
|
126
|
24 |
|
return $value * static::getRatio($to, $from); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
130
|
|
|
| Calculation Functions |
|
131
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
132
|
|
|
*/ |
|
133
|
|
|
/** |
|
134
|
|
|
* Add the distance. |
|
135
|
|
|
* |
|
136
|
|
|
* @param float|int $value |
|
137
|
|
|
* @param string $unit |
|
138
|
|
|
* |
|
139
|
|
|
* @return \Arcanedev\Units\Contracts\Distance |
|
140
|
|
|
*/ |
|
141
|
16 |
|
public function addDistance($value, $unit = self::M) |
|
142
|
|
|
{ |
|
143
|
16 |
|
return $this->add( |
|
144
|
16 |
|
self::make($value, $unit) |
|
145
|
12 |
|
); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Add the distance instance. |
|
150
|
|
|
* |
|
151
|
|
|
* @param \Arcanedev\Units\Contracts\Distance $distance |
|
152
|
|
|
* |
|
153
|
|
|
* @return \Arcanedev\Units\Contracts\Distance |
|
154
|
|
|
*/ |
|
155
|
32 |
|
public function add(DistanceContract $distance) |
|
156
|
|
|
{ |
|
157
|
32 |
|
return $this->setValue( |
|
158
|
32 |
|
static::calculate( |
|
159
|
32 |
|
$this->value(), '+', $distance->to($this->unit())->value() |
|
160
|
24 |
|
) |
|
161
|
24 |
|
); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Sub the weight. |
|
166
|
|
|
* |
|
167
|
|
|
* @param float|int $value |
|
168
|
|
|
* @param string $unit |
|
169
|
|
|
* |
|
170
|
|
|
* @return \Arcanedev\Units\Contracts\Distance |
|
171
|
|
|
*/ |
|
172
|
16 |
|
public function subDistance($value, $unit = self::M) |
|
173
|
|
|
{ |
|
174
|
16 |
|
return $this->sub( |
|
175
|
16 |
|
static::make($value, $unit) |
|
176
|
12 |
|
); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Sub the distance instance. |
|
181
|
|
|
* |
|
182
|
|
|
* @param \Arcanedev\Units\Contracts\Distance $distance |
|
183
|
|
|
* |
|
184
|
|
|
* @return \Arcanedev\Units\Contracts\Distance |
|
185
|
|
|
*/ |
|
186
|
16 |
|
public function sub(DistanceContract $distance) |
|
187
|
|
|
{ |
|
188
|
16 |
|
return $this->setValue( |
|
189
|
16 |
|
static::calculate( |
|
190
|
16 |
|
$this->value(), '-', $distance->to($this->unit())->value() |
|
191
|
12 |
|
) |
|
192
|
12 |
|
); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Multiply distance by the given number. |
|
197
|
|
|
* |
|
198
|
|
|
* @param float|int $number |
|
199
|
|
|
* |
|
200
|
|
|
* @return \Arcanedev\Units\Contracts\Distance |
|
201
|
|
|
*/ |
|
202
|
16 |
|
public function multiply($number) |
|
203
|
|
|
{ |
|
204
|
16 |
|
return $this->setValue( |
|
205
|
16 |
|
static::calculate($this->value(), 'x', $number) |
|
206
|
12 |
|
); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Divide distance by the given number. |
|
211
|
|
|
* |
|
212
|
|
|
* @param float|int $number |
|
213
|
|
|
* |
|
214
|
|
|
* @return \Arcanedev\Units\Contracts\Distance |
|
215
|
|
|
*/ |
|
216
|
16 |
|
public function divide($number) |
|
217
|
|
|
{ |
|
218
|
16 |
|
return $this->setValue( |
|
219
|
16 |
|
static::calculate($this->value(), '/', $number) |
|
220
|
6 |
|
); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
224
|
|
|
| Other Functions |
|
225
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
226
|
|
|
*/ |
|
227
|
|
|
/** |
|
228
|
|
|
* Get the weight convert ratio. |
|
229
|
|
|
* |
|
230
|
|
|
* @param string $to |
|
231
|
|
|
* @param string $from |
|
232
|
|
|
* |
|
233
|
|
|
* @return double|float|integer |
|
234
|
|
|
*/ |
|
235
|
24 |
|
protected static function getRatio($to, $from) |
|
236
|
|
|
{ |
|
237
|
24 |
|
static::checkUnit($from); |
|
238
|
24 |
|
static::checkUnit($to); |
|
239
|
|
|
|
|
240
|
24 |
|
if ($to === $from) return 1; |
|
241
|
|
|
|
|
242
|
24 |
|
$ratios = static::getRatios(); |
|
243
|
|
|
|
|
244
|
24 |
|
return $ratios[$to] / $ratios[$from]; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Get all the weight ratios. |
|
249
|
|
|
* |
|
250
|
|
|
* @return array |
|
251
|
|
|
*/ |
|
252
|
24 |
|
protected static function getRatios() |
|
253
|
|
|
{ |
|
254
|
24 |
|
$rate = 10; |
|
255
|
|
|
$ratios = [ |
|
256
|
24 |
|
static::KM => 0, |
|
257
|
24 |
|
static::HM => 1, |
|
258
|
24 |
|
static::DAM => 2, |
|
259
|
24 |
|
static::M => 3, |
|
260
|
24 |
|
static::DM => 4, |
|
261
|
24 |
|
static::CM => 5, |
|
262
|
24 |
|
static::MM => 6, |
|
263
|
18 |
|
]; |
|
264
|
|
|
|
|
265
|
24 |
|
return array_map(function ($ratio) use ($rate) { |
|
266
|
24 |
|
return static::calculate($rate, '^', $ratio); |
|
267
|
24 |
|
}, $ratios); |
|
268
|
|
|
} |
|
269
|
|
|
} |
|
270
|
|
|
|