|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Shetabit\Stampable\Traits; |
|
4
|
|
|
|
|
5
|
|
|
trait HasStamps |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* Get current timestamp |
|
9
|
|
|
* |
|
10
|
|
|
* @return false|\Illuminate\Support\Carbon|string |
|
11
|
|
|
*/ |
|
12
|
|
|
private function getFreshTimestamp() |
|
13
|
|
|
{ |
|
14
|
|
|
$timestamp = function_exists('now') ? now() : date('Y/m/d H:i:s'); |
|
15
|
|
|
|
|
16
|
|
|
return $timestamp; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Determine if an stamp exists |
|
21
|
|
|
* |
|
22
|
|
|
* @param $stampName |
|
23
|
|
|
* @return bool |
|
24
|
|
|
*/ |
|
25
|
|
|
private function stampExists($stampName) |
|
26
|
|
|
{ |
|
27
|
|
|
$stamps = $this->getStamps(); |
|
28
|
|
|
|
|
29
|
|
|
return isset($stamps[$stampName]); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Retrieve available stamps |
|
34
|
|
|
* |
|
35
|
|
|
* @return array |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getStamps() |
|
38
|
|
|
{ |
|
39
|
|
|
$stamps = []; |
|
40
|
|
|
|
|
41
|
|
|
if (!empty($this->stamps)) { |
|
42
|
|
|
/* |
|
43
|
|
|
* Change structure to [stampName => fieldName] |
|
44
|
|
|
* if stampName is numeric, we use fieldName as stampName. |
|
45
|
|
|
*/ |
|
46
|
|
|
foreach ($this->stamps as $key => $stamp) { |
|
47
|
|
|
if (is_numeric($key)) { |
|
48
|
|
|
$stamps[$stamp] = $stamp; |
|
49
|
|
|
} else { |
|
50
|
|
|
$stamps[$key] = $stamp; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $stamps; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Determine if the data is published. |
|
60
|
|
|
* |
|
61
|
|
|
* @param $stampName |
|
62
|
|
|
* @return bool |
|
63
|
|
|
*/ |
|
64
|
|
|
public function isStampedBy($stampName) |
|
65
|
|
|
{ |
|
66
|
|
|
$stamps = $this->getStamps(); |
|
67
|
|
|
|
|
68
|
|
|
// if stamp exists, we check it for not being null, or will return false |
|
69
|
|
|
return $this->stampExists($stampName) ? ($this->{$stamps[$stampName]} !== null) : false; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Determine if the data is unpublished. |
|
74
|
|
|
* |
|
75
|
|
|
* @param $stampName |
|
76
|
|
|
* @return bool |
|
77
|
|
|
*/ |
|
78
|
|
|
public function isUnstampedBy($stampName) |
|
79
|
|
|
{ |
|
80
|
|
|
$stamps = $this->getStamps(); |
|
81
|
|
|
|
|
82
|
|
|
// if stamp exists, we check it for being null, or will return false |
|
83
|
|
|
return $this->stampExists($stampName) ? ($this->{$stamps[$stampName]} == null) : false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Mark the current instance as stamped. |
|
88
|
|
|
* |
|
89
|
|
|
* @return bool |
|
90
|
|
|
*/ |
|
91
|
|
|
public function markAsStamped($stampName) |
|
92
|
|
|
{ |
|
93
|
|
|
$stamps = $this->getStamps(); |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
return $this->forceFill([$stamps[$stampName] => $this->getFreshTimestamp()])->save(); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Mark the current instance as unstamped. |
|
101
|
|
|
* |
|
102
|
|
|
* @return bool |
|
103
|
|
|
*/ |
|
104
|
|
|
public function markAsUnstamped($stampName) |
|
105
|
|
|
{ |
|
106
|
|
|
$stamps = $this->getStamps(); |
|
107
|
|
|
|
|
108
|
|
|
return $this->forceFill([$stamps[$stampName] => null])->save(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Get only stamped data. |
|
113
|
|
|
* |
|
114
|
|
|
* @return mixed |
|
115
|
|
|
*/ |
|
116
|
|
|
public function scopeStamped($query, $stampName) |
|
117
|
|
|
{ |
|
118
|
|
|
$stamps = $this->getStamps(); |
|
119
|
|
|
|
|
120
|
|
|
return $query->whereNotNull($stamps[$stampName]); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Get only unstamped data. |
|
125
|
|
|
* |
|
126
|
|
|
* @param $query |
|
127
|
|
|
* @param $stampName |
|
128
|
|
|
* @return mixed |
|
129
|
|
|
*/ |
|
130
|
|
|
public function scopeUnstamped($query, $stampName) |
|
131
|
|
|
{ |
|
132
|
|
|
$stamps = $this->getStamps(); |
|
133
|
|
|
|
|
134
|
|
|
return $query->whereNull($stamps[$stampName]); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
private function getStampBehavior($method) |
|
138
|
|
|
{ |
|
139
|
|
|
$behavior = null; |
|
140
|
|
|
$stampName = null; |
|
141
|
|
|
$lowerCaseMethod = strtolower($method); |
|
142
|
|
|
|
|
143
|
|
|
$prefixes = [ |
|
144
|
|
|
'is' => 'isStampedBy', |
|
145
|
|
|
'isUn' => 'isUnstampedBy', |
|
146
|
|
|
'markAs' => 'markAsStamped', |
|
147
|
|
|
'markAsUn' => 'markAsUnstamped' |
|
148
|
|
|
]; |
|
149
|
|
|
|
|
150
|
|
|
$stampsName = array_keys($this->getStamps()); |
|
151
|
|
|
|
|
152
|
|
|
foreach ($stampsName as $key => $name) { |
|
153
|
|
|
foreach ($prefixes as $prefix => $methodName) { |
|
154
|
|
|
if (strtolower($prefix.$name) == $lowerCaseMethod) { |
|
155
|
|
|
$behavior = $methodName; |
|
156
|
|
|
$stampName = $name; |
|
157
|
|
|
break; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return empty($behavior) ? false : [$behavior, $stampName]; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
private function getStampScope($method) |
|
166
|
|
|
{ |
|
167
|
|
|
$scope = null; |
|
168
|
|
|
$lowerCaseMethod = strtolower($method); |
|
169
|
|
|
|
|
170
|
|
|
$stampKeys = array_keys($this->getStamps()); |
|
171
|
|
|
|
|
172
|
|
|
foreach ($stampKeys as $key) { |
|
173
|
|
|
if ($lowerCaseMethod == $key) { |
|
174
|
|
|
$scope = 'stamped'; |
|
175
|
|
|
} elseif ($lowerCaseMethod == strtolower('un'.$key)) { |
|
176
|
|
|
$scope = 'unstamped'; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
return $scope; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Handle dynamic method calls into the model. |
|
185
|
|
|
* |
|
186
|
|
|
* @param string $method |
|
187
|
|
|
* @param array $parameters |
|
188
|
|
|
* @return mixed |
|
189
|
|
|
*/ |
|
190
|
|
|
public function __call($method, $parameters) |
|
191
|
|
|
{ |
|
192
|
|
|
if (in_array($method, ['increment', 'decrement'])) { |
|
193
|
|
|
return $this->$method(...$parameters); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
if ($info = $this->getStampBehavior($method)) { |
|
197
|
|
|
$methodName = $info[0]; |
|
198
|
|
|
$stampName = $info[1]; |
|
199
|
|
|
return $this->$methodName($stampName); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
if ($methodName = $this->getStampScope($method)) { |
|
203
|
|
|
return $this->forwardCallTo($this->newQuery(), $methodName, [$method]); |
|
|
|
|
|
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
return $this->forwardCallTo($this->newQuery(), $method, $parameters); |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|