|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace MaksimM\MultiUnitModels\Traits; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use MaksimM\MultiUnitModels\Exceptions\NotSupportedMultiUnitField; |
|
8
|
|
|
|
|
9
|
|
|
trait ModelConfiguration |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @return array |
|
14
|
|
|
*/ |
|
15
|
|
|
public function getFillable() |
|
16
|
|
|
{ |
|
17
|
|
|
return array_merge($this->getUnitConversionDataColumns(), parent::getFillable()); |
|
|
|
|
|
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @return mixed |
|
22
|
|
|
*/ |
|
23
|
|
|
public function getHidden() |
|
24
|
|
|
{ |
|
25
|
|
|
return array_merge(parent::getHidden(), $this->getUnitConversionDataColumns()); |
|
|
|
|
|
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function scopeSelectedUnits($query, $units) |
|
29
|
|
|
{ |
|
30
|
|
|
foreach ($units as $unitBasedColumn => $unit) { |
|
31
|
|
|
$query->getModel()->setMultiUnitFieldSelectedUnit($unitBasedColumn, $unit); |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Create a new instance of the given model. |
|
37
|
|
|
* |
|
38
|
|
|
* @param array $attributes |
|
39
|
|
|
* @param bool $exists |
|
40
|
|
|
* @return static |
|
41
|
|
|
* @throws NotSupportedMultiUnitField |
|
42
|
|
|
*/ |
|
43
|
|
|
public function newInstance($attributes = [], $exists = false) |
|
44
|
|
|
{ |
|
45
|
|
|
$model = parent::newInstance($attributes, $exists); |
|
46
|
|
|
foreach ($this->getMultiUnitColumns() as $unitBasedColumn => $options) { |
|
|
|
|
|
|
47
|
|
|
$model->setMultiUnitFieldSelectedUnit($unitBasedColumn, $this->getMultiUnitFieldSelectedUnit($unitBasedColumn)->getId()); |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
return $model; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Determine if a set mutator exists for an attribute. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $key |
|
56
|
|
|
* |
|
57
|
|
|
* @return bool |
|
58
|
|
|
*/ |
|
59
|
|
|
public function hasSetMutator($key) |
|
60
|
|
|
{ |
|
61
|
|
|
if ($this->isMultiUnitField($key)) { |
|
|
|
|
|
|
62
|
|
|
return true; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return parent::hasSetMutator($key); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Determine if a get mutator exists for an attribute. |
|
70
|
|
|
*coo. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $key |
|
73
|
|
|
* |
|
74
|
|
|
* @return bool |
|
75
|
|
|
*/ |
|
76
|
|
|
public function hasGetMutator($key) |
|
77
|
|
|
{ |
|
78
|
|
|
if ($this->isMultiUnitField($key) && isset($this->{$key})) { |
|
|
|
|
|
|
79
|
|
|
return true; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return parent::hasGetMutator($key); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Get the value of an attribute using its mutator. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $key |
|
89
|
|
|
* @param mixed $value |
|
90
|
|
|
* |
|
91
|
|
|
* @throws NotSupportedMultiUnitField |
|
92
|
|
|
* |
|
93
|
|
|
* @return mixed |
|
94
|
|
|
*/ |
|
95
|
|
|
public function mutateAttribute($key, $value) |
|
96
|
|
|
{ |
|
97
|
|
|
if ($this->isMultiUnitField($key)) { |
|
|
|
|
|
|
98
|
|
|
$requestedUnit = $this->getMultiUnitFieldUnit($key); |
|
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
$value = $this->getMultiUnitFieldValue($key, new $requestedUnit()); |
|
|
|
|
|
|
101
|
|
|
if (parent::hasGetMutator($key)) { |
|
|
|
|
|
|
102
|
|
|
return parent::mutateAttribute($key, $value); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $value; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return parent::mutateAttribute($key, $value); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Allows to set input units and process them before multi-unit field. |
|
113
|
|
|
* |
|
114
|
|
|
* @param array $attributes |
|
115
|
|
|
* |
|
116
|
|
|
* @return array |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function fillableFromArray(array $attributes) |
|
119
|
|
|
{ |
|
120
|
|
|
return array_merge($attributes, parent::fillableFromArray($attributes)); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Set the value of an attribute using its mutator. |
|
125
|
|
|
* |
|
126
|
|
|
* @param string $key |
|
127
|
|
|
* @param mixed $value |
|
128
|
|
|
* |
|
129
|
|
|
* @throws NotSupportedMultiUnitField |
|
130
|
|
|
* |
|
131
|
|
|
* @return mixed |
|
132
|
|
|
*/ |
|
133
|
|
|
protected function setMutatedAttributeValue($key, $value) |
|
134
|
|
|
{ |
|
135
|
|
|
if ($this->isMultiUnitField($key)) { |
|
|
|
|
|
|
136
|
|
|
$value = $this->processMultiUnitFieldChanges($key, $value); |
|
137
|
|
|
|
|
138
|
|
|
if (parent::hasSetMutator($key)) { |
|
|
|
|
|
|
139
|
|
|
return parent::setMutatedAttributeValue($key, $value); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return $value; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
parent::setMutatedAttributeValue($key, $value); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Detect changes and set proper database value |
|
150
|
|
|
* |
|
151
|
|
|
* @param $field |
|
152
|
|
|
* @param $value |
|
153
|
|
|
* |
|
154
|
|
|
* @throws NotSupportedMultiUnitField |
|
155
|
|
|
* |
|
156
|
|
|
* @return mixed |
|
157
|
|
|
*/ |
|
158
|
|
|
private function processMultiUnitFieldChanges($field, $value) |
|
159
|
|
|
{ |
|
160
|
|
|
if(!is_null($value)) { |
|
161
|
|
|
$existingConversionData = $this->getMultiUnitExistingConversionData($field); |
|
|
|
|
|
|
162
|
|
|
if (!is_null($existingConversionData)) { |
|
163
|
|
|
$inputUnit = $this->getMultiUnitFieldUnit($field); |
|
|
|
|
|
|
164
|
|
|
//change existing value only in case if new value doesn't match with stored conversion table or not exists |
|
165
|
|
|
if (!isset( |
|
166
|
|
|
$existingConversionData->{$inputUnit->getId()} |
|
167
|
|
|
) || $value != $existingConversionData->{$inputUnit->getId()}) { |
|
168
|
|
|
$defaultUnitValue = (new $inputUnit($value))->as($this->getMultiUnitFieldDefaultUnit($field)); |
|
|
|
|
|
|
169
|
|
|
$this->attributes[$field] = $defaultUnitValue; |
|
|
|
|
|
|
170
|
|
|
} elseif ($value == $existingConversionData->{$inputUnit->getId()}) { |
|
171
|
|
|
//forget changes if value actually isn't changed |
|
172
|
|
|
$defaultUnitValue = $existingConversionData->{$this->getMultiUnitFieldDefaultUnit($field)->getId()}; |
|
|
|
|
|
|
173
|
|
|
$this->attributes[$field] = $defaultUnitValue; |
|
174
|
|
|
$this->syncOriginalAttribute($field); |
|
|
|
|
|
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
return $value; |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
$this->attributes[$field] = $value; |
|
182
|
|
|
|
|
183
|
|
|
return $value; |
|
184
|
|
|
} |
|
185
|
|
|
} |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.