1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MaksimM\MultiUnitModels\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use MaksimM\MultiUnitModels\Exceptions\NotSupportedMultiUnitField; |
8
|
|
|
use MaksimM\MultiUnitModels\Exceptions\NotSupportedMultiUnitFieldUnit; |
9
|
|
|
use UnitConverter\Unit\AbstractUnit; |
10
|
|
|
|
11
|
|
|
trait MultiUnitSupport |
12
|
|
|
{ |
13
|
|
|
use ModelConfiguration; |
14
|
|
|
|
15
|
|
|
protected $unitConversionDataPostfix = '_ucd'; |
16
|
|
|
protected $multiUnitColumns = []; |
17
|
|
|
protected $multiUnitSelectedUnits = []; |
18
|
|
|
|
19
|
|
|
private function getUnitConversionDataColumns() |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
return array_map(function ($column) { |
22
|
|
|
return $column.$this->getUnitConversionDataPostfix(); |
23
|
|
|
}, array_keys($this->getMultiUnitColumns())); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
protected static function bootMultiUnitSupport() |
27
|
|
|
{ |
28
|
|
|
//save conversion table if base value is changed |
29
|
|
|
static::creating(function ($model) { |
30
|
|
|
/** |
31
|
|
|
* @var Model|MultiUnitSupport $model |
32
|
|
|
*/ |
33
|
|
|
foreach ($model->getMultiUnitColumns() as $unitBasedColumn => $options) { |
|
|
|
|
34
|
|
|
if (isset($model->attributes[$unitBasedColumn])) { |
35
|
|
|
$model->{$unitBasedColumn.$model->getUnitConversionDataPostfix()} = json_encode( |
|
|
|
|
36
|
|
|
$model->calculateMultiUnitConversionData( |
|
|
|
|
37
|
|
|
$model->attributes[$unitBasedColumn], |
38
|
|
|
$model->getMultiUnitFieldUnit($unitBasedColumn), |
|
|
|
|
39
|
|
|
$options['supported_units'] |
40
|
|
|
) |
41
|
|
|
); |
42
|
|
|
$model->{$unitBasedColumn} = $model->processMultiUnitFieldChanges( |
|
|
|
|
43
|
|
|
$unitBasedColumn, |
44
|
|
|
$model->{$unitBasedColumn} |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
}); |
49
|
|
|
static::updating(function ($model) { |
50
|
|
|
/** |
51
|
|
|
* @var Model|MultiUnitSupport $model |
52
|
|
|
*/ |
53
|
|
|
foreach (Arr::only($model->getMultiUnitColumns(), array_keys($model->getDirty())) as $unitBasedColumn => $options) { |
|
|
|
|
54
|
|
|
$model->{$unitBasedColumn.$model->getUnitConversionDataPostfix()} = json_encode( |
|
|
|
|
55
|
|
|
$model->calculateMultiUnitConversionData( |
|
|
|
|
56
|
|
|
$model->getDirty()[$unitBasedColumn], |
|
|
|
|
57
|
|
|
$model->getMultiUnitFieldUnit($unitBasedColumn, true), |
|
|
|
|
58
|
|
|
$options['supported_units'] |
59
|
|
|
) |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
}); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param $value |
67
|
|
|
* @param AbstractUnit $unit |
68
|
|
|
* @param string[] $requiredUnits |
69
|
|
|
* |
70
|
|
|
* @return array|null |
71
|
|
|
*/ |
72
|
|
|
private function calculateMultiUnitConversionData($value, AbstractUnit $unit, $requiredUnits) |
73
|
|
|
{ |
74
|
|
|
if (is_null($value)) { |
75
|
|
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$conversionData = []; |
79
|
|
|
foreach ($requiredUnits as $requiredUnitClass) { |
80
|
|
|
/** |
81
|
|
|
* @var AbstractUnit $requiredUnit |
82
|
|
|
*/ |
83
|
|
|
$requiredUnit = new $requiredUnitClass(); |
84
|
|
|
$conversionData[$requiredUnit->getId()] = (new $unit($value))->as($requiredUnit); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $conversionData; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getMultiUnitExistingConversionData($field) |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
return json_decode($this->{$field.$this->getUnitConversionDataPostfix()} ?? null); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
protected function getUnitConversionDataPostfix() |
99
|
|
|
{ |
100
|
|
|
return $this->unitConversionDataPostfix; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
public function getMultiUnitColumns() |
107
|
|
|
{ |
108
|
|
|
return $this->multiUnitColumns; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param $field |
113
|
|
|
* |
114
|
|
|
* @throws NotSupportedMultiUnitField |
115
|
|
|
* |
116
|
|
|
* @return AbstractUnit[] |
117
|
|
|
*/ |
118
|
|
|
public function getMultiUnitFieldSupportedUnits($field) |
119
|
|
|
{ |
120
|
|
|
if ($this->isMultiUnitField($field)) { |
121
|
|
|
return $this->getMultiUnitColumns()[$field]['supported_units']; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
throw new NotSupportedMultiUnitField($field); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param $field |
129
|
|
|
* |
130
|
|
|
* @throws NotSupportedMultiUnitField |
131
|
|
|
* |
132
|
|
|
* @return AbstractUnit |
133
|
|
|
*/ |
134
|
|
|
public function getMultiUnitFieldDefaultUnit($field) |
135
|
|
|
{ |
136
|
|
|
if ($this->isMultiUnitField($field)) { |
137
|
|
|
$unitClass = $this->getMultiUnitColumns()[$field]['default_unit']; |
138
|
|
|
|
139
|
|
|
return new $unitClass(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
throw new NotSupportedMultiUnitField($field); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param $field |
147
|
|
|
* |
148
|
|
|
* @throws NotSupportedMultiUnitField |
149
|
|
|
* |
150
|
|
|
* @return AbstractUnit |
151
|
|
|
*/ |
152
|
|
|
public function getMultiUnitFieldSelectedUnit($field) |
153
|
|
|
{ |
154
|
|
|
if ($this->isMultiUnitField($field)) { |
155
|
|
|
$unitClass = $this->multiUnitSelectedUnits[$field] ?? $this->getMultiUnitFieldDefaultUnit($field); |
156
|
|
|
|
157
|
|
|
return new $unitClass(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
throw new NotSupportedMultiUnitField($field); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param $field |
165
|
|
|
* @param string $unit |
166
|
|
|
* |
167
|
|
|
* @throws NotSupportedMultiUnitField |
168
|
|
|
* @throws NotSupportedMultiUnitFieldUnit |
169
|
|
|
*/ |
170
|
|
|
public function setMultiUnitFieldSelectedUnit($field, $unit) |
171
|
|
|
{ |
172
|
|
|
if ($this->isMultiUnitField($field)) { |
173
|
|
|
$found = false; |
174
|
|
View Code Duplication |
foreach ($this->getMultiUnitFieldSupportedUnits($field) as $unitClass) { |
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @var AbstractUnit $unit |
177
|
|
|
*/ |
178
|
|
|
$supportedUnit = new $unitClass(); |
179
|
|
|
if (strtolower($supportedUnit->getId()) == strtolower($unit)) { |
180
|
|
|
$found = true; |
181
|
|
|
break; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
if ($found) { |
185
|
|
|
$this->multiUnitSelectedUnits[$field] = $unitClass; |
|
|
|
|
186
|
|
|
} else { |
187
|
|
|
throw new NotSupportedMultiUnitFieldUnit($field, $unit); |
188
|
|
|
} |
189
|
|
|
} else { |
190
|
|
|
throw new NotSupportedMultiUnitField($field); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param $field |
196
|
|
|
* @param string $unit |
|
|
|
|
197
|
|
|
* |
198
|
|
|
* @throws NotSupportedMultiUnitField |
199
|
|
|
* |
200
|
|
|
* @return mixed |
201
|
|
|
*/ |
202
|
|
|
public function getMultiUnitFieldValueByUnitName($field, $unit = null) |
203
|
|
|
{ |
204
|
|
|
if ($this->isMultiUnitField($field)) { |
205
|
|
|
if (isset($this->{$field})) { |
206
|
|
|
if (is_null($unit)) { |
207
|
|
|
$unit = $this->getMultiUnitFieldUnit($field); |
208
|
|
|
} else { |
209
|
|
View Code Duplication |
foreach ($this->getMultiUnitFieldSupportedUnits($field) as $unitClass) { |
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @var AbstractUnit $unit |
212
|
|
|
*/ |
213
|
|
|
$supportedUnit = new $unitClass(); |
214
|
|
|
if (strtolower($supportedUnit->getId()) == strtolower($unit)) { |
215
|
|
|
$unit = $supportedUnit; |
216
|
|
|
break; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
if (is_string($unit)) { |
221
|
|
|
throw new NotSupportedMultiUnitField($field); |
222
|
|
|
} |
223
|
|
|
$existingConversionData = $this->getMultiUnitExistingConversionData($field); |
224
|
|
View Code Duplication |
if (!is_null($existingConversionData) && !is_null($existingConversionData->{$unit->getId()})) { |
|
|
|
|
225
|
|
|
return $existingConversionData->{$unit->getId()}; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
return ($this->getMultiUnitFieldSelectedUnit($field)->setValue($this->{$field} ?? $this->attributes[$field]))->as(new $unit()); |
|
|
|
|
229
|
|
|
} else { |
230
|
|
|
return; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
throw new NotSupportedMultiUnitField($field); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @param $field |
239
|
|
|
* @param AbstractUnit|null $unit |
240
|
|
|
* |
241
|
|
|
* @throws NotSupportedMultiUnitField |
242
|
|
|
* |
243
|
|
|
* @return mixed |
244
|
|
|
*/ |
245
|
|
|
public function getMultiUnitFieldValue($field, AbstractUnit $unit = null) |
246
|
|
|
{ |
247
|
|
|
if ($this->isMultiUnitField($field)) { |
248
|
|
|
if (isset($this->{$field})) { |
249
|
|
|
if (is_null($unit)) { |
250
|
|
|
$unit = $this->getMultiUnitFieldUnit($field); |
251
|
|
|
} |
252
|
|
|
$existingConversionData = $this->getMultiUnitExistingConversionData($field); |
253
|
|
View Code Duplication |
if (!is_null($existingConversionData) && !is_null($existingConversionData->{$unit->getId()})) { |
|
|
|
|
254
|
|
|
return $existingConversionData->{$unit->getId()}; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return ($this->getMultiUnitFieldSelectedUnit($field)->setValue($this->{$field} ?? $this->attributes[$field]))->as(new $unit()); |
258
|
|
|
} else { |
259
|
|
|
return; |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
throw new NotSupportedMultiUnitField($field); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
protected function isMultiUnitField($field) |
267
|
|
|
{ |
268
|
|
|
return isset($this->getMultiUnitColumns()[$field]); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @param $field |
273
|
|
|
* |
274
|
|
|
* @throws NotSupportedMultiUnitField |
275
|
|
|
* |
276
|
|
|
* @return AbstractUnit |
277
|
|
|
*/ |
278
|
|
|
protected function getMultiUnitFieldUnit($field, $preferDefault = false) |
279
|
|
|
{ |
280
|
|
|
return $preferDefault ? $this->getMultiUnitFieldDefaultUnit($field) : $this->getMultiUnitFieldSelectedUnit($field); |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|