1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Turahe\Likeable\Traits; |
4
|
|
|
|
5
|
|
|
use Turahe\Likeable\Enums\LikeType; |
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
7
|
|
|
use Turahe\Likeable\Observers\ModelObserver; |
8
|
|
|
use Turahe\Likeable\Contracts\Like as LikeContract; |
9
|
|
|
use Turahe\Likeable\Contracts\LikeCounter as LikeCounterContract; |
10
|
|
|
use Turahe\Likeable\Contracts\LikeableService as LikeableServiceContract; |
11
|
|
|
|
12
|
|
|
trait Likeable |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Boot the Likeable trait for a model. |
16
|
|
|
* |
17
|
|
|
* @return void |
18
|
|
|
*/ |
19
|
|
|
public static function bootLikeable() |
20
|
|
|
{ |
21
|
|
|
static::observe(ModelObserver::class); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Collection of likes and dislikes on this record. |
26
|
|
|
* |
27
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
28
|
|
|
*/ |
29
|
|
|
public function likesAndDislikes() |
30
|
|
|
{ |
31
|
|
|
return $this->morphMany(app(LikeContract::class), 'likeable'); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Collection of likes on this record. |
36
|
|
|
* |
37
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
38
|
|
|
*/ |
39
|
|
|
public function likes() |
40
|
|
|
{ |
41
|
|
|
return $this->likesAndDislikes()->where('type_id', LikeType::LIKE); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Collection of dislikes on this record. |
46
|
|
|
* |
47
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
48
|
|
|
*/ |
49
|
|
|
public function dislikes() |
50
|
|
|
{ |
51
|
|
|
return $this->likesAndDislikes()->where('type_id', LikeType::DISLIKE); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Counter is a record that stores the total likes for the morphed record. |
56
|
|
|
* |
57
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphOne |
58
|
|
|
*/ |
59
|
|
|
public function likesCounter() |
60
|
|
|
{ |
61
|
|
|
return $this->morphOne(app(LikeCounterContract::class), 'likeable') |
|
|
|
|
62
|
|
|
->where('type_id', LikeType::LIKE); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Counter is a record that stores the total dislikes for the morphed record. |
67
|
|
|
* |
68
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphOne |
69
|
|
|
*/ |
70
|
|
|
public function dislikesCounter() |
71
|
|
|
{ |
72
|
|
|
return $this->morphOne(app(LikeCounterContract::class), 'likeable') |
73
|
|
|
->where('type_id', LikeType::DISLIKE); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Fetch users who liked entity. |
78
|
|
|
* |
79
|
|
|
* @return \Illuminate\Support\Collection |
80
|
|
|
*/ |
81
|
|
|
public function collectLikers() |
82
|
|
|
{ |
83
|
|
|
return app(LikeableServiceContract::class)->collectLikersOf($this); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Fetch users who disliked entity. |
88
|
|
|
* |
89
|
|
|
* @return \Illuminate\Support\Collection |
90
|
|
|
*/ |
91
|
|
|
public function collectDislikers() |
92
|
|
|
{ |
93
|
|
|
return app(LikeableServiceContract::class)->collectDislikersOf($this); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Model likesCount attribute. |
98
|
|
|
* |
99
|
|
|
* @return int |
100
|
|
|
*/ |
101
|
|
|
public function getLikesCountAttribute() |
102
|
|
|
{ |
103
|
|
|
return $this->likesCounter ? $this->likesCounter->count : 0; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Model dislikesCount attribute. |
108
|
|
|
* |
109
|
|
|
* @return int |
110
|
|
|
*/ |
111
|
|
|
public function getDislikesCountAttribute() |
112
|
|
|
{ |
113
|
|
|
return $this->dislikesCounter ? $this->dislikesCounter->count : 0; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Did the currently logged in user like this model. |
118
|
|
|
* |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
public function getLikedAttribute() |
122
|
|
|
{ |
123
|
|
|
return $this->liked(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Did the currently logged in user dislike this model. |
128
|
|
|
* |
129
|
|
|
* @return bool |
130
|
|
|
*/ |
131
|
|
|
public function getDislikedAttribute() |
132
|
|
|
{ |
133
|
|
|
return $this->disliked(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Difference between likes and dislikes count. |
138
|
|
|
* |
139
|
|
|
* @return int |
140
|
|
|
*/ |
141
|
|
|
public function getLikesDiffDislikesCountAttribute() |
142
|
|
|
{ |
143
|
|
|
return $this->likesCount - $this->dislikesCount; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Fetch records that are liked by a given user id. |
148
|
|
|
* |
149
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
150
|
|
|
* @param int|null $userId |
151
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
152
|
|
|
* |
153
|
|
|
* @throws \Turahe\Likeable\Exceptions\LikerNotDefinedException |
154
|
|
|
*/ |
155
|
|
|
public function scopeWhereLikedBy(Builder $query, $userId = null) |
156
|
|
|
{ |
157
|
|
|
return app(LikeableServiceContract::class) |
158
|
|
|
->scopeWhereLikedBy($query, LikeType::LIKE, $userId); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Fetch records that are disliked by a given user id. |
163
|
|
|
* |
164
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
165
|
|
|
* @param int|null $userId |
166
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
167
|
|
|
* |
168
|
|
|
* @throws \Turahe\Likeable\Exceptions\LikerNotDefinedException |
169
|
|
|
*/ |
170
|
|
|
public function scopeWhereDislikedBy(Builder $query, $userId = null) |
171
|
|
|
{ |
172
|
|
|
return app(LikeableServiceContract::class) |
173
|
|
|
->scopeWhereLikedBy($query, LikeType::DISLIKE, $userId); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Fetch records sorted by likes count. |
178
|
|
|
* |
179
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
180
|
|
|
* @param string $direction |
181
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
182
|
|
|
*/ |
183
|
|
|
public function scopeOrderByLikesCount(Builder $query, $direction = 'desc') |
184
|
|
|
{ |
185
|
|
|
return app(LikeableServiceContract::class) |
186
|
|
|
->scopeOrderByLikesCount($query, LikeType::LIKE, $direction); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Fetch records sorted by likes count. |
191
|
|
|
* |
192
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
193
|
|
|
* @param string $direction |
194
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
195
|
|
|
*/ |
196
|
|
|
public function scopeOrderByDislikesCount(Builder $query, $direction = 'desc') |
197
|
|
|
{ |
198
|
|
|
return app(LikeableServiceContract::class) |
199
|
|
|
->scopeOrderByLikesCount($query, LikeType::DISLIKE, $direction); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Add a like for model by the given user. |
204
|
|
|
* |
205
|
|
|
* @param mixed $userId If null will use currently logged in user. |
206
|
|
|
* @return void |
207
|
|
|
* |
208
|
|
|
* @throws \Turahe\Likeable\Exceptions\LikerNotDefinedException |
209
|
|
|
*/ |
210
|
|
|
public function like($userId = null) |
211
|
|
|
{ |
212
|
|
|
app(LikeableServiceContract::class)->addLikeTo($this, LikeType::LIKE, $userId); |
|
|
|
|
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Remove a like from this record for the given user. |
217
|
|
|
* |
218
|
|
|
* @param int|null $userId If null will use currently logged in user. |
219
|
|
|
* @return void |
220
|
|
|
* |
221
|
|
|
* @throws \Turahe\Likeable\Exceptions\LikerNotDefinedException |
222
|
|
|
*/ |
223
|
|
|
public function unlike($userId = null) |
224
|
|
|
{ |
225
|
|
|
app(LikeableServiceContract::class)->removeLikeFrom($this, LikeType::LIKE, $userId); |
|
|
|
|
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Toggle like for model by the given user. |
230
|
|
|
* |
231
|
|
|
* @param mixed $userId If null will use currently logged in user. |
232
|
|
|
* @return void |
233
|
|
|
* |
234
|
|
|
* @throws \Turahe\Likeable\Exceptions\LikerNotDefinedException |
235
|
|
|
*/ |
236
|
|
|
public function likeToggle($userId = null) |
237
|
|
|
{ |
238
|
|
|
app(LikeableServiceContract::class)->toggleLikeOf($this, LikeType::LIKE, $userId); |
|
|
|
|
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Has the user already liked likeable model. |
243
|
|
|
* |
244
|
|
|
* @param int|null $userId |
245
|
|
|
* @return bool |
246
|
|
|
*/ |
247
|
|
|
public function liked($userId = null) |
248
|
|
|
{ |
249
|
|
|
return app(LikeableServiceContract::class)->isLiked($this, LikeType::LIKE, $userId); |
|
|
|
|
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Delete likes related to the current record. |
254
|
|
|
* |
255
|
|
|
* @return void |
256
|
|
|
*/ |
257
|
|
|
public function removeLikes() |
258
|
|
|
{ |
259
|
|
|
app(LikeableServiceContract::class)->removeModelLikes($this, LikeType::LIKE); |
|
|
|
|
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Add a dislike for model by the given user. |
264
|
|
|
* |
265
|
|
|
* @param mixed $userId If null will use currently logged in user. |
266
|
|
|
* @return void |
267
|
|
|
* |
268
|
|
|
* @throws \Turahe\Likeable\Exceptions\LikerNotDefinedException |
269
|
|
|
*/ |
270
|
|
|
public function dislike($userId = null) |
271
|
|
|
{ |
272
|
|
|
app(LikeableServiceContract::class)->addLikeTo($this, LikeType::DISLIKE, $userId); |
|
|
|
|
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Remove a dislike from this record for the given user. |
277
|
|
|
* |
278
|
|
|
* @param int|null $userId If null will use currently logged in user. |
279
|
|
|
* @return void |
280
|
|
|
* |
281
|
|
|
* @throws \Turahe\Likeable\Exceptions\LikerNotDefinedException |
282
|
|
|
*/ |
283
|
|
|
public function undislike($userId = null) |
284
|
|
|
{ |
285
|
|
|
app(LikeableServiceContract::class)->removeLikeFrom($this, LikeType::DISLIKE, $userId); |
|
|
|
|
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Toggle dislike for model by the given user. |
290
|
|
|
* |
291
|
|
|
* @param mixed $userId If null will use currently logged in user. |
292
|
|
|
* @return void |
293
|
|
|
* |
294
|
|
|
* @throws \Turahe\Likeable\Exceptions\LikerNotDefinedException |
295
|
|
|
*/ |
296
|
|
|
public function dislikeToggle($userId = null) |
297
|
|
|
{ |
298
|
|
|
app(LikeableServiceContract::class)->toggleLikeOf($this, LikeType::DISLIKE, $userId); |
|
|
|
|
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Has the user already disliked likeable model. |
303
|
|
|
* |
304
|
|
|
* @param int|null $userId |
305
|
|
|
* @return bool |
306
|
|
|
*/ |
307
|
|
|
public function disliked($userId = null) |
308
|
|
|
{ |
309
|
|
|
return app(LikeableServiceContract::class)->isLiked($this, LikeType::DISLIKE, $userId); |
|
|
|
|
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Delete dislikes related to the current record. |
314
|
|
|
* |
315
|
|
|
* @return void |
316
|
|
|
*/ |
317
|
|
|
public function removeDislikes() |
318
|
|
|
{ |
319
|
|
|
app(LikeableServiceContract::class)->removeModelLikes($this, LikeType::DISLIKE); |
|
|
|
|
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|