|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* _ __ __ _____ _____ ___ ____ _____ |
|
5
|
|
|
* | | / // // ___//_ _// || __||_ _| |
|
6
|
|
|
* | |/ // /(__ ) / / / /| || | | | |
|
7
|
|
|
* |___//_//____/ /_/ /_/ |_||_| |_| |
|
8
|
|
|
* @link https://vistart.name/ |
|
9
|
|
|
* @copyright Copyright (c) 2016 vistart |
|
10
|
|
|
* @license https://vistart.name/license/ |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace vistart\Models\traits; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* This trait should be used in models extended from models used BlameableTrait. |
|
17
|
|
|
* Notice: The models used BlameableTrait are also models used EntityTrait. |
|
18
|
|
|
* |
|
19
|
|
|
* @author vistart <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
trait MessageTrait |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
public $recipientGuidAttribute = 'other_guid'; |
|
25
|
|
|
public $attachmentAttribute = 'attachment'; |
|
26
|
|
|
public $expiration = 604800; // 7 days |
|
27
|
|
|
public $receivedAtAttribute = 'received_at'; |
|
28
|
|
|
public $readAtAttribute = 'read_at'; |
|
29
|
|
|
public static $eventMessageReceived = 'messageReceived'; |
|
30
|
|
|
public static $eventMessageRead = 'messageRead'; |
|
31
|
|
|
|
|
32
|
|
|
public function hasBeenReceived() |
|
33
|
|
|
{ |
|
34
|
|
|
return is_string($this->receivedAtAttribute) ? $this->isInitDatetime($this->receivedAtAttribute) : false; |
|
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function hasBeenRead() |
|
38
|
|
|
{ |
|
39
|
|
|
return is_string($this->readAtAttribute) ? $this->isInitDatetim($this->readAtAttribute) : false; |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function touchReceived($event) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->setReceivedAt(static::getCurrentDatetime($event)); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function touchRead($event) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->setReadAt(static::getCurrentDatetime($event)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function getReceivedAt() |
|
53
|
|
|
{ |
|
54
|
|
|
if (is_string($this->receivedAtAttribute)) { |
|
55
|
|
|
$raAttribute = $this->receivedAtAttribute; |
|
56
|
|
|
return $this->$raAttribute; |
|
57
|
|
|
} |
|
58
|
|
|
return null; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
public function setReceivedAt($receivedAt) |
|
62
|
|
|
{ |
|
63
|
1 |
|
if (is_string($this->receivedAtAttribute)) { |
|
64
|
1 |
|
$raAttribute = $this->receivedAtAttribute; |
|
65
|
1 |
|
return $this->$raAttribute = $receivedAt; |
|
66
|
|
|
} |
|
67
|
|
|
return null; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function getReadAt() |
|
71
|
|
|
{ |
|
72
|
|
|
if (is_string($this->readAtAttribute)) { |
|
73
|
|
|
$raAttribute = $this->readAtAttribute; |
|
74
|
|
|
return $this->$raAttribute; |
|
75
|
|
|
} |
|
76
|
|
|
return null; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
public function setReadAt($readAt) |
|
80
|
|
|
{ |
|
81
|
1 |
|
if (is_string($this->readAtAttribute)) { |
|
82
|
1 |
|
$raAttribute = $this->readAtAttribute; |
|
83
|
1 |
|
return $this->$raAttribute = $readAt; |
|
84
|
|
|
} |
|
85
|
|
|
return null; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param \yii\base\ModelEvent $event |
|
90
|
|
|
*/ |
|
91
|
1 |
|
public function onInitReceivedAtAttribute($event) |
|
92
|
|
|
{ |
|
93
|
1 |
|
$sender = $event->sender; |
|
94
|
|
|
/* @var $sender static */ |
|
95
|
1 |
|
$sender->setReceivedAt(static::getInitDatetime($event)); |
|
96
|
1 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param \yii\base\ModelEvent $event |
|
100
|
|
|
*/ |
|
101
|
1 |
|
public function onInitReadAtAttribute($event) |
|
102
|
|
|
{ |
|
103
|
1 |
|
$sender = $event->sender; |
|
104
|
|
|
/* @var $sender static */ |
|
105
|
1 |
|
$sender->setReadAt(static::getInitDatetime($event)); |
|
106
|
1 |
|
} |
|
107
|
|
|
|
|
108
|
1 |
|
public function initMessageEvents() |
|
109
|
|
|
{ |
|
110
|
1 |
|
$this->on(static::EVENT_BEFORE_INSERT, [$this, 'onInitReceivedAtAttribute']); |
|
|
|
|
|
|
111
|
1 |
|
$this->on(static::EVENT_BEFORE_INSERT, [$this, 'onInitReadAtAttribute']); |
|
|
|
|
|
|
112
|
1 |
|
} |
|
113
|
|
|
|
|
114
|
1 |
|
public function getMessageRules() |
|
115
|
|
|
{ |
|
116
|
|
|
$rules = [ |
|
117
|
1 |
|
[$this->recipientGuidAttribute, 'required'], |
|
118
|
1 |
|
[$this->recipientGuidAttribute, 'string', 'max' => 36], |
|
119
|
1 |
|
]; |
|
120
|
1 |
|
if (is_string($this->attachmentAttribute)) { |
|
121
|
1 |
|
$rules[] = [$this->attachmentAttribute, 'safe']; |
|
122
|
1 |
|
} |
|
123
|
1 |
|
if (is_string($this->receivedAtAttribute)) { |
|
124
|
1 |
|
$rules[] = [$this->receivedAtAttribute, 'safe']; |
|
125
|
1 |
|
} |
|
126
|
1 |
|
if (is_string($this->readAtAttribute)) { |
|
127
|
1 |
|
$rules[] = [$this->readAtAttribute, 'safe']; |
|
128
|
1 |
|
} |
|
129
|
1 |
|
return $rules; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
1 |
|
public function rules() |
|
133
|
|
|
{ |
|
134
|
1 |
|
return array_merge(parent::rules(), $this->getMessageRules()); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
1 |
|
public function enabledFields() |
|
138
|
|
|
{ |
|
139
|
1 |
|
$fields = parent::enabledFields(); |
|
140
|
1 |
|
$fields[] = $this->recipientGuidAttribute; |
|
141
|
1 |
|
if (is_string($this->attachmentAttribute)) { |
|
142
|
1 |
|
$fields[] = $this->attachmentAttribute; |
|
143
|
1 |
|
} |
|
144
|
1 |
|
if (is_string($this->receivedAtAttribute)) { |
|
145
|
1 |
|
$fields[] = $this->receivedAtAttribute; |
|
146
|
1 |
|
} |
|
147
|
1 |
|
if (is_string($this->readAtAttribute)) { |
|
148
|
1 |
|
$fields[] = $this->readAtAttribute; |
|
149
|
1 |
|
} |
|
150
|
1 |
|
return $fields; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
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.