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 | * Invoked before a model is saved. Return false to abort the operation. |
||
78 | * |
||
79 | * @return bool |
||
80 | */ |
||
81 | public function preSave() |
||
115 | 5 | ||
116 | 5 | /** |
|
117 | 5 | * Get all of the changes that have been made, that are also supposed |
|
118 | * to be approved. |
||
119 | * |
||
120 | * @return array fields with new data, that should be recorded |
||
121 | 5 | */ |
|
122 | 5 | private function changedApprovableFields() |
|
144 | |||
145 | /** |
||
146 | 5 | * @param $key |
|
147 | * @return bool |
||
148 | */ |
||
149 | 5 | private function isApprovable($key) |
|
160 | |||
161 | /** |
||
162 | 5 | * @return mixed|null |
|
163 | */ |
||
164 | 5 | protected function getSystemUserId() |
|
168 | |||
169 | /** |
||
170 | * @return bool |
||
171 | 5 | */ |
|
172 | protected function currentUserCanApprove() |
||
176 | } |
||
177 |
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.