| Total Complexity | 23 |
| Total Lines | 168 |
| Duplicated Lines | 0 % |
| Coverage | 0% |
| Changes | 0 | ||
| 1 | <?php |
||
| 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 | * @var bool |
||
| 49 | */ |
||
| 50 | protected $timestamp = false; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * 是否需要自动写入时间字段 |
||
| 54 | * @access public |
||
| 55 | * @param bool|string $auto |
||
| 56 | * @return $this |
||
| 57 | */ |
||
| 58 | public function isAutoWriteTimestamp($auto) |
||
| 59 | { |
||
| 60 | $this->autoWriteTimestamp = $auto; |
||
| 61 | |||
| 62 | return $this; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * 获取自动写入时间字段 |
||
| 67 | * @access public |
||
| 68 | * @return bool|string |
||
| 69 | */ |
||
| 70 | public function getAutoWriteTimestamp() |
||
| 71 | { |
||
| 72 | return $this->autoWriteTimestamp; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * 设置时间字段格式化 |
||
| 77 | * @access public |
||
| 78 | * @param string $format |
||
| 79 | * @return $this |
||
| 80 | */ |
||
| 81 | public function setDateFormat(string $format) |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * 获取自动写入时间字段 |
||
| 90 | * @access public |
||
| 91 | * @return string |
||
| 92 | */ |
||
| 93 | public function getDateFormat() |
||
| 94 | { |
||
| 95 | return $this->dateFormat; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * 自动写入时间戳 |
||
| 100 | * @access protected |
||
| 101 | * @param string $name 时间戳字段 |
||
| 102 | * @return mixed |
||
| 103 | */ |
||
| 104 | protected function autoWriteTimestamp(string $name) |
||
| 105 | { |
||
| 106 | $value = time(); |
||
| 107 | |||
| 108 | if (isset($this->type[$name])) { |
||
| 109 | $type = $this->type[$name]; |
||
| 110 | |||
| 111 | if (strpos($type, ':')) { |
||
| 112 | list($type, $param) = explode(':', $type, 2); |
||
| 113 | } |
||
| 114 | |||
| 115 | switch ($type) { |
||
| 116 | case 'datetime': |
||
|
1 ignored issue
–
show
|
|||
| 117 | case 'date': |
||
|
1 ignored issue
–
show
|
|||
| 118 | case 'timestamp': |
||
|
1 ignored issue
–
show
|
|||
| 119 | $value = $this->formatDateTime('Y-m-d H:i:s.u'); |
||
| 120 | break; |
||
| 121 | default: |
||
|
1 ignored issue
–
show
|
|||
| 122 | if (false !== strpos($type, '\\')) { |
||
|
1 ignored issue
–
show
|
|||
| 123 | // 对象数据写入 |
||
| 124 | $value = new $type(); |
||
| 125 | if (method_exists($value, '__toString')) { |
||
|
1 ignored issue
–
show
|
|||
| 126 | // 对象数据写入 |
||
| 127 | $value = $value->__toString(); |
||
| 128 | } |
||
|
1 ignored issue
–
show
|
|||
| 129 | } |
||
|
1 ignored issue
–
show
|
|||
| 130 | } |
||
| 131 | } elseif (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), |
||
| 132 | ['datetime', 'date', 'timestamp'])) { |
||
| 133 | $value = $this->formatDateTime('Y-m-d H:i:s.u'); |
||
| 134 | } |
||
| 135 | |||
| 136 | return $value; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * 时间日期字段格式化处理 |
||
| 141 | * @access protected |
||
| 142 | * @param mixed $format 日期格式 |
||
| 143 | * @param mixed $time 时间日期表达式 |
||
| 144 | * @param bool $timestamp 时间表达式是否为时间戳 |
||
| 145 | * @return mixed |
||
| 146 | */ |
||
| 147 | protected function formatDateTime($format, $time = 'now', bool $timestamp = false) |
||
| 148 | { |
||
| 149 | if (empty($time)) { |
||
| 150 | return; |
||
| 151 | } |
||
| 152 | |||
| 153 | if (false === $format) { |
||
| 154 | return $time; |
||
| 155 | } elseif (false !== strpos($format, '\\')) { |
||
| 156 | return new $format($time); |
||
| 157 | } |
||
| 158 | |||
| 159 | if ($time instanceof DateTime) { |
||
| 160 | $dateTime = $time; |
||
| 161 | } elseif ($timestamp) { |
||
| 162 | $dateTime = new DateTime(); |
||
| 163 | $dateTime->setTimestamp((int) $time); |
||
| 164 | } else { |
||
| 165 | $dateTime = new DateTime($time); |
||
| 166 | } |
||
| 167 | |||
| 168 | return $dateTime->format($format); |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * 获取时间字段值 |
||
| 173 | * @access protected |
||
| 174 | * @param mixed $value |
||
| 175 | * @return mixed |
||
| 176 | */ |
||
| 177 | protected function getTimestampValue($value) |
||
| 188 | } |
||
| 189 | } |
||
| 190 |