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 | 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 | public function isPendingApproval($attribute = null) |
||
62 | |||
63 | /** |
||
64 | * Generates a list of the last $limit approvals to any objects of the class it is being called from. |
||
65 | * @param int $limit |
||
66 | * @param string $order |
||
67 | * @return mixed |
||
68 | */ |
||
69 | public function classApprovals($limit = 100, $order = 'desc') |
||
74 | |||
75 | /** |
||
76 | * Invoked before a model is saved. Return false to abort the operation. |
||
77 | * @return bool |
||
78 | */ |
||
79 | public function preSave() |
||
113 | |||
114 | /** |
||
115 | * Get all of the changes that have been made, that are also supposed |
||
116 | * to be approved. |
||
117 | * |
||
118 | * @return array fields with new data, that should be recorded |
||
119 | */ |
||
120 | private function changedApprovableFields() |
||
142 | |||
143 | /** |
||
144 | * @param $key |
||
145 | * @return bool |
||
146 | */ |
||
147 | private function isApprovable($key) |
||
158 | |||
159 | /** |
||
160 | * @return mixed|null |
||
161 | */ |
||
162 | protected function getSystemUserId() |
||
169 | |||
170 | /** |
||
171 | * @return bool |
||
172 | */ |
||
173 | protected function currentUserCanApprove() |
||
177 | } |
||
178 |
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.