Completed
Push — 6.0 ( a99d0b...cc2e0b )
by liu
02:48
created

TimeStamp::formatDateTime()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 6
nop 3
dl 0
loc 22
ccs 0
cts 14
cp 0
crap 42
rs 9.2222
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: liu21st <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think\model\concern;
14
15
use DateTime;
16
17
/**
18
 * 自动时间戳
19
 */
5 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
20
trait TimeStamp
21
{
22
    /**
23
     * 是否需要自动写入时间戳 如果设置为字符串 则表示时间字段的类型
24
     * @var bool|string
25
     */
26
    protected $autoWriteTimestamp;
27
28
    /**
29
     * 创建时间字段 false表示关闭
30
     * @var false|string
31
     */
32
    protected $createTime = 'create_time';
33
34
    /**
35
     * 更新时间字段 false表示关闭
36
     * @var false|string
37
     */
38
    protected $updateTime = 'update_time';
39
40
    /**
41
     * 时间字段显示格式
42
     * @var string
43
     */
44
    protected $dateFormat;
45
46
    /**
47
     * 是否需要自动写入时间字段
48
     * @access public
49
     * @param  bool|string $auto
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
50
     * @return $this
51
     */
52
    public function isAutoWriteTimestamp($auto)
53
    {
54
        $this->autoWriteTimestamp = $auto;
55
56
        return $this;
57
    }
58
59
    /**
60
     * 获取自动写入时间字段
61
     * @access public
62
     * @return $this
63
     */
64
    public function getAutoWriteTimestamp()
65
    {
66
        return $this->autoWriteTimestamp;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->autoWriteTimestamp returns the type boolean|string which is incompatible with the documented return type think\model\concern\TimeStamp.
Loading history...
67
    }
68
69
    /**
70
     * 设置时间字段格式化
71
     * @access public
72
     * @param  string $format
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
73
     * @return $this
74
     */
75
    public function setDateFormat(string $format)
76
    {
77
        $this->dateFormat = $format;
78
79
        return $this;
80
    }
81
82
    /**
83
     * 获取自动写入时间字段
84
     * @access public
85
     * @return $this
86
     */
87
    public function getDateFormat()
88
    {
89
        return $this->dateFormat;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->dateFormat returns the type string which is incompatible with the documented return type think\model\concern\TimeStamp.
Loading history...
90
    }
91
92
    /**
93
     * 自动写入时间戳
94
     * @access protected
95
     * @param  string $name 时间戳字段
96
     * @return mixed
97
     */
98
    protected function autoWriteTimestamp(string $name)
99
    {
100
        $value = time();
101
102
        if (isset($this->type[$name])) {
103
            $type = $this->type[$name];
104
105
            if (strpos($type, ':')) {
106
                list($type, $param) = explode(':', $type, 2);
107
            }
108
109
            switch ($type) {
110
                case 'datetime':
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
111
                case 'date':
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
112
                case 'timestamp':
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
113
                    $format = !empty($param) ? $param : $this->dateFormat;
114
                    $format .= strpos($format, 'u') || false !== strpos($format, '\\') ? '' : '.u';
115
                    $value = $this->formatDateTime($format);
116
                    break;
117
                default:
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
118
                    if (false !== strpos($type, '\\')) {
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
119
                        // 对象数据写入
120
                        $value = new $type();
121
                        if (method_exists($value, '__toString')) {
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
122
                            // 对象数据写入
123
                            $value = $value->__toString();
124
                        }
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 20 spaces, found 24
Loading history...
125
                    }
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
126
            }
127
        } elseif (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp),
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
128
            ['datetime', 'date', 'timestamp'])) {
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 8 spaces, but found 12.
Loading history...
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
Coding Style introduced by
Closing parenthesis of a multi-line IF statement must be on a new line
Loading history...
129
            $format = strpos($this->dateFormat, 'u') || false !== strpos($this->dateFormat, '\\') ? '' : '.u';
130
            $value  = $this->formatDateTime($this->dateFormat . $format);
131
        }
132
133
        return $value;
134
    }
135
136
    /**
137
     * 时间日期字段格式化处理
138
     * @access protected
139
     * @param  mixed $format    日期格式
140
     * @param  mixed $time      时间日期表达式
141
     * @param  bool  $timestamp 时间表达式是否为时间戳
142
     * @return mixed
143
     */
144
    protected function formatDateTime($format, $time = 'now', bool $timestamp = false)
145
    {
146
        if (empty($time)) {
147
            return;
148
        }
149
150
        if (false === $format) {
151
            return $time;
152
        } elseif (false !== strpos($format, '\\')) {
153
            return new $format($time);
154
        }
155
156
        if ($time instanceof DateTime) {
157
            $dateTime = $time;
158
        } elseif ($timestamp) {
159
            $dateTime = new DateTime();
160
            $dateTime->setTimestamp((int) $time);
161
        } else {
162
            $dateTime = new DateTime($time);
163
        }
164
165
        return $dateTime->format($format);
166
    }
167
168
    /**
169
     * 获取时间字段值
170
     * @access protected
171
     * @param  mixed   $value
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
172
     * @return mixed
173
     */
174
    protected function getTimestampValue($value)
175
    {
176
        if (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
177
            'datetime', 'date', 'timestamp',
178
        ])) {
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
Coding Style introduced by
Closing parenthesis of a multi-line IF statement must be on a new line
Loading history...
179
            $value = $this->formatDateTime($this->dateFormat, $value);
180
        } else {
181
            $value = $this->formatDateTime($this->dateFormat, $value, true);
182
        }
183
184
        return $value;
185
    }
186
}
187