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 yii\behaviors\TimestampBehavior; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Entity features concerning timestamp. |
19
|
|
|
* @property-read array $timestampBehaviors |
20
|
|
|
* @property-read array $createdAtRules |
21
|
|
|
* @property-read array $updatedAtRules |
22
|
|
|
* @version 2.0 |
23
|
|
|
* @author vistart <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
trait TimestampTrait |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string the attribute that will receive datetime value |
30
|
|
|
* Set this property to false if you do not want to record the creation time. |
31
|
|
|
*/ |
32
|
|
|
public $createdAtAttribute = 'create_time'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string the attribute that will receive datetime value. |
36
|
|
|
* Set this property to false if you do not want to record the update time. |
37
|
|
|
*/ |
38
|
|
|
public $updatedAtAttribute = 'update_time'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var integer Determine the format of timestamp. |
42
|
|
|
*/ |
43
|
|
|
public $timeFormat = 0; |
44
|
|
|
public static $timeFormatDatetime = 0; |
45
|
|
|
public static $timeFormatTimestamp = 1; |
46
|
|
|
public static $initDatetime = '1970-01-01 00:00:00'; |
47
|
|
|
public static $initTimestamp = 0; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get the current date & time in format of "Y-m-d H:i:s" or timestamp. |
51
|
|
|
* You can override this method to customize the return value. |
52
|
|
|
* @param \yii\base\ModelEvent $event |
53
|
|
|
* @return string Date & Time. |
54
|
|
|
* @since 1.1 |
55
|
|
|
*/ |
56
|
41 |
|
public static function getCurrentDatetime($event) |
57
|
|
|
{ |
58
|
41 |
|
$sender = $event->sender; |
59
|
41 |
|
return $sender->currentDatetime(); |
60
|
|
|
} |
61
|
|
|
|
62
|
41 |
|
public function currentDatetime() |
63
|
|
|
{ |
64
|
41 |
|
if ($this->timeFormat === self::$timeFormatDatetime) { |
65
|
41 |
|
return date('Y-m-d H:i:s'); |
66
|
|
|
} |
67
|
|
|
if ($this->timeFormat === self::$timeFormatTimestamp) { |
68
|
|
|
return time(); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get init date & time in format of "Y-m-d H:i:s" or timestamp.s |
74
|
|
|
* @param \yii\base\ModelEvent $event |
75
|
|
|
* @return string|int |
76
|
|
|
*/ |
77
|
3 |
|
public static function getInitDatetime($event) |
78
|
|
|
{ |
79
|
3 |
|
$sender = $event->sender; |
80
|
3 |
|
return $sender->initDatetime(); |
81
|
|
|
} |
82
|
|
|
|
83
|
3 |
|
public function initDatetime() |
84
|
|
|
{ |
85
|
3 |
|
if ($this->timeFormat === self::$timeFormatDatetime) { |
86
|
3 |
|
return static::$initDatetime; |
87
|
|
|
} |
88
|
|
|
if ($this->timeFormat === self::$timeFormatTimestamp) { |
89
|
|
|
return static::$initTimestamp; |
90
|
|
|
} |
91
|
|
|
return null; |
92
|
|
|
} |
93
|
|
|
|
94
|
2 |
|
protected function isInitDatetime($attribute) |
95
|
|
|
{ |
96
|
2 |
|
if ($this->timeFormat === self::$timeFormatDatetime) { |
97
|
2 |
|
return $this->$attribute == static::$initDatetime || $this->$attribute == null; |
98
|
|
|
} |
99
|
|
|
if ($this->timeFormat === self::$timeFormatTimestamp) { |
100
|
|
|
return $this->$attribute == static::$initTimestamp || $this->$attribute == null; |
101
|
|
|
} |
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get the current date & time in format of "Y-m-d H:i:s". |
107
|
|
|
* This method is ONLY used for being triggered by event. DO NOT call, |
108
|
|
|
* override or modify it directly, unless you know the consequences. |
109
|
|
|
* @param \yii\base\Event $event |
110
|
|
|
* @return string Date & Time. |
111
|
|
|
* @since 1.1 |
112
|
|
|
*/ |
113
|
41 |
|
public function onUpdateCurrentDatetime($event) |
114
|
|
|
{ |
115
|
41 |
|
return self::getCurrentDatetime($event); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Behaviors associated with timestamp. |
120
|
|
|
* @return array behaviors |
121
|
|
|
*/ |
122
|
11 |
|
public function getTimestampBehaviors() |
123
|
|
|
{ |
124
|
|
|
return [ |
125
|
|
|
[ |
126
|
11 |
|
'class' => TimestampBehavior::className(), |
127
|
11 |
|
'createdAtAttribute' => $this->createdAtAttribute, |
128
|
11 |
|
'updatedAtAttribute' => $this->updatedAtAttribute, |
129
|
11 |
|
'value' => [$this, 'onUpdateCurrentDatetime'], |
130
|
|
|
] |
131
|
11 |
|
]; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get createdAtAttribute. |
136
|
|
|
* @return string timestamp |
137
|
|
|
*/ |
138
|
|
|
public function getCreatedAt() |
139
|
|
|
{ |
140
|
|
|
$createdAtAttribute = $this->createdAtAttribute; |
141
|
|
|
return $this->$createdAtAttribute; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get rules associated with createdAtAttribute. |
146
|
|
|
* @return array rules |
147
|
|
|
*/ |
148
|
11 |
|
public function getCreatedAtRules() |
149
|
|
|
{ |
150
|
11 |
|
if (!$this->createdAtAttribute) { |
151
|
|
|
return []; |
152
|
|
|
} |
153
|
|
|
return [ |
154
|
11 |
|
[[$this->createdAtAttribute], 'safe'], |
155
|
11 |
|
]; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get rules associated with updatedAtAttribute. |
160
|
|
|
* @return array rules |
161
|
|
|
*/ |
162
|
11 |
|
public function getUpdatedAtRules() |
163
|
|
|
{ |
164
|
11 |
|
if (!$this->updatedAtAttribute) { |
165
|
1 |
|
return []; |
166
|
|
|
} |
167
|
|
|
return [ |
168
|
10 |
|
[[$this->updatedAtAttribute], 'safe'], |
169
|
10 |
|
]; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.