1 | <?php namespace Mpociot\Teamwork\Traits; |
||
15 | trait UserHasTeams |
||
16 | { |
||
17 | /** |
||
18 | * Many-to-Many relations with the user model. |
||
19 | * |
||
20 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
21 | */ |
||
22 | public function teams() |
||
26 | |||
27 | /** |
||
28 | * has-one relation with the current selected team model. |
||
29 | * |
||
30 | * @return \Illuminate\Database\Eloquent\Relations\HasOne |
||
31 | */ |
||
32 | public function currentTeam() |
||
36 | |||
37 | /** |
||
38 | * @return mixed |
||
39 | */ |
||
40 | public function ownedTeams() |
||
44 | |||
45 | /** |
||
46 | * One-to-Many relation with the invite model |
||
47 | * @return mixed |
||
48 | */ |
||
49 | public function invites() |
||
53 | |||
54 | /** |
||
55 | * Boot the user model |
||
56 | * Attach event listener to remove the many-to-many records when trying to delete |
||
57 | * Will NOT delete any records if the user model uses soft deletes. |
||
58 | * |
||
59 | * @return void|bool |
||
60 | */ |
||
61 | public static function bootUserHasTeams() |
||
62 | { |
||
63 | static::deleting( function ( Model $user ) |
||
64 | { |
||
65 | if ( !method_exists( Config::get( 'teamwork.user_model' ), 'bootSoftDeletes' ) ) |
||
66 | { |
||
67 | $user->teams()->sync( [ ] ); |
||
68 | } |
||
69 | return true; |
||
70 | } ); |
||
71 | } |
||
72 | |||
73 | |||
74 | /** |
||
75 | * Returns if the user owns a team |
||
76 | * |
||
77 | * @return bool |
||
78 | */ |
||
79 | public function isOwner() |
||
83 | |||
84 | /** |
||
85 | * Wrapper method for "isOwner" |
||
86 | * |
||
87 | * @return bool |
||
88 | */ |
||
89 | public function isTeamOwner() |
||
93 | |||
94 | /** |
||
95 | * @param $team |
||
96 | * @return mixed |
||
97 | */ |
||
98 | protected function retrieveTeamId( $team ) |
||
110 | |||
111 | |||
112 | /** |
||
113 | * Returns if the user owns the given team |
||
114 | * |
||
115 | * @param mixed $team |
||
116 | * @return bool |
||
117 | */ |
||
118 | public function isOwnerOfTeam( $team ) |
||
128 | |||
129 | /** |
||
130 | * Alias to eloquent many-to-many relation's attach() method. |
||
131 | * |
||
132 | * @param mixed $team |
||
133 | * @return $this |
||
134 | */ |
||
135 | public function attachTeam( $team ) |
||
166 | |||
167 | /** |
||
168 | * Alias to eloquent many-to-many relation's detach() method. |
||
169 | * |
||
170 | * @param mixed $team |
||
171 | * @return $this |
||
172 | */ |
||
173 | public function detachTeam( $team ) |
||
198 | |||
199 | /** |
||
200 | * Attach multiple teams to a user |
||
201 | * |
||
202 | * @param mixed $teams |
||
203 | * @return $this |
||
204 | */ |
||
205 | public function attachTeams( $teams ) |
||
213 | |||
214 | /** |
||
215 | * Detach multiple teams from a user |
||
216 | * |
||
217 | * @param mixed $teams |
||
218 | * @return $this |
||
219 | */ |
||
220 | public function detachTeams( $teams ) |
||
228 | |||
229 | /** |
||
230 | * Switch the current team of the user |
||
231 | * |
||
232 | * @param object|array|integer $team |
||
233 | * @return $this |
||
234 | * @throws ModelNotFoundException |
||
235 | * @throws UserNotInTeamException |
||
236 | */ |
||
237 | public function switchTeam( $team ) |
||
266 | } |
||
267 |
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.