1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of emanci/laravel-rally package. |
5
|
|
|
* |
6
|
|
|
* (c) emanci <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Emanci\Rally\Traits; |
13
|
|
|
|
14
|
|
|
use Illuminate\Database\Eloquent\Model; |
15
|
|
|
|
16
|
|
|
trait CanFollow |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Follow a followable entity. |
20
|
|
|
* |
21
|
|
|
* @param int|array|\Illuminate\Database\Eloquent\Model $followable |
22
|
|
|
* @param string $className |
23
|
|
|
* |
24
|
|
|
* @return array |
25
|
|
|
*/ |
26
|
7 |
|
public function follow($followable, $className = __CLASS__) |
27
|
|
|
{ |
28
|
7 |
|
$followable = $this->parseFollowable($followable); |
29
|
|
|
|
30
|
7 |
|
return $this->following($className)->sync($followable, false); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Unfollow a followable entity. |
35
|
|
|
* |
36
|
|
|
* @param int|array|\Illuminate\Database\Eloquent\Model $followable |
37
|
|
|
* @param string $className |
38
|
|
|
* |
39
|
|
|
* @return bool|null |
40
|
|
|
*/ |
41
|
|
|
public function unfollow($followable, $className = __CLASS__) |
42
|
|
|
{ |
43
|
|
|
$followable = $this->parseFollowable($followable); |
44
|
|
|
|
45
|
|
|
return $this->following($className)->detach($followable); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Check if entity is following given entity. |
50
|
|
|
* |
51
|
|
|
* @param int|\Illuminate\Database\Eloquent\Model $followable |
52
|
|
|
* @param string $className |
53
|
|
|
* |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
public function isFollowing($followable, $className = __CLASS__) |
57
|
|
|
{ |
58
|
|
|
return $this->following($className)->get()->contains($followable); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Toggle follow a followable or followables. |
63
|
|
|
* |
64
|
|
|
* @param int|array|\Illuminate\Database\Eloquent\Model $followable |
65
|
|
|
* @param string $className |
66
|
|
|
* |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
1 |
|
public function toggleFollow($followable, $className = __CLASS__) |
70
|
|
|
{ |
71
|
1 |
|
$followable = $this->parseFollowable($followable); |
72
|
|
|
|
73
|
1 |
|
return $this->following($className)->toggle($followable); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Return entity following. |
78
|
|
|
* |
79
|
|
|
* @param string $className |
80
|
|
|
* |
81
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany |
82
|
|
|
*/ |
83
|
8 |
|
public function following($className = __CLASS__) |
84
|
|
|
{ |
85
|
8 |
|
return $this->morphedByMany($className, config('rally.followable_prefix'), config('rally.followers_table'), config('rally.follower_prefix').'_id', config('rally.followable_prefix').'_id')->withTimestamps(); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param int|array|\Illuminate\Database\Eloquent\Model $followable |
90
|
|
|
* |
91
|
|
|
* @return int|array |
92
|
|
|
*/ |
93
|
8 |
|
protected function parseFollowable($followable) |
94
|
|
|
{ |
95
|
8 |
|
if ($followable instanceof Model) { |
96
|
1 |
|
return $followable->getKey(); |
97
|
|
|
} |
98
|
|
|
|
99
|
7 |
|
return $followable; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.