1 | <?php |
||
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__) |
|
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__) |
|
87 | |||
88 | /** |
||
89 | * @param int|array|\Illuminate\Database\Eloquent\Model $followable |
||
90 | * |
||
91 | * @return int|array |
||
92 | */ |
||
93 | 8 | protected function parseFollowable($followable) |
|
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.