|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PHPHub\Repositories\Eloquent; |
|
4
|
|
|
|
|
5
|
|
|
use Auth; |
|
6
|
|
|
use PHPHub\Events\NewReply; |
|
7
|
|
|
use PHPHub\Repositories\Criteria\ReplyCriteria; |
|
8
|
|
|
use PHPHub\Repositories\Eloquent\Traits\IncludeUserTrait; |
|
9
|
|
|
use PHPHub\Repositories\ReplyRepositoryInterface; |
|
10
|
|
|
use PHPHub\Reply; |
|
11
|
|
|
use Prettus\Validator\Contracts\ValidatorInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class ReplyRepositoryEloquent. |
|
15
|
|
|
*/ |
|
16
|
|
|
class ReplyRepository extends BaseRepository implements ReplyRepositoryInterface |
|
17
|
|
|
{ |
|
18
|
|
|
use IncludeUserTrait; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Specify Validator Rules. |
|
22
|
|
|
* |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $rules = [ |
|
26
|
|
|
ValidatorInterface::RULE_CREATE => [ |
|
27
|
|
|
'body' => 'required|min:2', |
|
28
|
|
|
'topic_id' => 'required|integer', |
|
29
|
|
|
], |
|
30
|
|
|
ValidatorInterface::RULE_UPDATE => [ |
|
31
|
|
|
|
|
32
|
|
|
], |
|
33
|
|
|
]; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* 发布新的评论. |
|
37
|
|
|
* |
|
38
|
|
|
* @param array $attributes |
|
39
|
|
|
* |
|
40
|
|
|
* @return Reply |
|
41
|
|
|
*/ |
|
42
|
|
|
public function store(array $attributes) |
|
43
|
|
|
{ |
|
44
|
|
|
if (! is_null($this->validator)) { |
|
45
|
|
|
$this->validator->with($attributes) |
|
46
|
|
|
->passesOrFail(ValidatorInterface::RULE_CREATE); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$reply = new Reply($attributes); |
|
50
|
|
|
|
|
51
|
|
|
$reply->user_id = Auth::id(); |
|
|
|
|
|
|
52
|
|
|
$reply->body = app('markdown')->text($attributes['body']); |
|
|
|
|
|
|
53
|
|
|
$reply->body_original = $attributes['body']; |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
$reply->save(); |
|
56
|
|
|
|
|
57
|
|
|
$reply->topic()->getQuery()->increment('reply_count'); |
|
58
|
|
|
|
|
59
|
|
|
event(new NewReply($reply, Auth::id(), $reply->topic()->getQuery()->pluck('user_id'))); |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
return $reply; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Specify Model class name. |
|
66
|
|
|
* |
|
67
|
|
|
* @return string |
|
68
|
|
|
*/ |
|
69
|
|
|
public function model() |
|
70
|
|
|
{ |
|
71
|
|
|
return Reply::class; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Boot up the repository, pushing criteria. |
|
76
|
|
|
*/ |
|
77
|
|
|
public function boot() |
|
78
|
|
|
{ |
|
79
|
|
|
$this->pushCriteria(app(ReplyCriteria::class)); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* 通过 TopicId 过滤. |
|
84
|
|
|
* |
|
85
|
|
|
* @param $topic_id |
|
86
|
|
|
* |
|
87
|
|
|
* @return $this |
|
88
|
|
|
*/ |
|
89
|
|
|
public function byTopicId($topic_id) |
|
90
|
|
|
{ |
|
91
|
|
|
$this->model = $this->model->where('topic_id', $topic_id); |
|
92
|
|
|
|
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* 通过 UserId 过滤. |
|
98
|
|
|
* |
|
99
|
|
|
* @param $user_id |
|
100
|
|
|
* |
|
101
|
|
|
* @return $this |
|
102
|
|
|
*/ |
|
103
|
|
|
public function byUserId($user_id) |
|
104
|
|
|
{ |
|
105
|
|
|
$this->model = $this->model->where('user_id', $user_id); |
|
106
|
|
|
|
|
107
|
|
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
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@propertyannotation 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.