1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sfneal\Models; |
4
|
|
|
|
5
|
|
|
use DateTimeInterface; |
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
7
|
|
|
use Illuminate\Database\Eloquent\Model as EloquentModel; |
8
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
9
|
|
|
use Sfneal\Builders\QueryBuilder; |
10
|
|
|
use Sfneal\Models\Traits\UploadDirectory; |
11
|
|
|
|
12
|
|
|
abstract class Model extends EloquentModel |
13
|
|
|
{ |
14
|
|
|
use SoftDeletes; |
15
|
|
|
use UploadDirectory; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Retrieve the datetime format used in the timestamp attribute accessors. |
19
|
|
|
* |
20
|
|
|
* @return string |
21
|
|
|
*/ |
22
|
|
|
protected $timestampFormat = 'Y-m-d h:i a'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Query Builder. |
26
|
|
|
* @param $query |
27
|
|
|
* @return QueryBuilder |
28
|
|
|
*/ |
29
|
|
|
public function newEloquentBuilder($query) |
30
|
|
|
{ |
31
|
|
|
return new QueryBuilder($query); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return QueryBuilder|Builder |
36
|
|
|
*/ |
37
|
|
|
public static function query() |
38
|
|
|
{ |
39
|
|
|
return parent::query(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Prepare a date for array / JSON serialization. |
44
|
|
|
* |
45
|
|
|
* @param DateTimeInterface $date |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
protected function serializeDate(DateTimeInterface $date) |
49
|
|
|
{ |
50
|
|
|
return $date->format('Y-m-d H:i:s'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Max number of hours ago a model instance could have been created ago and considered 'new'. |
55
|
|
|
*/ |
56
|
|
|
private const IS_NEW_MAX_HOURS = 168; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Determine if a model instance 'is new' if it was created within the time frame. |
60
|
|
|
* |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
|
|
public function isNew(): bool |
64
|
|
|
{ |
65
|
|
|
if (empty($this->created_at)) { |
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return strtotime($this->created_at) >= strtotime('-'.self::IS_NEW_MAX_HOURS.' hours'); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Determine how new a model instance is by subtracting the current time with the created_at time. |
74
|
|
|
* |
75
|
|
|
* @return float |
76
|
|
|
*/ |
77
|
|
|
public function howNew(): float |
78
|
|
|
{ |
79
|
|
|
return round((time() - strtotime($this->created_at)) / (60 * 60 * 24)); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Return the value of the 'is new' column (default is 'created_at'). |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function getIsNewColumn() |
88
|
|
|
{ |
89
|
|
|
return $this->getCreatedAtColumn(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Determine if a model has an attribute. |
94
|
|
|
* |
95
|
|
|
* - Optionally determine if the attribute is fillable. |
96
|
|
|
* - Allows $attr to be null for conditionals where a column may not exist |
97
|
|
|
* |
98
|
|
|
* @param string|null $attr |
99
|
|
|
* @param bool $is_fillable |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
|
|
public function hasAttribute(string $attr = null, bool $is_fillable = false): bool |
103
|
|
|
{ |
104
|
|
|
// Determine that the attribute exists and optionally weather it is fillable |
105
|
|
|
return |
106
|
|
|
isset($attr) && |
107
|
|
|
array_key_exists($attr, $this->attributesToArray()) && |
108
|
|
|
(! $is_fillable || in_array($attr, $this->getFillable())); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Determine if a model has attribute that is also null. |
113
|
|
|
* |
114
|
|
|
* @param string $attr |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
|
|
public function hasNullAttribute(string $attr): bool |
118
|
|
|
{ |
119
|
|
|
return $this->hasAttribute($attr) && is_null($this->getAttribute($attr)); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Retrieve an integer has of the model instance's ID. |
124
|
|
|
* |
125
|
|
|
* @return int |
126
|
|
|
*/ |
127
|
|
|
public function getIdHashAttribute(): int |
128
|
|
|
{ |
129
|
|
|
return crc32($this->getKey()); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Returns the default 'label' for a model instance. |
134
|
|
|
* |
135
|
|
|
* @return mixed |
136
|
|
|
*/ |
137
|
|
|
public function getLabel() |
138
|
|
|
{ |
139
|
|
|
return $this->getKey(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Retrieve a Model's table name statically. |
144
|
|
|
* |
145
|
|
|
* @return mixed |
146
|
|
|
*/ |
147
|
|
|
public static function getTableName() |
148
|
|
|
{ |
149
|
|
|
return with(new static)->getTable(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Retrieve a Model's primary key statically. |
154
|
|
|
* |
155
|
|
|
* @return mixed |
156
|
|
|
*/ |
157
|
|
|
public static function getPrimaryKeyName() |
158
|
|
|
{ |
159
|
|
|
return with(new static)->getKeyName(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Determine if a Model was recently 'created'. |
164
|
|
|
* |
165
|
|
|
* @return bool |
166
|
|
|
*/ |
167
|
|
|
public function wasCreated(): bool |
168
|
|
|
{ |
169
|
|
|
return $this->wasRecentlyCreated; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Determine if a Model was recently 'updated'. |
174
|
|
|
* |
175
|
|
|
* @return bool |
176
|
|
|
*/ |
177
|
|
|
public function wasUpdated(): bool |
178
|
|
|
{ |
179
|
|
|
return $this->wasChanged(); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Determine if a Model was recently 'deleted'. |
184
|
|
|
* |
185
|
|
|
* @return bool |
186
|
|
|
*/ |
187
|
|
|
public function wasDeleted(): bool |
188
|
|
|
{ |
189
|
|
|
return ! $this->exists || ! is_null($this->deleted_at); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Retrieve the most recent Model change. |
194
|
|
|
* |
195
|
|
|
* @return string |
196
|
|
|
*/ |
197
|
|
|
public function mostRecentChange(): string |
198
|
|
|
{ |
199
|
|
|
if ($this->wasCreated()) { |
200
|
|
|
return 'created'; |
201
|
|
|
} elseif ($this->wasUpdated()) { |
202
|
|
|
return 'updated'; |
203
|
|
|
} elseif ($this->wasDeleted()) { |
204
|
|
|
return 'deleted'; |
205
|
|
|
} else { |
206
|
|
|
return 'unchanged'; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Retrieve the 'timestampFormat' property. |
212
|
|
|
* |
213
|
|
|
* @return string |
214
|
|
|
*/ |
215
|
|
|
public function getTimestampFormat(): string |
216
|
|
|
{ |
217
|
|
|
return $this->timestampFormat; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Retrieve the 'timestampFormatForHumans' property. |
222
|
|
|
* |
223
|
|
|
* @param string $stringToTime |
224
|
|
|
* @return string |
225
|
|
|
*/ |
226
|
|
|
protected function getDatetimeForHumans(string $stringToTime): string |
227
|
|
|
{ |
228
|
|
|
return date('F j, Y', $stringToTime).' at '.date('g:i a', $stringToTime); |
|
|
|
|
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Retrieve the 'created_at' attribute mutated to human readable datetime. |
233
|
|
|
* |
234
|
|
|
* @return string |
235
|
|
|
*/ |
236
|
|
|
public function getDatetimeAttribute(): string |
237
|
|
|
{ |
238
|
|
|
return date('Y-m-d h:i a', strtotime($this->created_at)); |
|
|
|
|
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Retrieve the 'created_at' attribute mutated to timestamp string. |
243
|
|
|
* |
244
|
|
|
* - 'created_timestamp' attribute accessor |
245
|
|
|
* |
246
|
|
|
* @return string |
247
|
|
|
*/ |
248
|
|
|
public function getCreatedTimestampAttribute(): string |
249
|
|
|
{ |
250
|
|
|
return date($this->getTimestampFormat(), strtotime($this->created_at)); |
|
|
|
|
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Retrieve the 'created_for_humans' attribute mutated to a timestamp for humans string. |
255
|
|
|
* |
256
|
|
|
* - 'created_for_humans' attribute accessor |
257
|
|
|
* |
258
|
|
|
* @return string |
259
|
|
|
*/ |
260
|
|
|
public function getCreatedForHumansAttribute(): string |
261
|
|
|
{ |
262
|
|
|
return $this->getDatetimeForHumans(strtotime($this->created_at)); |
|
|
|
|
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Retrieve the 'created_at' attribute mutated to difference for humans string. |
267
|
|
|
* |
268
|
|
|
* - 'created_for_humans' attribute accessor |
269
|
|
|
* |
270
|
|
|
* @return string |
271
|
|
|
*/ |
272
|
|
|
public function getCreatedDiffForHumansAttribute(): string |
273
|
|
|
{ |
274
|
|
|
return $this->created_at->diffForHumans(); |
|
|
|
|
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Access the 'creation_date' attribute that returns the attribute in 'Y-m-d' format. |
279
|
|
|
* |
280
|
|
|
* @return string |
281
|
|
|
*/ |
282
|
|
|
public function getCreatedDateAttribute(): string |
283
|
|
|
{ |
284
|
|
|
return date('Y-m-d', strtotime($this->created_at)); |
|
|
|
|
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Access the 'creation_time' attribute that returns the attribute in 'h:i A' format. |
289
|
|
|
* |
290
|
|
|
* @return string |
291
|
|
|
*/ |
292
|
|
|
public function getCreatedTimeAttribute(): string |
293
|
|
|
{ |
294
|
|
|
return date('h:i A', strtotime($this->created_at)); |
|
|
|
|
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Retrieve the 'updated_at' attribute mutated to timestamp string. |
299
|
|
|
* |
300
|
|
|
* - 'updated_timestamp' attribute accessor |
301
|
|
|
* |
302
|
|
|
* @return string |
303
|
|
|
*/ |
304
|
|
|
public function getUpdatedTimestampAttribute(): string |
305
|
|
|
{ |
306
|
|
|
return date($this->getTimestampFormat(), strtotime($this->updated_at)); |
|
|
|
|
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Retrieve the 'updated_for_humans' attribute mutated to a timestamp for humans string. |
311
|
|
|
* |
312
|
|
|
* - 'updated_for_humans' attribute accessor |
313
|
|
|
* |
314
|
|
|
* @return string |
315
|
|
|
*/ |
316
|
|
|
public function getUpdatedForHumansAttribute(): string |
317
|
|
|
{ |
318
|
|
|
return $this->getDatetimeForHumans(strtotime($this->updated_at)); |
|
|
|
|
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Retrieve the 'updated_at' attribute mutated to difference for humans string. |
323
|
|
|
* |
324
|
|
|
* - 'updated_for_humans' attribute accessor |
325
|
|
|
* |
326
|
|
|
* @return string |
327
|
|
|
*/ |
328
|
|
|
public function getUpdatedDiffForHumansAttribute(): string |
329
|
|
|
{ |
330
|
|
|
return $this->updated_at->diffForHumans(); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Access the 'updated_date' attribute that returns the attribute in 'Y-m-d' format. |
335
|
|
|
* |
336
|
|
|
* @return string |
337
|
|
|
*/ |
338
|
|
|
public function getUpdatedDateAttribute(): string |
339
|
|
|
{ |
340
|
|
|
return date('Y-m-d', strtotime($this->updated_at)); |
|
|
|
|
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Access the 'updated_time' attribute that returns the attribute in 'h:i A' format. |
345
|
|
|
* |
346
|
|
|
* @return string |
347
|
|
|
*/ |
348
|
|
|
public function getUpdatedTimeAttribute(): string |
349
|
|
|
{ |
350
|
|
|
return date('h:i A', strtotime($this->updated_at)); |
|
|
|
|
351
|
|
|
} |
352
|
|
|
} |
353
|
|
|
|