1 | <?php |
||
14 | trait InteractsWithRole |
||
15 | { |
||
16 | private $roleClass; |
||
17 | |||
18 | /** |
||
19 | * Check if user has the given role. |
||
20 | * |
||
21 | * @param string|array $role |
||
22 | * @return bool |
||
23 | */ |
||
24 | public function hasRole($role): bool |
||
37 | |||
38 | /** |
||
39 | * Get all user roles. |
||
40 | * |
||
41 | * @return array |
||
42 | */ |
||
43 | public function getRoleSlugs(): array |
||
47 | |||
48 | /** |
||
49 | * Attach a role to user using slug. |
||
50 | * |
||
51 | * @param string $slug |
||
52 | */ |
||
53 | public function attachRoleBySlug(string $slug) |
||
59 | |||
60 | /** |
||
61 | * Attach a role to user. |
||
62 | * |
||
63 | * @param mixed $role |
||
64 | * @param array $attributes |
||
65 | * @param bool $touch |
||
66 | */ |
||
67 | public function attachRole($role, array $attributes = [], $touch = true) |
||
73 | |||
74 | /** |
||
75 | * Model can have many roles. |
||
76 | * |
||
77 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
78 | */ |
||
79 | public function roles(): BelongsToMany |
||
83 | |||
84 | /** |
||
85 | * Find a role by slug. |
||
86 | * |
||
87 | * @param string $slug |
||
88 | * @return \Illuminate\Database\Eloquent\Model|static |
||
89 | * @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
||
90 | */ |
||
91 | protected function findRoleBySlug(string $slug): Role |
||
95 | |||
96 | /** |
||
97 | * Get Role class. |
||
98 | * |
||
99 | * @return Role |
||
100 | */ |
||
101 | public function getRoleClass(): Role |
||
109 | |||
110 | /** |
||
111 | * Query scope for user having the given roles. |
||
112 | * |
||
113 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
114 | * @param mixed $roles |
||
115 | * @return \Illuminate\Database\Eloquent\Builder |
||
116 | */ |
||
117 | public function scopeHavingRoles(Builder $query, $roles): Builder |
||
126 | |||
127 | /** |
||
128 | * Query scope for user having the given roles by slugs. |
||
129 | * |
||
130 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
131 | * @param mixed $slugs |
||
132 | * @return \Illuminate\Database\Eloquent\Builder |
||
133 | */ |
||
134 | public function scopeHavingRolesBySlugs(Builder $query, $slugs): Builder |
||
140 | |||
141 | /** |
||
142 | * Revokes the given role from the user using slug. |
||
143 | * |
||
144 | * @param string|array $slug |
||
145 | * @param bool $touch |
||
146 | * @return int |
||
147 | */ |
||
148 | public function revokeRoleBySlug($slug, $touch = true): int |
||
161 | |||
162 | /** |
||
163 | * Revokes the given role from the user. |
||
164 | * |
||
165 | * @param mixed $role |
||
166 | * @param bool $touch |
||
167 | * @return int |
||
168 | */ |
||
169 | public function revokeRole($role, $touch = true): int |
||
177 | |||
178 | /** |
||
179 | * Syncs the given role(s) with the user. |
||
180 | * |
||
181 | * @param \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array $roles |
||
182 | * @param bool $detaching |
||
183 | * @return array |
||
184 | */ |
||
185 | public function syncRoles($roles, $detaching = true): array |
||
193 | |||
194 | /** |
||
195 | * Revokes all roles from the user. |
||
196 | * |
||
197 | * @return int |
||
198 | */ |
||
199 | public function revokeAllRoles(): int |
||
207 | } |
||
208 |
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.