1 | <?php |
||
5 | trait Friendable |
||
6 | { |
||
7 | 34 | public function checkFriendship($user) |
|
8 | { |
||
9 | 34 | if ($this->id == $user->id) { |
|
|
|||
10 | 2 | return 'same_user'; |
|
11 | } |
||
12 | |||
13 | 32 | $friendship = Friendship::betweenUsers($this, $user); |
|
14 | |||
15 | 32 | if ($friendship->count() == 0) { |
|
16 | 32 | return 'not_friends'; |
|
17 | 30 | } elseif ($friendship->count() == 2) { |
|
18 | 8 | return 'friends'; |
|
19 | 30 | } elseif ($friendship->first()->user_id == $this->id) { |
|
20 | 10 | return 'waiting'; |
|
21 | } else { |
||
22 | 28 | return 'pending'; |
|
23 | } |
||
24 | } |
||
25 | |||
26 | 34 | public function addFriend($recipient) |
|
27 | { |
||
28 | 34 | $friendshipStatus = $this->checkFriendship($recipient); |
|
29 | |||
30 | 34 | if ($friendshipStatus == 'not_friends') { |
|
31 | 32 | Friendship::create([ |
|
32 | 32 | 'user_id' => $this->id, |
|
33 | 32 | 'friend_id' => $recipient->id, |
|
34 | ]); |
||
35 | 32 | event('friendrequest.sent', [$this, $recipient]); |
|
36 | } |
||
37 | |||
38 | 34 | return $friendshipStatus == 'waiting'; |
|
39 | } |
||
40 | |||
41 | 22 | public function acceptFriend($sender) |
|
42 | { |
||
43 | 22 | $friendshipStatus = $this->checkFriendship($sender); |
|
44 | |||
45 | 22 | if ($friendshipStatus == 'pending') { |
|
46 | 20 | Friendship::create([ |
|
47 | 20 | 'user_id' => $this->id, |
|
48 | 20 | 'friend_id' => $sender->id, |
|
49 | 20 | 'status' => 1 |
|
50 | ]); |
||
51 | 20 | Friendship::betweenUsers($this, $sender) |
|
52 | 20 | ->update(['status' => 1]); |
|
53 | 20 | event('friendrequest.accepted', [$this, $sender]); |
|
54 | } |
||
55 | |||
56 | 22 | return $friendshipStatus == 'friends'; |
|
57 | } |
||
58 | |||
59 | 10 | public function deleteFriend($user) |
|
71 | |||
72 | 2 | public function friendsIds() |
|
73 | { |
||
74 | $friendsIds = Friendship::where(function ($query) { |
||
86 | |||
87 | 4 | public function friends() |
|
93 | |||
94 | 14 | public function friendRequestsReceived() |
|
99 | |||
100 | 8 | public function friendRequestsSent() |
|
105 | |||
106 | 4 | public function isFriendsWith($user) |
|
112 | |||
113 | 2 | public function mutualFriendsCount($user) |
|
120 | |||
121 | 2 | public function mutualFriends($user) |
|
130 | } |
||
131 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: