1 | <?php |
||
10 | trait Approvable |
||
11 | { |
||
12 | |||
13 | /** @var array */ |
||
14 | public $approveOf = array(); |
||
15 | |||
16 | /** @var array */ |
||
17 | public $dontApproveOf = array(); |
||
18 | |||
19 | /** @var bool */ |
||
20 | protected $withoutApproval = false; |
||
21 | |||
22 | /** |
||
23 | * Create the event listeners for the saving event |
||
24 | * This lets us save approvals whenever a save is made, no matter the |
||
25 | * http method |
||
26 | */ |
||
27 | 15 | public static function bootApprovable(): void |
|
33 | |||
34 | 5 | public function approvals(): MorphMany |
|
38 | |||
39 | /** |
||
40 | * Check if this model has pending changes, |
||
41 | * If an attribute is provided, check if the attribute has pending changes. |
||
42 | * |
||
43 | * @param null $attribute |
||
44 | * @return bool |
||
45 | */ |
||
46 | 2 | public function isPendingApproval($attribute = null): bool |
|
47 | { |
||
48 | 2 | return $this->approvals() |
|
49 | ->when($attribute !== null, function ($query) use ($attribute) { |
||
50 | 1 | $query->where('key', $attribute); |
|
51 | 2 | }) |
|
52 | 2 | ->where('approved', null) |
|
53 | 2 | ->exists(); |
|
54 | } |
||
55 | |||
56 | /** |
||
57 | * List all the attributes, that currently have pending changes. |
||
58 | * |
||
59 | * @return \Illuminate\Support\Collection |
||
60 | */ |
||
61 | 1 | public function getPendingApprovalAttributes(): Collection |
|
68 | |||
69 | /** |
||
70 | * Disable the approval process for this model instance. |
||
71 | * |
||
72 | * @param bool $withoutApproval Deprecated, see withoApproval() |
||
73 | * Will be removed in 2.0.0 |
||
74 | * |
||
75 | * @return self |
||
76 | */ |
||
77 | 1 | public function withoutApproval(bool $withoutApproval = true): self |
|
83 | |||
84 | /** |
||
85 | * Enable the approval process for this model instance |
||
86 | * |
||
87 | * @return self |
||
88 | */ |
||
89 | 1 | public function withApproval(): self |
|
95 | |||
96 | /** |
||
97 | * Invoked before a model is saved. Return false to abort the operation. |
||
98 | * |
||
99 | * @return bool |
||
100 | */ |
||
101 | 15 | protected function preSave(): bool |
|
139 | |||
140 | /** |
||
141 | * Get all of the changes that have been made, that are also supposed |
||
142 | * to be approved. |
||
143 | * |
||
144 | * @return array fields with new data, that should be recorded |
||
145 | */ |
||
146 | 11 | private function changedApprovableFields(): array |
|
168 | |||
169 | /** |
||
170 | * Return whether an attribute of this model should be approvable. |
||
171 | * |
||
172 | * @param string $key |
||
173 | * @return bool |
||
174 | */ |
||
175 | 11 | private function isApprovable(string $key): bool |
|
186 | |||
187 | /** |
||
188 | * Get the user id that should be stored as the requester for the approval. |
||
189 | * |
||
190 | * @return int|null |
||
191 | */ |
||
192 | 11 | protected function getSystemUserId(): ?int |
|
196 | |||
197 | /** |
||
198 | * Check if the approval process needs to happen for the currently logged in user. |
||
199 | * |
||
200 | * @return bool |
||
201 | */ |
||
202 | 2 | protected function currentUserCanApprove(): bool |
|
206 | } |
||
207 |
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.