Completed
Push — master ( 323dc6...8df26d )
by vistart
17:10
created

TimestampTrait::offsetDatetime()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 0
cts 6
cp 0
rs 8.8571
cc 5
eloc 5
nc 4
nop 2
crap 30
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 string|int createdAt
21
 * @property-read string|int updatedAt
22
 * @property-read array $createdAtRules
23
 * @property-read array $updatedAtRules
24
 * @version 2.0
25
 * @author vistart <[email protected]>
26
 */
27
trait TimestampTrait
28
{
29
30
    /**
31
     * @var string the attribute that will receive datetime value
32
     * Set this property to false if you do not want to record the creation time.
33
     */
34
    public $createdAtAttribute = 'create_time';
35
36
    /**
37
     * @var string the attribute that will receive datetime value.
38
     * Set this property to false if you do not want to record the update time.
39
     */
40
    public $updatedAtAttribute = 'update_time';
41
42
    /**
43
     * @var integer Determine the format of timestamp.
44
     */
45
    public $timeFormat = 0;
46
    public static $timeFormatDatetime = 0;
47
    public static $timeFormatTimestamp = 1;
48
    public static $initDatetime = '1970-01-01 00:00:00';
49
    public static $initTimestamp = 0;
50
51
    /**
52
     * Get the current date & time in format of "Y-m-d H:i:s" or timestamp.
53
     * You can override this method to customize the return value.
54
     * @param \yii\base\ModelEvent $event
55
     * @return string Date & Time.
56
     * @since 1.1
57
     */
58 44
    public static function getCurrentDatetime($event)
59
    {
60 44
        $sender = $event->sender;
61 44
        return $sender->currentDatetime();
62
    }
63
64 44
    public function currentDatetime()
65
    {
66 44
        if ($this->timeFormat === self::$timeFormatDatetime) {
67 44
            return date('Y-m-d H:i:s');
68
        }
69
        if ($this->timeFormat === self::$timeFormatTimestamp) {
70
            return time();
71
        }
72
    }
73
74
    public function offsetDatetime($time = null, $offset = 0)
75
    {
76
        if ($this->timeFormat === self::$timeFormatDatetime) {
77
            return date('Y-m-d H:i:s', strtotime("+$offset seconds", is_string($time) ? strtotime($time) : time()));
78
        }
79
        if ($this->timeFormat === self::$timeFormatTimestamp) {
80
            return (is_int($time) ? $time : time()) + $offset;
81
        }
82
    }
83
84
    /**
85
     * Get init date & time in format of "Y-m-d H:i:s" or timestamp.s
86
     * @param \yii\base\ModelEvent $event
87
     * @return string|int
88
     */
89 6
    public static function getInitDatetime($event)
90
    {
91 6
        $sender = $event->sender;
92 6
        return $sender->initDatetime();
93
    }
94
95 6
    public function initDatetime()
96
    {
97 6
        if ($this->timeFormat === self::$timeFormatDatetime) {
98 6
            return static::$initDatetime;
99
        }
100
        if ($this->timeFormat === self::$timeFormatTimestamp) {
101
            return static::$initTimestamp;
102
        }
103
        return null;
104
    }
105
106
    /**
107
     * Check whether the attribute is init datetime.
108
     * @param mixed $attribute
109
     * @return boolean
110
     */
111 4
    protected function isInitDatetime($attribute)
112
    {
113 4
        if ($this->timeFormat === self::$timeFormatDatetime) {
114 4
            return $attribute == static::$initDatetime || $attribute == null;
115
        }
116
        if ($this->timeFormat === self::$timeFormatTimestamp) {
117
            return $attribute == static::$initTimestamp || $attribute == null;
118
        }
119
        return false;
120
    }
121
122
    /**
123
     * Get the current date & time in format of "Y-m-d H:i:s".
124
     * This method is ONLY used for being triggered by event. DO NOT call,
125
     * override or modify it directly, unless you know the consequences.
126
     * @param \yii\base\Event $event
127
     * @return string Date & Time.
128
     * @since 1.1
129
     */
130 44
    public function onUpdateCurrentDatetime($event)
131
    {
132 44
        return self::getCurrentDatetime($event);
0 ignored issues
show
Compatibility introduced by
$event of type object<yii\base\Event> is not a sub-type of object<yii\base\ModelEvent>. It seems like you assume a child class of the class yii\base\Event to be always present.

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.

Loading history...
133
    }
134
135
    /**
136
     * Behaviors associated with timestamp.
137
     * @return array behaviors
138
     */
139 12
    public function getTimestampBehaviors()
140
    {
141
        return [
142
            [
143 12
                'class' => TimestampBehavior::className(),
144 12
                'createdAtAttribute' => $this->createdAtAttribute,
145 12
                'updatedAtAttribute' => $this->updatedAtAttribute,
146 12
                'value' => [$this, 'onUpdateCurrentDatetime'],
147
            ]
148 12
        ];
149
    }
150
151
    /**
152
     * Get createdAt.
153
     * @return string timestamp
154
     */
155
    public function getCreatedAt()
156
    {
157
        $createdAtAttribute = $this->createdAtAttribute;
158
        return $this->$createdAtAttribute;
159
    }
160
161
    /**
162
     * Get rules associated with createdAtAttribute.
163
     * @return array rules
164
     */
165 12
    public function getCreatedAtRules()
166
    {
167 12
        if (!$this->createdAtAttribute) {
168
            return [];
169
        }
170
        return [
171 12
            [[$this->createdAtAttribute], 'safe'],
172 12
        ];
173
    }
174
175
    /**
176
     * Get updatedAt.
177
     * @return string timestamp
178
     */
179
    public function getUpdatedAt()
180
    {
181
        $updatedAtAttribute = $this->updatedAtAttribute;
182
        return $this->$updatedAtAttribute;
183
    }
184
185
    /**
186
     * Get rules associated with updatedAtAttribute.
187
     * @return array rules
188
     */
189 12
    public function getUpdatedAtRules()
190
    {
191 12
        if (!$this->updatedAtAttribute) {
192 1
            return [];
193
        }
194
        return [
195 11
            [[$this->updatedAtAttribute], 'safe'],
196 11
        ];
197
    }
198
}
199