3
3
namespace OwenIt \Auditing ;
4
4
5
5
use Illuminate \Database \Eloquent \Model ;
6
- use Illuminate \Support \Facades \Config ;
7
6
8
7
class Auditing extends Model
9
8
{
9
+ use CustomAuditMessage;
10
+
11
+ /**
12
+ * The attributes that should be appends.
13
+ *
14
+ * @var array
15
+ */
16
+ protected $ appends = [
17
+ 'custom_message ' ,
18
+ 'custom_fields ' ,
19
+ 'elapsed_time ' ,
20
+ ];
21
+
10
22
/**
11
23
* Indicates if the IDs are auto-incrementing.
12
24
*
@@ -21,12 +33,6 @@ class Auditing extends Model
21
33
*/
22
34
public $ timestamps = false ;
23
35
24
- /**
25
- * The table associated with the model.
26
- *
27
- * @var string
28
- */
29
- protected $ table = 'audits ' ;
30
36
31
37
/**
32
38
* The guarded attributes on the model.
@@ -53,205 +59,9 @@ class Auditing extends Model
53
59
];
54
60
55
61
/**
56
- * The attributes that should be appends.
57
- *
58
- * @var array
59
- */
60
- protected $ appends = [
61
- 'custom_message ' ,
62
- 'custom_fields ' ,
63
- 'elapsed_time ' ,
64
- ];
65
-
66
- /**
67
- * Get the auditable entity that the audits belongs to.
68
- */
69
- public function auditable ()
70
- {
71
- return $ this ->morphTo ();
72
- }
73
-
74
- /**
75
- * Author responsible for the change.
76
- *
77
- * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
78
- */
79
- public function user ()
80
- {
81
- return $ this ->belongsTo (Config::get ('auditing.model ' ));
82
- }
83
-
84
- /**
85
- * Get elapsed time.
86
- *
87
- * @return mixed
88
- */
89
- public function getElapsedTimeAttribute ()
90
- {
91
- return $ this ->created_at ->diffForHumans ();
92
- }
93
-
94
- /**
95
- * Custom output message.
96
- *
97
- * @return mixed
98
- */
99
- public function getCustomMessageAttribute ()
100
- {
101
- if (class_exists ($ class = $ this ->auditable_type )) {
102
- return $ this ->resolveCustomMessage ($ this ->getCustomMessage ($ class ));
103
- } else {
104
- return false ;
105
- }
106
- }
107
-
108
- /**
109
- * Custom output fields.
110
- *
111
- * @return array
112
- */
113
- public function getCustomFieldsAttribute ()
114
- {
115
- if (class_exists ($ class = $ this ->auditable_type )) {
116
- $ customFields = [];
117
-
118
- foreach ($ this ->getCustomFields ($ class ) as $ field => $ message ) {
119
- if (is_array ($ message ) && isset ($ message [$ this ->type ])) {
120
- $ customFields [$ field ] = $ this ->resolveCustomMessage ($ message [$ this ->type ]);
121
- } elseif (is_string ($ message )) {
122
- $ customFields [$ field ] = $ this ->resolveCustomMessage ($ message );
123
- }
124
- }
125
-
126
- return array_filter ($ customFields );
127
- }
128
-
129
- return [];
130
- }
131
-
132
- /**
133
- * Get custom message.
134
- *
135
- * @return string
136
- */
137
- public function getCustomMessage ($ class )
138
- {
139
- if (!isset ($ class ::$ auditCustomMessage )) {
140
- return 'Not defined custom message! ' ;
141
- }
142
-
143
- return $ class ::$ auditCustomMessage ;
144
- }
145
-
146
- /**
147
- * Get custom fields.
148
- *
149
- * @return string
150
- */
151
- public function getCustomFields ($ class )
152
- {
153
- if (!isset ($ class ::$ auditCustomFields )) {
154
- return [];
155
- }
156
-
157
- return $ class ::$ auditCustomFields ;
158
- }
159
-
160
- /**
161
- * Resolve custom message.
162
- *
163
- * @param $message
164
- *
165
- * @return mixed
166
- */
167
- public function resolveCustomMessage ($ message )
168
- {
169
- preg_match_all ('/\{[\w.| ]+\}/ ' , $ message , $ segments );
170
-
171
- foreach (current ($ segments ) as $ segment ) {
172
- $ pipe = str_replace (['{ ' , '} ' ], '' , $ segment );
173
-
174
- list ($ property , $ defaultValue , $ method ) = array_pad (
175
- explode ('| ' , $ pipe , 3 ), 3 , null
176
- );
177
-
178
- if (empty ($ defaultValue ) && !empty ($ method )) {
179
- $ defaultValue = $ this ->callback ($ method );
180
- }
181
-
182
- $ valueSegmented = $ this ->getValueSegmented ($ this , $ property , $ defaultValue ?: ' ' );
183
-
184
- $ message = str_replace ($ segment , $ valueSegmented , $ message );
185
- }
186
-
187
- return $ message ;
188
- }
189
-
190
- /**
191
- * Message callback.
192
- *
193
- * @param $function
194
- *
195
- * @return mixed
196
- */
197
- public function callback ($ method )
198
- {
199
- if (method_exists ($ this ->auditable , $ method )) {
200
- return $ this ->auditable ->{$ method }($ this );
201
- }
202
- }
203
-
204
- /**
205
- * Get the database connection for the model.
206
- *
207
- * @return \Illuminate\Database\Connection
208
- */
209
- public function getConnection ()
210
- {
211
- return static ::resolveConnection (Config::get ('auditing.connection ' ));
212
- }
213
-
214
- /**
215
- * Get Value of segment.
216
- *
217
- * @param $object
218
- * @param $key
219
- * @param $default
220
- *
221
- * @return mixed
222
- */
223
- public function getValueSegmented ($ object , $ key , $ default )
224
- {
225
- if (empty ($ key )) {
226
- return $ default ;
227
- }
228
-
229
- foreach (explode ('. ' , $ key ) as $ segment ) {
230
- $ object = is_array ($ object ) ? (object ) $ object : $ object ;
231
-
232
- if (!isset ($ object ->{$ segment })) {
233
- return $ default ;
234
- }
235
-
236
- $ object = $ object ->{$ segment };
237
- }
238
-
239
- return $ object ;
240
- }
241
-
242
- /**
243
- * Get the table associated with the model.
62
+ * The table associated with the model.
244
63
*
245
- * @return string
64
+ * @var string
246
65
*/
247
- public function getTable ()
248
- {
249
- $ table = Config::get ('auditing.table ' );
250
-
251
- if (!empty ($ table )) {
252
- return $ table ;
253
- }
254
-
255
- return parent ::getTable ();
256
- }
66
+ protected $ table = 'audits ' ;
257
67
}
0 commit comments