Completed
Push — master ( a53812...c29406 )
by vistart
06:25
created

TimestampTrait::isInitDatetime()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 0
cts 6
cp 0
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 12
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 38
    public static function getCurrentDatetime($event)
55
    {
56 38
        $sender = $event->sender;
57 38
        return $sender->currentDatetime();
58
    }
59
60 38
    public function currentDatetime()
61
    {
62 38
        if ($this->timeFormat === self::$timeFormatDatetime) {
63 38
            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
    public static function getInitDatetime($event)
76
    {
77
        $sender = $event->sender;
78
        return $sender->initDatetime();
79
    }
80
81
    public function initDatetime()
82
    {
83
        if ($this->timeFormat === self::$timeFormatDatetime) {
84
            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';
96
        }
97
        if ($this->timeFormat === self::$timeFormatTimestamp) {
98
            return $this->$attribute == 0;
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 38
    public function onUpdateCurrentDatetime($event)
112
    {
113 38
        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...
114
    }
115
116
    /**
117
     * Behaviors associated with timestamp.
118
     * @return array behaviors
119
     */
120 10
    public function getTimestampBehaviors()
121
    {
122
        return [
123
            [
124 10
                'class' => TimestampBehavior::className(),
125 10
                'createdAtAttribute' => $this->createdAtAttribute,
126 10
                'updatedAtAttribute' => $this->updatedAtAttribute,
127 10
                'value' => [$this, 'onUpdateCurrentDatetime'],
128
            ]
129 10
        ];
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 10
    public function getCreatedAtRules()
147
    {
148 10
        if (!$this->createdAtAttribute) {
149
            return [];
150
        }
151
        return [
152 10
            [[$this->createdAtAttribute], 'safe'],
153 10
        ];
154
    }
155
156
    /**
157
     * Get rules associated with updatedAtAttribute.
158
     * @return array rules
159
     */
160 10
    public function getUpdatedAtRules()
161
    {
162 10
        if (!$this->updatedAtAttribute) {
163 1
            return [];
164
        }
165
        return [
166 9
            [[$this->updatedAtAttribute], 'safe'],
167 9
        ];
168
    }
169
}
170