1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Merodiro\Friendships; |
4
|
|
|
|
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
|
28 |
|
} elseif ($friendship->count() == 2) { |
18
|
8 |
|
return 'FRIENDS'; |
19
|
28 |
|
} elseif ($friendship->first()->user_id == $this->id) { |
20
|
8 |
|
return 'WAITING'; |
21
|
|
|
} else { |
22
|
26 |
|
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 |
|
$this->friends()->attach($recipient); |
32
|
|
|
|
33
|
32 |
|
event('friendrequest.sent', [$this, $recipient]); |
34
|
|
|
|
35
|
32 |
|
return 'waiting'; |
36
|
|
|
} |
37
|
6 |
|
} |
38
|
|
|
|
39
|
22 |
|
public function acceptFriend($sender) |
40
|
|
|
{ |
41
|
22 |
|
$friendshipStatus = $this->checkFriendship($sender); |
42
|
|
|
|
43
|
22 |
|
if ($friendshipStatus == 'PENDING') { |
44
|
20 |
|
$this->friends()->attach($sender, ['status' => 1]); |
45
|
20 |
|
$sender->friends()->updateExistingPivot($this, ['status' => 1]); |
46
|
|
|
|
47
|
20 |
|
event('friendrequest.accepted', [$this, $sender]); |
48
|
|
|
|
49
|
20 |
|
return 'friends'; |
50
|
|
|
} |
51
|
4 |
|
} |
52
|
|
|
|
53
|
10 |
|
public function deleteFriend($user) |
54
|
|
|
{ |
55
|
10 |
|
$friendshipStatus = $this->checkFriendship($user); |
56
|
|
|
|
57
|
10 |
|
if ($friendshipStatus != 'NOT_FRIENDS') { |
58
|
10 |
|
$this->friends()->detach($user); |
59
|
10 |
|
$user->friends()->detach($this); |
60
|
|
|
|
61
|
10 |
|
event('friendship.deleted', [$this, $user]); |
62
|
|
|
|
63
|
10 |
|
return 'NOT_FRIENDS'; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function ban($user) |
68
|
|
|
{ |
69
|
|
|
$friendshipStatus = $this->checkFriendship($user); |
|
|
|
|
70
|
|
|
|
71
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
32 |
|
public function friends() |
75
|
|
|
{ |
76
|
32 |
|
return $this->belongsToMany(config('friendships.user_model'), 'friendships', 'user_id', 'friend_id') |
|
|
|
|
77
|
32 |
|
->where('status', 1); |
78
|
|
|
} |
79
|
|
|
|
80
|
14 |
|
public function friendRequestsReceived() |
81
|
|
|
{ |
82
|
14 |
|
return $this->belongsToMany(config('friendships.user_model'), 'friendships', 'friend_id', 'user_id') |
|
|
|
|
83
|
14 |
|
->where('status', 0); |
84
|
|
|
} |
85
|
|
|
|
86
|
8 |
|
public function friendRequestsSent() |
87
|
|
|
{ |
88
|
8 |
|
return $this->belongsToMany(config('friendships.user_model'), 'friendships', 'user_id', 'friend_id') |
|
|
|
|
89
|
8 |
|
->where('status', 0); |
90
|
|
|
} |
91
|
|
|
|
92
|
4 |
|
public function isFriendsWith($user) |
93
|
|
|
{ |
94
|
4 |
|
return $this->friends->contains($user); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
2 |
|
public function mutualFriendsCount($user) |
98
|
|
|
{ |
99
|
2 |
|
$userFriends = $user->friends->pluck('id'); |
100
|
2 |
|
$friends = $this->friends->pluck('id'); |
101
|
|
|
|
102
|
2 |
|
return $userFriends->intersect($friends)->count(); |
103
|
|
|
} |
104
|
|
|
|
105
|
2 |
|
public function mutualFriends($user) |
106
|
|
|
{ |
107
|
2 |
|
$userFriends = $user->friends->pluck('id'); |
108
|
2 |
|
$friends = $this->friends->pluck('id'); |
109
|
|
|
|
110
|
2 |
|
$mutualIds = $userFriends->intersect($friends); |
111
|
|
|
|
112
|
2 |
|
return static::whereIn('id', $mutualIds)->get(); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
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: