1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaraCed; |
4
|
|
|
|
5
|
|
|
trait LaraCedTrait |
6
|
|
|
{ |
7
|
|
|
public static function bootLaraCedTrait() |
8
|
|
|
{ |
9
|
|
|
static::observe(new LaraCedObserver); |
10
|
|
|
} |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Has the model loaded the SoftDeletes trait. |
14
|
|
|
* |
15
|
|
|
* @return bool |
16
|
|
|
*/ |
17
|
|
|
public static function isUseSoftDeletes() |
18
|
|
|
{ |
19
|
|
|
static $isUseSoftDeletes; |
20
|
|
|
if (is_null($isUseSoftDeletes)) { |
21
|
|
|
return $isUseSoftDeletes = in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses_recursive(get_called_class())); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
return $isUseSoftDeletes; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Get user model. |
29
|
|
|
*/ |
30
|
|
|
public function getUserModel() |
31
|
|
|
{ |
32
|
|
|
$class = config('auth.providers.users.model'); |
33
|
|
|
|
34
|
|
|
return $class; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get the name of the "creator" column. |
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
public function getCreatorColumn() |
43
|
|
|
{ |
44
|
|
|
return defined('static::CREATOR_COLUMN') ? static::CREATOR_COLUMN : 'created_by'; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get the name of the "editor" column. |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function getEditorColumn() |
53
|
|
|
{ |
54
|
|
|
return defined('static::EDITOR_COLUMN') ? static::EDITOR_COLUMN : 'updated_by'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get the name of the "destroyer" column. |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function getDestroyerColumn() |
63
|
|
|
{ |
64
|
|
|
return defined('static::DESTROYER_COLUMN') ? static::DESTROYER_COLUMN : 'deleted_by'; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Creator, relation with user model. |
69
|
|
|
*/ |
70
|
|
|
public function creator() |
71
|
|
|
{ |
72
|
|
|
return $this->belongsTo($this->getUserModel(), $this->getCreatorColumn()); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Editor, relation with user model. |
77
|
|
|
*/ |
78
|
|
|
public function editor() |
79
|
|
|
{ |
80
|
|
|
return $this->belongsTo($this->getUserModel(), $this->getEditorColumn()); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Destroyer, relation with user model. |
85
|
|
|
*/ |
86
|
|
|
public function destroyer() |
87
|
|
|
{ |
88
|
|
|
return $this->belongsTo($this->getUserModel(), $this->getDestroyerColumn()); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
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.