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 = [ ] ) |
||
78 | |||
79 | /** |
||
80 | * Get the adjustment associated with the adjustable model. |
||
81 | * |
||
82 | * @return Relation |
||
83 | */ |
||
84 | public function adjustment():Relation |
||
92 | |||
93 | /** |
||
94 | * Fill the model instance with the adjusted values, replacing the original values. |
||
95 | * |
||
96 | * @return self |
||
97 | */ |
||
98 | public function applyAdjustments():Model |
||
113 | |||
114 | /** |
||
115 | * Checks if the given model has applied the adjustments. |
||
116 | * |
||
117 | * @return bool |
||
118 | */ |
||
119 | public function isAdjusted():bool |
||
123 | |||
124 | /** |
||
125 | * Checks if save protection is enabled. Save protection protects you from persisting |
||
126 | * the model after applying the adjustments. |
||
127 | * |
||
128 | * @return bool |
||
129 | */ |
||
130 | public function hasSaveProtection():bool |
||
134 | |||
135 | /** |
||
136 | * Check if the changes attribute has any set casts on the given model, and if so we |
||
137 | * cast the changes collection to the appropiate type. |
||
138 | * |
||
139 | * @param Collection $changes |
||
140 | * @param Model $adjustment |
||
141 | * @return mixed |
||
142 | */ |
||
143 | protected function castChanges( Collection $changes, Model $adjustment ) |
||
155 | } |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: