| Total Complexity | 28 |
| Total Lines | 165 |
| 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 | * @access public |
||
| 49 | * @param bool|string $auto |
||
| 50 | * @return $this |
||
| 51 | */ |
||
| 52 | public function isAutoWriteTimestamp($auto) |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * 获取自动写入时间字段 |
||
| 61 | * @access public |
||
| 62 | * @return $this |
||
| 63 | */ |
||
| 64 | public function getAutoWriteTimestamp() |
||
| 65 | { |
||
| 66 | return $this->autoWriteTimestamp; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * 设置时间字段格式化 |
||
| 71 | * @access public |
||
| 72 | * @param string $format |
||
| 73 | * @return $this |
||
| 74 | */ |
||
| 75 | public function setDateFormat(string $format) |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * 获取自动写入时间字段 |
||
| 84 | * @access public |
||
| 85 | * @return $this |
||
| 86 | */ |
||
| 87 | public function getDateFormat() |
||
| 88 | { |
||
| 89 | return $this->dateFormat; |
||
| 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
|
|||
| 111 | case 'date': |
||
|
1 ignored issue
–
show
|
|||
| 112 | case 'timestamp': |
||
|
1 ignored issue
–
show
|
|||
| 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
|
|||
| 118 | if (false !== strpos($type, '\\')) { |
||
|
1 ignored issue
–
show
|
|||
| 119 | // 对象数据写入 |
||
| 120 | $value = new $type(); |
||
| 121 | if (method_exists($value, '__toString')) { |
||
|
1 ignored issue
–
show
|
|||
| 122 | // 对象数据写入 |
||
| 123 | $value = $value->__toString(); |
||
| 124 | } |
||
|
1 ignored issue
–
show
|
|||
| 125 | } |
||
|
1 ignored issue
–
show
|
|||
| 126 | } |
||
| 127 | } elseif (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), |
||
| 128 | ['datetime', 'date', 'timestamp'])) { |
||
| 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 |
||
| 172 | * @return mixed |
||
| 173 | */ |
||
| 174 | protected function getTimestampValue($value) |
||
| 185 | } |
||
| 186 | } |
||
| 187 |