1 | <?php |
||
20 | trait CanBeAdjusted |
||
21 | { |
||
22 | /** |
||
23 | * A boolean to keep track of wether the model has been adjusted or not. |
||
24 | * |
||
25 | * @var bool |
||
26 | */ |
||
27 | protected $adjusted = false; |
||
28 | |||
29 | /** |
||
30 | * The booting method of the model trait. This method will be called once the model |
||
31 | * has been booted, and registers an event listener that listens for save calls |
||
32 | * to block if save protection is enabled. |
||
33 | * |
||
34 | * @return void |
||
35 | * @throws ModelAdjustedException |
||
36 | */ |
||
37 | protected static function bootCanBeAdjusted() |
||
45 | |||
46 | /** |
||
47 | * Adjusts the model by updating an existing record in the adjustments table or adds |
||
48 | * a new one if no previous adjustments are set. All changes will be merged with |
||
49 | * old changes and old changes can be unset using null. |
||
50 | * |
||
51 | * @param array $changes |
||
52 | * @param array $attributes |
||
53 | * @return Model|null |
||
54 | */ |
||
55 | public function adjust( array $changes, array $attributes = [ ] ) |
||
70 | |||
71 | /** |
||
72 | * Get the adjustment associated with the adjustable model. |
||
73 | * |
||
74 | * @return Relation |
||
75 | */ |
||
76 | public function adjustment():Relation |
||
84 | |||
85 | /** |
||
86 | * Fill the model instance with the adjusted values, replacing the original values. |
||
87 | * |
||
88 | * @return $this |
||
89 | */ |
||
90 | public function applyAdjustments():Model |
||
106 | |||
107 | /** |
||
108 | * Checks if the given model has applied the adjustments. |
||
109 | * |
||
110 | * @return bool |
||
111 | */ |
||
112 | public function isAdjusted():bool |
||
116 | |||
117 | /** |
||
118 | * Checks if save protection is enabled. Save protection protects you from persisting |
||
119 | * the model after applying the adjustments. |
||
120 | * |
||
121 | * @return bool |
||
122 | */ |
||
123 | public function hasSaveProtection():bool |
||
127 | |||
128 | /** |
||
129 | * We will fetch any existing changes from the adjustment and then filter them down |
||
130 | * based on certain criterias. Then we will return the changes converted to the |
||
131 | * correct data type depending on set casts on the Adjustment model. |
||
132 | * |
||
133 | * @param array $changes |
||
134 | * @param Model $adjustment |
||
135 | * @return Collection |
||
136 | */ |
||
137 | protected function mergeAndFilterChanges( array $changes, Model $adjustment ):Collection |
||
145 | |||
146 | /** |
||
147 | * Checks if a given attribute value pair is valid. The value should not be null and |
||
148 | * not be equal the original value, and the attribute should actually be fillable. |
||
149 | * |
||
150 | * @param string $attribute |
||
151 | * @param mixed $value |
||
152 | * @return bool |
||
153 | */ |
||
154 | protected function isValidChange( string $attribute, $value ):bool |
||
162 | |||
163 | /** |
||
164 | * Cast the changes collection to the appropiate type. |
||
165 | * |
||
166 | * @param Collection $changes |
||
167 | * @param Model $adjustment |
||
168 | * @return mixed |
||
169 | */ |
||
170 | protected function castChanges( Collection $changes, Model $adjustment ) |
||
184 | |||
185 | /** |
||
186 | * Fill the model with an array of attributes. |
||
187 | */ |
||
188 | abstract public function fill( array $attributes ); |
||
189 | |||
190 | /** |
||
191 | * Determine if the given attribute may be mass assigned. |
||
192 | */ |
||
193 | abstract public function isFillable( $key ); |
||
194 | |||
195 | /** |
||
196 | * Define a polymorphic one-to-one relationship. |
||
197 | */ |
||
198 | abstract public function morphOne( $related, $name, $type = null, $id = null, $localKey = null ); |
||
199 | |||
200 | /** |
||
201 | * Define a one-to-one relationship. |
||
202 | */ |
||
203 | abstract public function hasOne( $related, $foreignKey = null, $localKey = null ); |
||
204 | } |