|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Units plugin for Craft CMS |
|
4
|
|
|
* |
|
5
|
|
|
* A plugin for handling physical quantities and the units of measure in which |
|
6
|
|
|
* they're represented. |
|
7
|
|
|
* |
|
8
|
|
|
* @link https://nystudio107.com/ |
|
|
|
|
|
|
9
|
|
|
* @copyright Copyright (c) nystudio107 |
|
|
|
|
|
|
10
|
|
|
*/ |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
namespace nystudio107\units\models; |
|
13
|
|
|
|
|
14
|
|
|
use craft\base\Model; |
|
15
|
|
|
use nystudio107\units\Units; |
|
16
|
|
|
use PhpUnitsOfMeasure\AbstractPhysicalQuantity; |
|
17
|
|
|
use PhpUnitsOfMeasure\Exception\NonNumericValue; |
|
18
|
|
|
use PhpUnitsOfMeasure\Exception\NonStringUnitName; |
|
19
|
|
|
use PhpUnitsOfMeasure\PhysicalQuantityInterface; |
|
20
|
|
|
use PhpUnitsOfMeasure\UnitOfMeasure; |
|
21
|
|
|
use PhpUnitsOfMeasure\UnitOfMeasureInterface; |
|
22
|
|
|
use yii\base\InvalidArgumentException; |
|
23
|
|
|
use function call_user_func_array; |
|
24
|
|
|
use function get_class; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
|
|
|
|
|
27
|
|
|
* @author nystudio107 |
|
|
|
|
|
|
28
|
|
|
* @package Units |
|
|
|
|
|
|
29
|
|
|
* @since 1.0.0 |
|
|
|
|
|
|
30
|
|
|
* |
|
31
|
|
|
* @property string $valueFraction |
|
32
|
|
|
* @property array|float[] $valueParts |
|
33
|
|
|
* @property array|string[] $valuePartsFraction |
|
34
|
|
|
*/ |
|
|
|
|
|
|
35
|
|
|
class UnitsData extends Model implements PhysicalQuantityInterface |
|
36
|
|
|
{ |
|
37
|
|
|
// Public Properties |
|
38
|
|
|
// ========================================================================= |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
|
|
|
|
|
41
|
|
|
* @var ?string The fully qualified class name of the unit of measure |
|
42
|
|
|
*/ |
|
43
|
|
|
public ?string $unitsClass = null; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
|
|
|
|
|
46
|
|
|
* @var ?float The value of the unit of measure |
|
47
|
|
|
*/ |
|
48
|
|
|
public ?float $value = null; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
|
|
|
|
|
51
|
|
|
* @var ?string The units that the unit of measure is in |
|
52
|
|
|
*/ |
|
53
|
|
|
public ?string $units = null; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
|
|
|
|
|
56
|
|
|
* @var AbstractPhysicalQuantity |
|
57
|
|
|
*/ |
|
58
|
|
|
public AbstractPhysicalQuantity $unitsInstance; |
|
59
|
|
|
|
|
60
|
|
|
// Public Methods |
|
61
|
|
|
// ========================================================================= |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Call through to the appropriate method in the AbstractPhysicalQuantity |
|
65
|
|
|
* class in $unitsInstance, if it exists |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $method |
|
|
|
|
|
|
68
|
|
|
* @param array $args |
|
|
|
|
|
|
69
|
|
|
* |
|
70
|
|
|
* @return mixed |
|
71
|
|
|
* @throws InvalidArgumentException |
|
72
|
|
|
* @throws NonNumericValue |
|
73
|
|
|
* @throws NonStringUnitName |
|
74
|
|
|
*/ |
|
75
|
|
|
public function __call($method, $args) |
|
76
|
|
|
{ |
|
77
|
|
|
$unitsInstance = $this->unitsInstance; |
|
78
|
|
|
if (method_exists($unitsInstance, $method)) { |
|
79
|
|
|
return call_user_func_array([$unitsInstance, $method], $args); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
throw new InvalidArgumentException("Method {$method} doesn't exist"); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
|
|
|
|
|
86
|
|
|
* @inheritdoc |
|
87
|
|
|
*/ |
|
|
|
|
|
|
88
|
|
|
public function init(): void |
|
89
|
|
|
{ |
|
90
|
|
|
parent::init(); |
|
91
|
|
|
/** @var Settings $settings */ |
|
|
|
|
|
|
92
|
|
|
$settings = Units::$plugin->getSettings(); |
|
93
|
|
|
$this->unitsClass = $this->unitsClass ?? $settings->defaultUnitsClass; |
|
94
|
|
|
$this->value = $this->value ?? $settings->defaultValue; |
|
95
|
|
|
$this->units = $this->units ?? $settings->defaultUnits; |
|
96
|
|
|
|
|
97
|
|
|
if ($this->unitsClass !== null) { |
|
98
|
|
|
$this->unitsInstance = new $this->unitsClass($this->value, $this->units); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
|
|
|
|
|
103
|
|
|
* @inheritdoc |
|
104
|
|
|
*/ |
|
|
|
|
|
|
105
|
|
|
public function rules(): array |
|
106
|
|
|
{ |
|
107
|
|
|
$rules = parent::rules(); |
|
108
|
|
|
$rules = array_merge($rules, [ |
|
|
|
|
|
|
109
|
|
|
['unitsClass', 'string'], |
|
110
|
|
|
['value', 'number'], |
|
111
|
|
|
['units', 'string'], |
|
112
|
|
|
]); |
|
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
return $rules; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
|
|
|
|
|
118
|
|
|
* @inheritdoc |
|
119
|
|
|
*/ |
|
|
|
|
|
|
120
|
|
|
public function fields(): array |
|
121
|
|
|
{ |
|
122
|
|
|
$fields = parent::fields(); |
|
123
|
|
|
$fields = array_diff_key( |
|
124
|
|
|
$fields, |
|
125
|
|
|
array_flip([ |
|
|
|
|
|
|
126
|
|
|
'unitsInstance', |
|
127
|
|
|
]) |
|
|
|
|
|
|
128
|
|
|
); |
|
129
|
|
|
|
|
130
|
|
|
return $fields; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
|
|
|
|
|
134
|
|
|
* @inheritdoc |
|
135
|
|
|
*/ |
|
|
|
|
|
|
136
|
|
|
public function toUnit($unit) |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->unitsInstance->toUnit($unit); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
|
|
|
|
|
142
|
|
|
* @inheritdoc |
|
143
|
|
|
*/ |
|
|
|
|
|
|
144
|
|
|
public function toNativeUnit() |
|
145
|
|
|
{ |
|
146
|
|
|
return $this->unitsInstance->toNativeUnit(); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
|
|
|
|
|
150
|
|
|
* @inheritdoc |
|
151
|
|
|
*/ |
|
|
|
|
|
|
152
|
|
|
public function __toString(): string |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->unitsInstance->__toString(); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
|
|
|
|
|
158
|
|
|
* @inheritdoc |
|
159
|
|
|
*/ |
|
|
|
|
|
|
160
|
|
|
public function add(PhysicalQuantityInterface $quantity) |
|
161
|
|
|
{ |
|
162
|
|
|
/** @var UnitsData $quantity */ |
|
|
|
|
|
|
163
|
|
|
return $this->physicalQuantityToUnitsData($this->unitsInstance->add($quantity->unitsInstance)); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
|
|
|
|
|
167
|
|
|
* @inheritdoc |
|
168
|
|
|
*/ |
|
|
|
|
|
|
169
|
|
|
public function subtract(PhysicalQuantityInterface $quantity) |
|
170
|
|
|
{ |
|
171
|
|
|
/** @var UnitsData $quantity */ |
|
|
|
|
|
|
172
|
|
|
return $this->physicalQuantityToUnitsData($this->unitsInstance->subtract($quantity->unitsInstance)); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
|
|
|
|
|
176
|
|
|
* @inheritdoc |
|
177
|
|
|
*/ |
|
|
|
|
|
|
178
|
|
|
public function isEquivalentQuantity(PhysicalQuantityInterface $testQuantity) |
|
179
|
|
|
{ |
|
180
|
|
|
/** @var UnitsData $testQuantity */ |
|
|
|
|
|
|
181
|
|
|
return $this->unitsInstance->isEquivalentQuantity($testQuantity->unitsInstance); |
|
|
|
|
|
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
|
|
|
|
|
185
|
|
|
* @inheritdoc |
|
186
|
|
|
*/ |
|
|
|
|
|
|
187
|
|
|
public function availableUnits(bool $includeAliases = true) |
|
188
|
|
|
{ |
|
189
|
|
|
$availableUnits = []; |
|
190
|
|
|
$units = $this->unitsInstance::getUnitDefinitions(); |
|
191
|
|
|
/** @var UnitOfMeasure $unit */ |
|
|
|
|
|
|
192
|
|
|
foreach ($units as $unit) { |
|
193
|
|
|
$name = $unit->getName(); |
|
194
|
|
|
$aliases = $unit->getAliases(); |
|
195
|
|
|
$availableUnits[$name] = $includeAliases ? $aliases : $aliases[0] ?? $name; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
return $availableUnits; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Return the measurement as a fraction, with the units appended |
|
203
|
|
|
* |
|
204
|
|
|
* @return string |
|
205
|
|
|
*/ |
|
206
|
|
|
public function toFraction(): string |
|
207
|
|
|
{ |
|
208
|
|
|
return trim(Units::$variable->fraction($this->value) . ' ' . $this->units); |
|
|
|
|
|
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Return the measurement as a fraction, in the given unit of measure |
|
213
|
|
|
* |
|
214
|
|
|
* @param UnitOfMeasureInterface|string $unit The desired unit of measure, |
|
215
|
|
|
* or a string name of one |
|
|
|
|
|
|
216
|
|
|
* |
|
217
|
|
|
* @return string The measurement cast in the requested units, as a |
|
218
|
|
|
* fraction |
|
219
|
|
|
*/ |
|
220
|
|
|
public function toUnitFraction($unit): string |
|
221
|
|
|
{ |
|
222
|
|
|
$value = $this->toUnit($unit); |
|
223
|
|
|
|
|
224
|
|
|
return Units::$variable->fraction($value); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Return the value as a fraction |
|
229
|
|
|
* |
|
230
|
|
|
* @return string |
|
231
|
|
|
*/ |
|
232
|
|
|
public function getValueFraction(): string |
|
233
|
|
|
{ |
|
234
|
|
|
return Units::$variable->fraction($this->value); |
|
|
|
|
|
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Return an array of the whole number and decimal number ports of the value |
|
239
|
|
|
* [0] has the whole number part, and [1] has the decimal part |
|
240
|
|
|
* |
|
241
|
|
|
* @return float[] |
|
242
|
|
|
*/ |
|
243
|
|
|
public function getValueParts(): array |
|
244
|
|
|
{ |
|
245
|
|
|
return Units::$variable->float2parts($this->value); |
|
|
|
|
|
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* Return an array of the whole number and decimal number ports of the |
|
250
|
|
|
* value with the decimal part converted to a fraction. [0] has the whole |
|
251
|
|
|
* number part, and [1] has the fractional part |
|
252
|
|
|
* |
|
253
|
|
|
* @return string[] |
|
254
|
|
|
*/ |
|
255
|
|
|
public function getValuePartsFraction(): array |
|
256
|
|
|
{ |
|
257
|
|
|
$parts = Units::$variable->float2parts($this->value); |
|
|
|
|
|
|
258
|
|
|
$parts[0] = (string)$parts[0]; |
|
259
|
|
|
$parts[1] = Units::$variable->float2ratio($parts[1]); |
|
260
|
|
|
|
|
261
|
|
|
return $parts; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
// Protected Methods |
|
265
|
|
|
// ========================================================================= |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* Convert a PhysicalQuantity object into a UnitsData object |
|
269
|
|
|
* |
|
270
|
|
|
* @param PhysicalQuantityInterface $quantity |
|
|
|
|
|
|
271
|
|
|
* |
|
272
|
|
|
* @return UnitsData |
|
273
|
|
|
*/ |
|
274
|
|
|
protected function physicalQuantityToUnitsData(PhysicalQuantityInterface $quantity): UnitsData |
|
275
|
|
|
{ |
|
276
|
|
|
$unitsClass = get_class($quantity); |
|
277
|
|
|
list($value, $units) = explode(' ', (string)$quantity); |
|
278
|
|
|
$config = [ |
|
279
|
|
|
'unitsClass' => $unitsClass, |
|
280
|
|
|
'value' => $value, |
|
281
|
|
|
'units' => $units, |
|
282
|
|
|
]; |
|
283
|
|
|
|
|
284
|
|
|
return new UnitsData($config); |
|
285
|
|
|
} |
|
286
|
|
|
} |
|
287
|
|
|
|