1 | <?php |
||
12 | trait VersionableTrait |
||
13 | { |
||
14 | |||
15 | /** |
||
16 | * Private variable to detect if this is an update |
||
17 | * or an insert |
||
18 | * @var bool |
||
19 | */ |
||
20 | private $updating; |
||
21 | |||
22 | /** |
||
23 | * Contains all dirty data that is valid for versioning |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | private $versionableDirtyData; |
||
28 | |||
29 | /** |
||
30 | * Optional reason, why this version was created |
||
31 | * @var string |
||
32 | */ |
||
33 | private $reason; |
||
34 | |||
35 | /** |
||
36 | * Flag that determines if the model allows versioning at all |
||
37 | * @var bool |
||
38 | */ |
||
39 | protected $versioningEnabled = true; |
||
40 | |||
41 | /** |
||
42 | * @return $this |
||
43 | */ |
||
44 | public function enableVersioning() |
||
49 | |||
50 | /** |
||
51 | * @return $this |
||
52 | */ |
||
53 | public function disableVersioning() |
||
58 | |||
59 | /** |
||
60 | * Attribute mutator for "reason" |
||
61 | * Prevent "reason" to become a database attribute of model |
||
62 | * |
||
63 | * @param string $value |
||
64 | */ |
||
65 | public function setReasonAttribute($value) |
||
69 | |||
70 | /** |
||
71 | * Initialize model events |
||
72 | */ |
||
73 | public static function bootVersionableTrait() |
||
84 | |||
85 | /** |
||
86 | * Return all versions of the model |
||
87 | * @return MorphMany |
||
88 | */ |
||
89 | public function versions() |
||
93 | |||
94 | /** |
||
95 | * Returns the latest version available |
||
96 | * @return Version |
||
97 | */ |
||
98 | public function currentVersion() |
||
102 | |||
103 | /** |
||
104 | * Returns the previous version |
||
105 | * @return Version |
||
106 | */ |
||
107 | public function previousVersion() |
||
111 | |||
112 | /** |
||
113 | * Get a model based on the version id |
||
114 | * |
||
115 | * @param $version_id |
||
116 | * |
||
117 | * @return $this|null |
||
118 | */ |
||
119 | public function getVersionModel($version_id) |
||
127 | |||
128 | /** |
||
129 | * Pre save hook to determine if versioning is enabled and if we're updating |
||
130 | * the model |
||
131 | * @return void |
||
132 | */ |
||
133 | protected function versionablePreSave() |
||
140 | |||
141 | /** |
||
142 | * Save a new version. |
||
143 | * @return void |
||
144 | */ |
||
145 | protected function versionablePostSave() |
||
168 | |||
169 | /** |
||
170 | * Determine if a new version should be created for this model. |
||
171 | * |
||
172 | * @return bool |
||
173 | */ |
||
174 | private function isValidForVersioning() |
||
185 | |||
186 | /** |
||
187 | * @return int|null |
||
188 | */ |
||
189 | protected function getAuthUserId() |
||
204 | |||
205 | |||
206 | } |
||
207 |
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.