1 | <?php |
||
8 | trait VersionableTrait |
||
9 | { |
||
10 | /** |
||
11 | * Retrieve, if exists, the property that define that Version model. |
||
12 | * If no property defined, use the default Version model. |
||
13 | * |
||
14 | * Trait cannot share properties whth their class ! |
||
15 | * http://php.net/manual/en/language.oop5.traits.php |
||
16 | * @return unknown|string |
||
17 | */ |
||
18 | protected function getVersionClass() |
||
26 | |||
27 | /** |
||
28 | * Private variable to detect if this is an update |
||
29 | * or an insert |
||
30 | * @var bool |
||
31 | */ |
||
32 | private $updating; |
||
33 | |||
34 | /** |
||
35 | * Contains all dirty data that is valid for versioning |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | private $versionableDirtyData; |
||
40 | |||
41 | /** |
||
42 | * Optional reason, why this version was created |
||
43 | * @var string |
||
44 | */ |
||
45 | private $reason; |
||
46 | |||
47 | /** |
||
48 | * Flag that determines if the model allows versioning at all |
||
49 | * @var bool |
||
50 | */ |
||
51 | protected $versioningEnabled = true; |
||
52 | |||
53 | /** |
||
54 | * @return $this |
||
55 | */ |
||
56 | public function enableVersioning() |
||
62 | |||
63 | /** |
||
64 | * @return $this |
||
65 | */ |
||
66 | public function disableVersioning() |
||
72 | |||
73 | /** |
||
74 | * Attribute mutator for "reason" |
||
75 | * Prevent "reason" to become a database attribute of model |
||
76 | * |
||
77 | * @param string $value |
||
78 | */ |
||
79 | public function setReasonAttribute($value) |
||
83 | |||
84 | /** |
||
85 | * Initialize model events |
||
86 | */ |
||
87 | public static function bootVersionableTrait() |
||
97 | |||
98 | /** |
||
99 | * Return all versions of the model |
||
100 | * @return MorphMany |
||
101 | */ |
||
102 | public function versions() |
||
106 | |||
107 | /** |
||
108 | * Returns the latest version available |
||
109 | * @return Version |
||
110 | */ |
||
111 | public function currentVersion() |
||
116 | |||
117 | /** |
||
118 | * Returns the previous version |
||
119 | * @return Version |
||
120 | */ |
||
121 | public function previousVersion() |
||
125 | |||
126 | /** |
||
127 | * Get a model based on the version id |
||
128 | * |
||
129 | * @param $version_id |
||
130 | * |
||
131 | * @return $this|null |
||
132 | */ |
||
133 | public function getVersionModel($version_id) |
||
142 | |||
143 | /** |
||
144 | * Pre save hook to determine if versioning is enabled and if we're updating |
||
145 | * the model |
||
146 | * @return void |
||
147 | */ |
||
148 | protected function versionablePreSave() |
||
155 | |||
156 | /** |
||
157 | * Save a new version. |
||
158 | * @return void |
||
159 | */ |
||
160 | protected function versionablePostSave() |
||
188 | |||
189 | /** |
||
190 | * Delete old versions of this model when the reach a specific count. |
||
191 | * |
||
192 | * @return void |
||
193 | */ |
||
194 | private function purgeOldVersions() |
||
210 | |||
211 | /** |
||
212 | * Determine if a new version should be created for this model. |
||
213 | * |
||
214 | * @return bool |
||
215 | */ |
||
216 | private function isValidForVersioning() |
||
227 | |||
228 | /** |
||
229 | * @return int|null |
||
230 | */ |
||
231 | protected function getAuthUserId() |
||
240 | |||
241 | /** |
||
242 | * @return string|null |
||
243 | */ |
||
244 | protected function getAuthGuard() |
||
253 | } |
||
254 |
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: