1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Laravel :package_name. |
5
|
|
|
* |
6
|
|
|
* (c) DraperStudio <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace DraperStudio\Commentable\Traits; |
13
|
|
|
|
14
|
|
|
use Illuminate\Database\Eloquent\Model; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Commentable. |
18
|
|
|
* |
19
|
|
|
* @author DraperStudio <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
trait Commentable |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
|
|
public function commentable_model() |
28
|
|
|
{ |
29
|
|
|
return config('commentable.model'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return mixed |
34
|
|
|
*/ |
35
|
|
|
public function comments() |
36
|
|
|
{ |
37
|
|
|
return $this->morphMany($this->commentable_model(), 'commentable'); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param $data |
42
|
|
|
* @param Model $creator |
43
|
|
|
* @param Model|null $parent |
44
|
|
|
* |
45
|
|
|
* @return static |
46
|
|
|
*/ |
47
|
|
|
public function comment($data, Model $creator, Model $parent = null) |
48
|
|
|
{ |
49
|
|
|
$commentableModel = $this->commentable_model(); |
50
|
|
|
|
51
|
|
|
$comment = (new $commentableModel)->createComment($this, $data, $creator); |
52
|
|
|
|
53
|
|
|
if (!empty($parent)) { |
54
|
|
|
$comment->appendTo($parent)->save(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $comment; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param $id |
62
|
|
|
* @param $data |
63
|
|
|
* @param Model|null $parent |
64
|
|
|
* |
65
|
|
|
* @return mixed |
66
|
|
|
*/ |
67
|
|
|
public function updateComment($id, $data, Model $parent = null) |
68
|
|
|
{ |
69
|
|
|
$commentableModel = $this->commentable_model(); |
70
|
|
|
|
71
|
|
|
$comment = (new $commentableModel)->updateComment($id, $data); |
72
|
|
|
|
73
|
|
|
if (!empty($parent)) { |
74
|
|
|
$comment->appendTo($parent)->save(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $comment; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param $id |
82
|
|
|
* |
83
|
|
|
* @return mixed |
84
|
|
|
*/ |
85
|
|
|
public function deleteComment($id) |
86
|
|
|
{ |
87
|
|
|
$commentableModel = $this->commentable_model(); |
88
|
|
|
|
89
|
|
|
return (new $commentableModel)->deleteComment($id); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return mixed |
94
|
|
|
*/ |
95
|
|
|
public function commentCount() |
96
|
|
|
{ |
97
|
|
|
return $this->comments->count(); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
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
Idable
provides a methodequalsId
that 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.