|
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
|
|
|
trait CanFollow |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Follow a followable entity. |
|
18
|
|
|
* |
|
19
|
|
|
* @param int|array|\Illuminate\Database\Eloquent\Model $followable |
|
20
|
|
|
* @param string $className |
|
21
|
|
|
* |
|
22
|
|
|
* @return array |
|
23
|
|
|
*/ |
|
24
|
7 |
|
public function follow($followable, $className = __CLASS__) |
|
25
|
|
|
{ |
|
26
|
7 |
|
return $this->following($className)->sync($followable, false); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Unfollow a followable entity. |
|
31
|
|
|
* |
|
32
|
|
|
* @param int|array|\Illuminate\Database\Eloquent\Model $followable |
|
33
|
|
|
* @param string $className |
|
34
|
|
|
* |
|
35
|
|
|
* @return bool|null |
|
36
|
|
|
*/ |
|
37
|
|
|
public function unfollow($followable, $className = __CLASS__) |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->following($className)->detach($followable); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Check if entity is following given entity. |
|
44
|
|
|
* |
|
45
|
|
|
* @param int|\Illuminate\Database\Eloquent\Model $followable |
|
46
|
|
|
* @param string $className |
|
47
|
|
|
* |
|
48
|
|
|
* @return bool |
|
49
|
|
|
*/ |
|
50
|
|
|
public function isFollowing($followable, $className = __CLASS__) |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->following($className)->get()->contains($followable); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Toggle follow a followable or followables. |
|
57
|
|
|
* |
|
58
|
|
|
* @param int|array|\Illuminate\Database\Eloquent\Model $followable |
|
59
|
|
|
* @param string $className |
|
60
|
|
|
* |
|
61
|
|
|
* @return array |
|
62
|
|
|
*/ |
|
63
|
1 |
|
public function toggleFollow($followable, $className = __CLASS__) |
|
64
|
|
|
{ |
|
65
|
1 |
|
return $this->following($className)->toggle($followable); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Return entity following. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $className |
|
72
|
|
|
* |
|
73
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany |
|
74
|
|
|
*/ |
|
75
|
8 |
|
public function following($className = __CLASS__) |
|
76
|
|
|
{ |
|
77
|
8 |
|
return $this->morphedByMany($className, config('rally.followable_prefix'), config('rally.followers_table'), config('rally.follower_prefix').'_id', config('rally.followable_prefix').'_id')->withTimestamps(); |
|
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.