1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Created by PhpStorm. |
5
|
|
|
* User: xuan |
6
|
|
|
* Date: 10/17/15 |
7
|
|
|
* Time: 4:36 PM. |
8
|
|
|
*/ |
9
|
|
|
namespace PHPHub\Events; |
10
|
|
|
|
11
|
|
|
use PHPHub\Reply; |
12
|
|
|
use PHPHub\Topic; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class ShouldNotifyTrait. |
16
|
|
|
* |
17
|
|
|
* @method getBody |
18
|
|
|
* @method getUserId |
19
|
|
|
* @method getTopicId |
20
|
|
|
* @method getFromUserId |
21
|
|
|
* @method getType |
22
|
|
|
*/ |
23
|
|
|
trait ShouldNotifyTrait |
24
|
|
|
{ |
25
|
|
|
protected $body = null; |
26
|
|
|
protected $topic_id = null; |
27
|
|
|
protected $reply_id = 0; |
28
|
|
|
protected $user_id = null; |
29
|
|
|
protected $from_user_id = null; |
30
|
|
|
protected $type = null; |
31
|
|
|
|
32
|
|
|
public function getReplyId() |
33
|
|
|
{ |
34
|
|
|
return $this->reply_id ?: 0; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function __call($method, $args) |
38
|
|
|
{ |
39
|
|
|
$property = snake_case(str_replace('get', '', $method)); |
40
|
|
|
if (! isset($this->$property) || $this->$property === null) { |
41
|
|
|
throw new \Exception('请在 Event 中设置 '.$property); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $this->$property; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* 设置 Notification Type. |
49
|
|
|
* |
50
|
|
|
* @param $type |
51
|
|
|
*/ |
52
|
|
|
public function setNotificationType($type) |
53
|
|
|
{ |
54
|
|
|
$this->type = $type; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* 从 Topic 对象初始化通知信息. |
59
|
|
|
* |
60
|
|
|
* @param Topic $topic |
61
|
|
|
* @param $from_user_id |
62
|
|
|
*/ |
63
|
|
|
public function setNotificationInfoFromTopic(Topic $topic, $from_user_id) |
64
|
|
|
{ |
65
|
|
|
$this->body = $topic->body; |
|
|
|
|
66
|
|
|
$this->topic_id = $topic->id; |
|
|
|
|
67
|
|
|
$this->user_id = $topic->user_id; |
|
|
|
|
68
|
|
|
$this->from_user_id = $from_user_id; |
69
|
|
|
|
70
|
|
|
if (isset($this->notification_type)) { |
71
|
|
|
$this->type = $this->type ?: $this->notification_type; |
|
|
|
|
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* 从 Reply 对象初始化通知信息. |
77
|
|
|
* |
78
|
|
|
* @param Reply $reply |
79
|
|
|
* @param $from_user_id |
80
|
|
|
* @param null $user_id |
81
|
|
|
*/ |
82
|
|
|
public function setNotificationInfoFromReply(Reply $reply, $from_user_id, $user_id = null) |
83
|
|
|
{ |
84
|
|
|
$this->body = $reply->body; |
|
|
|
|
85
|
|
|
$this->topic_id = $reply->topic_id; |
|
|
|
|
86
|
|
|
$this->reply_id = $reply->id; |
|
|
|
|
87
|
|
|
$this->user_id = $user_id ?: $reply->topic()->pluck('user_id'); |
88
|
|
|
$this->from_user_id = $from_user_id; |
89
|
|
|
|
90
|
|
|
if (isset($this->notification_type)) { |
91
|
|
|
$this->type = $this->type ?: $this->notification_type; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
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@property
annotation 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.