1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\DataObjects\Behaviors\Timestampable; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Nip\Utility\Date; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Trait TimestampableTrait |
10
|
|
|
* @package ByTIC\DataObjects\Behaviors\Timestampable |
11
|
|
|
*/ |
12
|
|
|
trait TimestampableTrait |
13
|
|
|
{ |
14
|
|
|
protected $timestampableTypes = ['create', 'update']; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param $attributes |
18
|
|
|
* @throws \Exception |
19
|
|
|
*/ |
20
|
|
|
public function updatedTimestamps($attributes) |
21
|
|
|
{ |
22
|
|
|
if (is_string($attributes)) { |
23
|
|
|
if (in_array($attributes, $this->timestampableTypes)) { |
24
|
|
|
$attributes = $this->getTimestampAttributes($attributes); |
25
|
|
|
} else { |
26
|
|
|
$attributes = [$attributes]; |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
foreach ($attributes as $attribute) { |
30
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
31
|
|
|
$this->setModelTimeAttribute($attribute, $this->{$attribute}); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function clearTimestamps() |
36
|
|
|
{ |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function bootTimestampableTrait() |
40
|
|
|
{ |
41
|
|
|
$this->hookCastableTrait(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param $attribute |
46
|
|
|
* @param $value |
47
|
|
|
* @return false|int|mixed|Date |
48
|
|
|
* @throws \Exception |
49
|
|
|
*/ |
50
|
|
|
public function setModelTimeAttribute($attribute, $value = null) |
51
|
|
|
{ |
52
|
|
|
if (\is_string($value) && !empty($value)) { |
53
|
|
|
$unix = \strtotime($value); |
54
|
|
|
if ($unix === false) { |
55
|
|
|
throw new \Exception( |
56
|
|
|
sprintf( |
57
|
|
|
"Error parsing time value [%s] for field [%s] in object [%s]", |
58
|
|
|
$value, |
59
|
|
|
$attribute, |
60
|
|
|
get_class($this) |
61
|
|
|
) |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
$value = $unix; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (\is_numeric($value)) { |
68
|
|
|
$value = Date::createFromTimestamp($value); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (!\is_object($value)) { |
72
|
|
|
$value = Date::now(); |
73
|
|
|
} |
74
|
|
|
return $this->{$attribute} = $value; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get a fresh timestamp for the model. |
79
|
|
|
* |
80
|
|
|
* @return \Nip\Utility\Date |
81
|
|
|
*/ |
82
|
|
|
public function freshTimestamp(): DateTime |
83
|
|
|
{ |
84
|
|
|
return Date::now(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $type |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
public function getTimestampAttributes(string $type = 'create'): array |
92
|
|
|
{ |
93
|
|
|
if (!in_array($type, ['create', 'update'])) { |
94
|
|
|
return []; |
95
|
|
|
} |
96
|
|
|
$updateTimestamps = $this->getUpdateTimestamps(); |
97
|
|
|
if ($type == 'update') { |
98
|
|
|
return $updateTimestamps; |
99
|
|
|
} |
100
|
|
|
$createTimestamps = $this->getCreateTimestamps(); |
101
|
|
|
|
102
|
|
|
return array_merge($createTimestamps, $updateTimestamps); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get the name of the "created at" column. |
107
|
|
|
* |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
public function getCreateTimestamps(): array |
111
|
|
|
{ |
112
|
|
|
if (!isset(static::$createTimestamps)) { |
113
|
|
|
return ['created_at']; |
114
|
|
|
} |
115
|
|
|
if (is_string(static::$createTimestamps)) { |
116
|
|
|
static::$createTimestamps = [static::$createTimestamps]; |
|
|
|
|
117
|
|
|
} |
118
|
|
|
return static::$createTimestamps; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get the name of the "updated at" column. |
123
|
|
|
* |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
|
|
public function getUpdateTimestamps(): array |
127
|
|
|
{ |
128
|
|
|
if (!isset(static::$createTimestamps)) { |
129
|
|
|
return ['updated_at']; |
130
|
|
|
} |
131
|
|
|
if (is_string(static::$updateTimestamps)) { |
132
|
|
|
static::$updateTimestamps = [static::$updateTimestamps]; |
|
|
|
|
133
|
|
|
} |
134
|
|
|
return static::$updateTimestamps; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get carbon object from timestamp attribute. |
139
|
|
|
* |
140
|
|
|
* @param string $attribute Attribute name |
141
|
|
|
* |
142
|
|
|
* @return Date|null|string |
143
|
|
|
*/ |
144
|
|
|
public function getTimeFromAttribute(string $attribute) |
145
|
|
|
{ |
146
|
|
|
$value = $this->{$attribute}; |
|
|
|
|
147
|
|
|
|
148
|
|
|
if (!$this->{$attribute}) { |
149
|
|
|
return $this->attributes[$attribute] = Date::now()->timestamp; |
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if (\is_string($this->attributes[$attribute]) && $timestamp = \strtotime($this->attributes[$attribute])) { |
153
|
|
|
return $this->attributes[$attribute] = (new Date())->setTimestamp($timestamp); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $this->attributes[$attribute]; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
protected function hookCastableTrait() |
160
|
|
|
{ |
161
|
|
|
if (method_exists($this, 'addCast') === false) { |
162
|
|
|
return; |
163
|
|
|
} |
164
|
|
|
$fields = $this->getTimestampAttributes(); |
165
|
|
|
foreach ($fields as $field) { |
166
|
|
|
if ($this->hasCast($field)) { |
|
|
|
|
167
|
|
|
continue; |
168
|
|
|
} |
169
|
|
|
$this->addCast($field,'datetime'); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|