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
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get the current date & time in format of "Y-m-d H:i:s" or timestamp. |
49
|
|
|
* You can override this method to customize the return value. |
50
|
|
|
* @param \yii\base\ModelEvent $event |
51
|
|
|
* @return string Date & Time. |
52
|
|
|
* @since 1.1 |
53
|
|
|
*/ |
54
|
39 |
|
public static function getCurrentDatetime($event) |
55
|
|
|
{ |
56
|
39 |
|
$sender = $event->sender; |
57
|
39 |
|
return $sender->currentDatetime(); |
58
|
|
|
} |
59
|
|
|
|
60
|
39 |
|
public function currentDatetime() |
61
|
|
|
{ |
62
|
39 |
|
if ($this->timeFormat === self::$timeFormatDatetime) { |
63
|
39 |
|
return date('Y-m-d H:i:s'); |
64
|
|
|
} |
65
|
|
|
if ($this->timeFormat === self::$timeFormatTimestamp) { |
66
|
|
|
return time(); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get init date & time in format of "Y-m-d H:i:s" or timestamp.s |
72
|
|
|
* @param \yii\base\ModelEvent $event |
73
|
|
|
* @return string|int |
74
|
|
|
*/ |
75
|
1 |
|
public static function getInitDatetime($event) |
76
|
|
|
{ |
77
|
1 |
|
$sender = $event->sender; |
78
|
1 |
|
return $sender->initDatetime(); |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
public function initDatetime() |
82
|
|
|
{ |
83
|
1 |
|
if ($this->timeFormat === self::$timeFormatDatetime) { |
84
|
1 |
|
return '1970-01-01 00:00:00'; |
85
|
|
|
} |
86
|
|
|
if ($this->timeFormat === self::$timeFormatTimestamp) { |
87
|
|
|
return 0; |
88
|
|
|
} |
89
|
|
|
return null; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
protected function isInitDatetime($attribute) |
93
|
|
|
{ |
94
|
|
|
if ($this->timeFormat === self::$timeFormatDatetime) { |
95
|
|
|
return $this->$attribute == '1970-01-01 00:00:00' || $this->$attribute == null; |
96
|
|
|
} |
97
|
|
|
if ($this->timeFormat === self::$timeFormatTimestamp) { |
98
|
|
|
return $this->$attribute == 0 || $this->$attribute == null; |
99
|
|
|
} |
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get the current date & time in format of "Y-m-d H:i:s". |
105
|
|
|
* This method is ONLY used for being triggered by event. DO NOT call, |
106
|
|
|
* override or modify it directly, unless you know the consequences. |
107
|
|
|
* @param \yii\base\Event $event |
108
|
|
|
* @return string Date & Time. |
109
|
|
|
* @since 1.1 |
110
|
|
|
*/ |
111
|
39 |
|
public function onUpdateCurrentDatetime($event) |
112
|
|
|
{ |
113
|
39 |
|
return self::getCurrentDatetime($event); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Behaviors associated with timestamp. |
118
|
|
|
* @return array behaviors |
119
|
|
|
*/ |
120
|
11 |
|
public function getTimestampBehaviors() |
121
|
|
|
{ |
122
|
|
|
return [ |
123
|
|
|
[ |
124
|
11 |
|
'class' => TimestampBehavior::className(), |
125
|
11 |
|
'createdAtAttribute' => $this->createdAtAttribute, |
126
|
11 |
|
'updatedAtAttribute' => $this->updatedAtAttribute, |
127
|
11 |
|
'value' => [$this, 'onUpdateCurrentDatetime'], |
128
|
|
|
] |
129
|
11 |
|
]; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get createdAtAttribute. |
134
|
|
|
* @return string timestamp |
135
|
|
|
*/ |
136
|
|
|
public function getCreatedAt() |
137
|
|
|
{ |
138
|
|
|
$createdAtAttribute = $this->createdAtAttribute; |
139
|
|
|
return $this->$createdAtAttribute; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Get rules associated with createdAtAttribute. |
144
|
|
|
* @return array rules |
145
|
|
|
*/ |
146
|
11 |
|
public function getCreatedAtRules() |
147
|
|
|
{ |
148
|
11 |
|
if (!$this->createdAtAttribute) { |
149
|
|
|
return []; |
150
|
|
|
} |
151
|
|
|
return [ |
152
|
11 |
|
[[$this->createdAtAttribute], 'safe'], |
153
|
11 |
|
]; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get rules associated with updatedAtAttribute. |
158
|
|
|
* @return array rules |
159
|
|
|
*/ |
160
|
11 |
|
public function getUpdatedAtRules() |
161
|
|
|
{ |
162
|
11 |
|
if (!$this->updatedAtAttribute) { |
163
|
1 |
|
return []; |
164
|
|
|
} |
165
|
|
|
return [ |
166
|
10 |
|
[[$this->updatedAtAttribute], 'safe'], |
167
|
10 |
|
]; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
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.