1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* _ __ __ _____ _____ ___ ____ _____ |
5
|
|
|
* | | / // // ___//_ _// || __||_ _| |
6
|
|
|
* | |/ // /(__ ) / / / /| || | | | |
7
|
|
|
* |___//_//____/ /_/ /_/ |_||_| |_| |
8
|
|
|
* @link http://vistart.name/ |
9
|
|
|
* @copyright Copyright (c) 2016 vistart |
10
|
|
|
* @license http://vistart.name/license/ |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace vistart\Models\traits; |
14
|
|
|
|
15
|
|
|
use Closure; |
16
|
|
|
use yii\behaviors\TimestampBehavior; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Entity features concerning timestamp. |
20
|
|
|
* @property-read array $timestampBehaviors |
21
|
|
|
* @property-read string|int createdAt |
22
|
|
|
* @property-read string|int updatedAt |
23
|
|
|
* @property-read array $createdAtRules |
24
|
|
|
* @property-read array $updatedAtRules |
25
|
|
|
* @property-read boolean isExpired |
26
|
|
|
* @version 2.0 |
27
|
|
|
* @author vistart <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
trait TimestampTrait |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string the attribute that will receive datetime value |
34
|
|
|
* Set this property to false if you do not want to record the creation time. |
35
|
|
|
*/ |
36
|
|
|
public $createdAtAttribute = 'create_time'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string the attribute that will receive datetime value. |
40
|
|
|
* Set this property to false if you do not want to record the update time. |
41
|
|
|
*/ |
42
|
|
|
public $updatedAtAttribute = 'update_time'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var integer Determine the format of timestamp. |
46
|
|
|
*/ |
47
|
|
|
public $timeFormat = 0; |
48
|
|
|
public static $timeFormatDatetime = 0; |
49
|
|
|
public static $timeFormatTimestamp = 1; |
50
|
|
|
public static $initDatetime = '1970-01-01 00:00:00'; |
51
|
|
|
public static $initTimestamp = 0; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var int|false the expiration in seconds, or false if it will not be expired. |
55
|
|
|
*/ |
56
|
|
|
public $expiredAt = false; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var Closure |
60
|
|
|
*/ |
61
|
|
|
public $expiredRemovingCallback; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Check this entity whether expired. |
65
|
|
|
* @return boolean |
66
|
|
|
*/ |
67
|
15 |
|
public function getIsExpired() |
68
|
|
|
{ |
69
|
15 |
|
if ($this->expiredAt === false) { |
70
|
9 |
|
return false; |
71
|
|
|
} |
72
|
6 |
|
return $this->offsetDatetime(-(int) $this->expiredAt) > $this->createdAt; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Remove myself if expired. |
77
|
|
|
* @return boolean |
78
|
|
|
*/ |
79
|
13 |
|
public function removeIfExpired() |
80
|
|
|
{ |
81
|
13 |
|
if ($this->getIsExpired()) { |
82
|
2 |
|
if ($this->expiredRemovingCallback instanceof Closure && is_callable($this->expiredRemovingCallback)) { |
83
|
|
|
return call_user_func($this->expiredRemovingCallback, $this); |
84
|
|
|
} |
85
|
2 |
|
return $this->delete(); |
|
|
|
|
86
|
|
|
} |
87
|
11 |
|
return false; |
88
|
|
|
} |
89
|
|
|
|
90
|
13 |
|
public function onRemoveExpired($event) |
91
|
|
|
{ |
92
|
13 |
|
$sender = $event->sender; |
93
|
13 |
|
return $sender->removeIfExpired(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get the current date & time in format of "Y-m-d H:i:s" or timestamp. |
98
|
|
|
* You can override this method to customize the return value. |
99
|
|
|
* @param \yii\base\ModelEvent $event |
100
|
|
|
* @return string Date & Time. |
101
|
|
|
* @since 1.1 |
102
|
|
|
*/ |
103
|
46 |
|
public static function getCurrentDatetime($event) |
104
|
|
|
{ |
105
|
46 |
|
$sender = $event->sender; |
106
|
46 |
|
return $sender->currentDatetime(); |
107
|
|
|
} |
108
|
|
|
|
109
|
46 |
|
public function currentDatetime() |
110
|
|
|
{ |
111
|
46 |
|
if ($this->timeFormat === self::$timeFormatDatetime) { |
112
|
46 |
|
return date('Y-m-d H:i:s'); |
113
|
|
|
} |
114
|
|
|
if ($this->timeFormat === self::$timeFormatTimestamp) { |
115
|
|
|
return time(); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
6 |
|
public function offsetDatetime($time = null, $offset = 0) |
120
|
|
|
{ |
121
|
6 |
|
if ($this->timeFormat === self::$timeFormatDatetime) { |
122
|
6 |
|
return date('Y-m-d H:i:s', strtotime(((int) $offset >= 0 ? "+$offset" : "-" . abs($offset)) . " seconds", is_string($time) ? strtotime($time) : time())); |
123
|
|
|
} |
124
|
|
|
if ($this->timeFormat === self::$timeFormatTimestamp) { |
125
|
|
|
return (is_int($time) ? $time : time()) + $offset; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get init date & time in format of "Y-m-d H:i:s" or timestamp.s |
131
|
|
|
* @param \yii\base\ModelEvent $event |
132
|
|
|
* @return string|int |
133
|
|
|
*/ |
134
|
8 |
|
public static function getInitDatetime($event) |
135
|
|
|
{ |
136
|
8 |
|
$sender = $event->sender; |
137
|
8 |
|
return $sender->initDatetime(); |
138
|
|
|
} |
139
|
|
|
|
140
|
8 |
|
public function initDatetime() |
141
|
|
|
{ |
142
|
8 |
|
if ($this->timeFormat === self::$timeFormatDatetime) { |
143
|
8 |
|
return static::$initDatetime; |
144
|
|
|
} |
145
|
|
|
if ($this->timeFormat === self::$timeFormatTimestamp) { |
146
|
|
|
return static::$initTimestamp; |
147
|
|
|
} |
148
|
|
|
return null; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Check whether the attribute is init datetime. |
153
|
|
|
* @param mixed $attribute |
154
|
|
|
* @return boolean |
155
|
|
|
*/ |
156
|
4 |
|
protected function isInitDatetime($attribute) |
157
|
|
|
{ |
158
|
4 |
|
if ($this->timeFormat === self::$timeFormatDatetime) { |
159
|
4 |
|
return $attribute == static::$initDatetime || $attribute == null; |
160
|
|
|
} |
161
|
|
|
if ($this->timeFormat === self::$timeFormatTimestamp) { |
162
|
|
|
return $attribute == static::$initTimestamp || $attribute == null; |
163
|
|
|
} |
164
|
|
|
return false; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get the current date & time in format of "Y-m-d H:i:s". |
169
|
|
|
* This method is ONLY used for being triggered by event. DO NOT call, |
170
|
|
|
* override or modify it directly, unless you know the consequences. |
171
|
|
|
* @param \yii\base\Event $event |
172
|
|
|
* @return string Date & Time. |
173
|
|
|
* @since 1.1 |
174
|
|
|
*/ |
175
|
46 |
|
public function onUpdateCurrentDatetime($event) |
176
|
|
|
{ |
177
|
46 |
|
return self::getCurrentDatetime($event); |
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Behaviors associated with timestamp. |
182
|
|
|
* @return array behaviors |
183
|
|
|
*/ |
184
|
12 |
|
public function getTimestampBehaviors() |
185
|
|
|
{ |
186
|
|
|
return [ |
187
|
|
|
[ |
188
|
12 |
|
'class' => TimestampBehavior::className(), |
189
|
12 |
|
'createdAtAttribute' => $this->createdAtAttribute, |
190
|
12 |
|
'updatedAtAttribute' => $this->updatedAtAttribute, |
191
|
12 |
|
'value' => [$this, 'onUpdateCurrentDatetime'], |
192
|
|
|
] |
193
|
12 |
|
]; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Get createdAt. |
198
|
|
|
* @return string timestamp |
199
|
|
|
*/ |
200
|
6 |
|
public function getCreatedAt() |
201
|
|
|
{ |
202
|
6 |
|
$createdAtAttribute = $this->createdAtAttribute; |
203
|
6 |
|
return $this->$createdAtAttribute; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Get rules associated with createdAtAttribute. |
208
|
|
|
* @return array rules |
209
|
|
|
*/ |
210
|
12 |
|
public function getCreatedAtRules() |
211
|
|
|
{ |
212
|
12 |
|
if (!$this->createdAtAttribute) { |
213
|
|
|
return []; |
214
|
|
|
} |
215
|
|
|
return [ |
216
|
12 |
|
[[$this->createdAtAttribute], 'safe'], |
217
|
12 |
|
]; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Get updatedAt. |
222
|
|
|
* @return string timestamp |
223
|
|
|
*/ |
224
|
|
|
public function getUpdatedAt() |
225
|
|
|
{ |
226
|
|
|
$updatedAtAttribute = $this->updatedAtAttribute; |
227
|
|
|
return $this->$updatedAtAttribute; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Get rules associated with updatedAtAttribute. |
232
|
|
|
* @return array rules |
233
|
|
|
*/ |
234
|
12 |
|
public function getUpdatedAtRules() |
235
|
|
|
{ |
236
|
12 |
|
if (!$this->updatedAtAttribute) { |
237
|
1 |
|
return []; |
238
|
|
|
} |
239
|
|
|
return [ |
240
|
11 |
|
[[$this->updatedAtAttribute], 'safe'], |
241
|
11 |
|
]; |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
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.