1 | <?php |
||
16 | trait HasRevisions |
||
17 | { |
||
18 | use SaveRevisionJsonRepresentation; |
||
19 | use RollbackRevisionJsonRepresentation; |
||
20 | |||
21 | /** |
||
22 | * The container for all the options necessary for this trait. |
||
23 | * Options can be viewed in the Neurony\Revisions\Options\RevisionOptions file. |
||
24 | * |
||
25 | * @var RevisionOptions |
||
26 | */ |
||
27 | protected $revisionOptions; |
||
28 | |||
29 | /** |
||
30 | * Set the options for the HasRevisions trait. |
||
31 | * |
||
32 | * @return RevisionOptions |
||
33 | */ |
||
34 | abstract public function getRevisionOptions(): RevisionOptions; |
||
35 | |||
36 | /** |
||
37 | * Boot the trait. |
||
38 | * Remove blocks on save and delete if one or many locations from model's instance have been changed/removed. |
||
39 | * |
||
40 | * @return void |
||
41 | */ |
||
42 | public static function bootHasRevisions() |
||
58 | |||
59 | /** |
||
60 | * Register a revisioning model event with the dispatcher. |
||
61 | * |
||
62 | * @param Closure|string $callback |
||
63 | * @return void |
||
64 | */ |
||
65 | public static function revisioning($callback): void |
||
69 | |||
70 | /** |
||
71 | * Register a revisioned model event with the dispatcher. |
||
72 | * |
||
73 | * @param Closure|string $callback |
||
74 | * @return void |
||
75 | */ |
||
76 | public static function revisioned($callback): void |
||
80 | |||
81 | /** |
||
82 | * Get all the revisions for a given model instance. |
||
83 | * |
||
84 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany |
||
85 | */ |
||
86 | public function revisions() |
||
92 | |||
93 | /** |
||
94 | * Create a new revision record for the model instance. |
||
95 | * |
||
96 | * @return Revision|bool |
||
97 | * @throws Exception |
||
98 | */ |
||
99 | public function createNewRevision() |
||
125 | |||
126 | /** |
||
127 | * Manually save a new revision for a model instance. |
||
128 | * This method should be called manually only where and if needed. |
||
129 | * |
||
130 | * @return Revision |
||
131 | * @throws Exception |
||
132 | */ |
||
133 | public function saveAsRevision(): Revision |
||
152 | |||
153 | /** |
||
154 | * Rollback the model instance to the given revision instance. |
||
155 | * |
||
156 | * @param RevisionModelContract $revision |
||
157 | * @return bool |
||
158 | * @throws Exception |
||
159 | */ |
||
160 | public function rollbackToRevision(RevisionModelContract $revision): bool |
||
194 | |||
195 | /** |
||
196 | * Remove all existing revisions from the database, belonging to a model instance. |
||
197 | * |
||
198 | * @return void |
||
199 | * @throws Exception |
||
200 | */ |
||
201 | public function deleteAllRevisions(): void |
||
209 | |||
210 | /** |
||
211 | * If a revision record limit is set on the model and that limit is exceeded. |
||
212 | * Remove the oldest revisions until the limit is met. |
||
213 | * |
||
214 | * @return void |
||
215 | */ |
||
216 | public function clearOldRevisions(): void |
||
227 | |||
228 | /** |
||
229 | * Determine if a revision should be stored for a given model instance. |
||
230 | * |
||
231 | * Check the revisionable fields set on the model. |
||
232 | * If any of those fields have changed, then a new revisions should be stored. |
||
233 | * If no fields are specifically set on the model, this will return true. |
||
234 | * |
||
235 | * @return bool |
||
236 | */ |
||
237 | protected function shouldCreateRevision(): bool |
||
261 | |||
262 | /** |
||
263 | * Both instantiate the revision options as well as validate their contents. |
||
264 | * |
||
265 | * @return void |
||
266 | */ |
||
267 | protected function initRevisionOptions(): void |
||
273 | } |
||
274 |
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
Idable
provides a methodequalsId
that 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.