Passed
Push — master ( 5cfd35...8c1c5f )
by Stephen
04:58 queued 02:08
created

Model::createdDiffForHumans()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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