1 | <?php |
||
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) |
|
59 | |||
60 | 39 | public function currentDatetime() |
|
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) |
|
115 | |||
116 | /** |
||
117 | * Behaviors associated with timestamp. |
||
118 | * @return array behaviors |
||
119 | */ |
||
120 | 11 | public function getTimestampBehaviors() |
|
131 | |||
132 | /** |
||
133 | * Get createdAtAttribute. |
||
134 | * @return string timestamp |
||
135 | */ |
||
136 | public function getCreatedAt() |
||
141 | |||
142 | /** |
||
143 | * Get rules associated with createdAtAttribute. |
||
144 | * @return array rules |
||
145 | */ |
||
146 | 11 | public function getCreatedAtRules() |
|
155 | |||
156 | /** |
||
157 | * Get rules associated with updatedAtAttribute. |
||
158 | * @return array rules |
||
159 | */ |
||
160 | 11 | public function getUpdatedAtRules() |
|
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.