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