1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Libraries\Likeable; |
4
|
|
|
|
5
|
|
|
use App\Libraries\Likeable\LikeCounter; |
6
|
|
|
use Conner\Likeable\LikeableTrait; |
7
|
|
|
|
8
|
|
|
trait Likeable |
9
|
|
|
{ |
10
|
|
|
use LikeableTrait; |
11
|
|
|
|
12
|
|
|
public function like($type = 'like', $userId = null) |
13
|
|
|
{ |
14
|
|
|
if(is_null($userId)) { |
15
|
|
|
$userId = $this->loggedInUserId(); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
if($userId) { |
19
|
|
|
$like = $this->likes() |
20
|
|
|
->where('user_id', '=', $userId) |
21
|
|
|
->typelike($type) |
22
|
|
|
->first(); |
23
|
|
|
|
24
|
|
|
if($like) return; |
25
|
|
|
|
26
|
|
|
$like = new Like(); |
27
|
|
|
$like->user_id = $userId; |
|
|
|
|
28
|
|
|
$like->type = $type; |
|
|
|
|
29
|
|
|
$this->likes() |
30
|
|
|
->typelike($type) |
31
|
|
|
->save($like); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$this->incrementLikeCount($type); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function unlike($type = 'like', $userId = null) |
38
|
|
|
{ |
39
|
|
|
if(is_null($userId)) { |
40
|
|
|
$userId = $this->loggedInUserId(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if($userId) { |
44
|
|
|
$like = $this->likes() |
45
|
|
|
->where('user_id', '=', $userId) |
46
|
|
|
->typelike($type) |
47
|
|
|
->first(); |
48
|
|
|
|
49
|
|
|
if(!$like) { return; } |
50
|
|
|
|
51
|
|
|
$like->delete(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->decrementLikeCount($type); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function wasLiked($type = 'like', $userId = null) { |
58
|
|
|
if(is_null($userId)) { |
59
|
|
|
$userId = $this->loggedInUserId(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return (bool) $this->likes() |
63
|
|
|
->where('user_id', '=', $userId) |
64
|
|
|
->typelike($type) |
65
|
|
|
->count(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function incrementLikeCount($type ='like') |
69
|
|
|
{ |
70
|
|
|
$counter = $this->likeCounter() |
71
|
|
|
->typelike($type) |
72
|
|
|
->first(); |
73
|
|
|
|
74
|
|
|
if($counter) { |
75
|
|
|
$counter->count++; |
76
|
|
|
$counter->save(); |
77
|
|
|
} else { |
78
|
|
|
$counter = new LikeCounter; |
79
|
|
|
$counter->type = $type; |
|
|
|
|
80
|
|
|
$counter->count = 1; |
|
|
|
|
81
|
|
|
$this->likeCounter() |
82
|
|
|
->typelike($type) |
83
|
|
|
->save($counter); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Private. Decrement the total like count stored in the counter |
89
|
|
|
*/ |
90
|
|
|
private function decrementLikeCount($type ='like') |
91
|
|
|
{ |
92
|
|
|
$counter = $this->likeCounter() |
93
|
|
|
->typelike($type) |
94
|
|
|
->first(); |
95
|
|
|
|
96
|
|
|
if($counter) { |
97
|
|
|
$counter->count--; |
98
|
|
|
if($counter->count) { |
99
|
|
|
$counter->type = $type; |
100
|
|
|
$counter->save(); |
101
|
|
|
} else { |
102
|
|
|
$counter->delete(); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function likes($type = 'like') |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
return $this->morphMany(Like::class, 'likable'); |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function getLikes($type = 'like') |
113
|
|
|
{ |
114
|
|
|
return $this->likes()->typelike($type)->get(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function likeCounter() |
118
|
|
|
{ |
119
|
|
|
return $this->morphOne(LikeCounter::class, 'likable'); |
|
|
|
|
120
|
|
|
} |
121
|
|
|
} |
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.