1 | <?php |
||
13 | trait Approvable |
||
14 | { |
||
15 | |||
16 | /** |
||
17 | * @var array |
||
18 | */ |
||
19 | protected $approveOf = array(); |
||
20 | |||
21 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $dontApproveOf = array(); |
||
25 | |||
26 | /** |
||
27 | * Create the event listeners for the saving event |
||
28 | * This lets us save approvals whenever a save is made, no matter the |
||
29 | * http method |
||
30 | */ |
||
31 | public static function bootApprovable() |
||
37 | |||
38 | /** |
||
39 | * @return MorphMany |
||
40 | */ |
||
41 | 3 | public function approvals() |
|
45 | |||
46 | /** |
||
47 | * Check if this model has pending changes, |
||
48 | * If an attribute is provided, check if the attribute has pending changes. |
||
49 | * |
||
50 | * @param null $attribute |
||
51 | * @return bool |
||
52 | */ |
||
53 | 2 | public function isPendingApproval($attribute = null) |
|
62 | |||
63 | /** |
||
64 | * List all the attributes, that currently have pending changes. |
||
65 | * |
||
66 | * @return \Illuminate\Support\Collection |
||
67 | */ |
||
68 | 1 | public function getPendingApprovalAttributes() |
|
75 | |||
76 | /** |
||
77 | * Generates a list of the last $limit approvals to any objects of the class it is being called from. |
||
78 | * |
||
79 | * @param int $limit |
||
80 | * @param string $order |
||
81 | * @return mixed |
||
82 | */ |
||
83 | 1 | public function classApprovals($limit = 100, $order = 'desc') |
|
84 | { |
||
85 | 1 | return Approval::where('approvable_type', get_called_class()) |
|
86 | 1 | ->orderBy('updated_at', $order)->limit($limit)->get(); |
|
87 | } |
||
88 | |||
89 | /** |
||
90 | * Invoked before a model is saved. Return false to abort the operation. |
||
91 | * |
||
92 | * @return bool |
||
93 | */ |
||
94 | 8 | public function preSave() |
|
128 | |||
129 | /** |
||
130 | * Get all of the changes that have been made, that are also supposed |
||
131 | * to be approved. |
||
132 | * |
||
133 | * @return array fields with new data, that should be recorded |
||
134 | */ |
||
135 | 5 | private function changedApprovableFields() |
|
157 | |||
158 | /** |
||
159 | * @param $key |
||
160 | * @return bool |
||
161 | */ |
||
162 | 5 | private function isApprovable($key) |
|
173 | |||
174 | /** |
||
175 | * @return mixed|null |
||
176 | */ |
||
177 | 5 | protected function getSystemUserId() |
|
184 | |||
185 | /** |
||
186 | * @return bool |
||
187 | */ |
||
188 | protected function currentUserCanApprove() |
||
192 | } |
||
193 |
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.