Model::createdForHumans()   A
last analyzed

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
        // Laravel v11 added a `hasAttribute()` method, use parent method if available
107
        try {
108
            return parent::hasAttribute($key);
109
        } catch (\BadMethodCallException $methodCallException) {
110
            return array_key_exists($key, $this->attributes);
111
        }
112
    }
113
114
    /**
115
     * Determine if a model has an attribute.
116
     *
117
     *  - Optionally determine if the attribute is fillable.
118
     *  - Allows $attr to be null for conditionals where a column may not exist
119
     *
120
     * @param  string  $key
121
     * @return bool
122
     */
123
    public function hasAttributeFillable($key): bool
124
    {
125
        // Determine that the attribute exists and weather it is fillable
126
        return
127
            $this->hasAttribute($key) &&
128
            in_array($key, $this->getFillable());
129
    }
130
131
    /**
132
     * Determine if a model has attribute that is also null.
133
     *
134
     * @param  string  $attr
135
     * @return bool
136
     */
137
    public function hasNullAttribute(string $attr): bool
138
    {
139
        return $this->hasAttribute($attr) && is_null($this->getAttribute($attr));
140
    }
141
142
    /**
143
     * Retrieve an integer has of the model instance's ID.
144
     *
145
     * @return int
146
     */
147
    public function idHash(): int
148
    {
149
        return crc32($this->getKey());
150
    }
151
152
    /**
153
     * Returns the default 'label' for a model instance.
154
     *
155
     * @return mixed
156
     */
157
    public function getLabel()
158
    {
159
        return $this->getKey();
160
    }
161
162
    /**
163
     * Retrieve a Model's table name statically.
164
     *
165
     * @return mixed
166
     */
167
    public static function getTableName()
168
    {
169
        /** @phpstan-ignore-next-line */
170
        return (new static)->getTable();
171
    }
172
173
    /**
174
     * Retrieve a Model's primary key statically.
175
     *
176
     * @return mixed
177
     */
178
    public static function getPrimaryKeyName()
179
    {
180
        /** @phpstan-ignore-next-line */
181
        return (new static)->getKeyName();
182
    }
183
184
    /**
185
     * Determine if a Model was recently 'created'.
186
     *
187
     * @return bool
188
     */
189
    public function wasCreated(): bool
190
    {
191
        return $this->wasRecentlyCreated;
192
    }
193
194
    /**
195
     * Determine if a Model was recently 'updated'.
196
     *
197
     * @return bool
198
     */
199
    public function wasUpdated(): bool
200
    {
201
        return $this->wasChanged();
202
    }
203
204
    /**
205
     * Determine if a Model was recently 'deleted'.
206
     *
207
     * @return bool
208
     */
209
    public function wasDeleted(): bool
210
    {
211
        if (! $this->exists) {
212
            return true;
213
        }
214
215
        if (array_key_exists('deleted_at', $this->attributes)) {
216
            return ! is_null($this->attributes['deleted_at']);
217
        }
218
219
        return false;
220
    }
221
222
    /**
223
     * Retrieve the most recent Model change.
224
     *
225
     * @return string
226
     */
227
    public function mostRecentChange(): string
228
    {
229
        if ($this->wasCreated()) {
230
            return 'created';
231
        } elseif ($this->wasUpdated()) {
232
            return 'updated';
233
        } elseif ($this->wasDeleted()) {
234
            return 'deleted';
235
        } else {
236
            return 'unchanged';
237
        }
238
    }
239
240
    /**
241
     * Retrieve the 'timestampFormat' property.
242
     *
243
     * @return string
244
     */
245
    public function getTimestampFormat(): string
246
    {
247
        return $this->timestampFormat;
248
    }
249
250
    /**
251
     * Retrieve the 'timestampFormatForHumans' property.
252
     *
253
     * @param  int  $stringToTime
254
     * @return string
255
     */
256
    protected function getDatetimeForHumans(int $stringToTime): string
257
    {
258
        return date('F j, Y', $stringToTime).' at '.date('g:i a', $stringToTime);
259
    }
260
261
    /**
262
     * Retrieve the 'created_at' attribute mutated to human readable datetime.
263
     *
264
     * @return string
265
     */
266
    public function datetime(): string
267
    {
268
        return date('Y-m-d h:i a', strtotime($this->attributes['created_at']));
269
    }
270
271
    /**
272
     * Retrieve the 'created_at' attribute mutated to timestamp string.
273
     *
274
     *  - 'created_timestamp' attribute accessor
275
     *
276
     * @return string
277
     */
278
    public function createdTimestamp(): string
279
    {
280
        return date($this->getTimestampFormat(), strtotime($this->attributes['created_at']));
281
    }
282
283
    /**
284
     * Retrieve the 'created_for_humans' attribute mutated to a timestamp for humans string.
285
     *
286
     *  - 'created_for_humans' attribute accessor
287
     *
288
     * @return string
289
     */
290
    public function createdForHumans(): string
291
    {
292
        return $this->getDatetimeForHumans(strtotime($this->attributes['created_at']));
293
    }
294
295
    /**
296
     * Retrieve the 'created_at' attribute mutated to difference for humans string.
297
     *
298
     *  - 'created_for_humans' attribute accessor
299
     *
300
     * @return string
301
     */
302
    public function createdDiffForHumans(): string
303
    {
304
        return (new Carbon($this->attributes['created_at']))->diffForHumans();
305
    }
306
307
    /**
308
     * Access the 'creation_date' attribute that returns the attribute in 'Y-m-d' format.
309
     *
310
     * @return string
311
     */
312
    public function createdDate(): string
313
    {
314
        return date('Y-m-d', strtotime($this->attributes['created_at']));
315
    }
316
317
    /**
318
     * Access the 'creation_time' attribute that returns the attribute in 'h:i A' format.
319
     *
320
     * @return string
321
     */
322
    public function createdTime(): string
323
    {
324
        return date('h:i A', strtotime($this->attributes['created_at']));
325
    }
326
327
    /**
328
     * Retrieve the 'updated_at' attribute mutated to timestamp string.
329
     *
330
     *  - 'updated_timestamp' attribute accessor
331
     *
332
     * @return string
333
     */
334
    public function updatedTimestamp(): string
335
    {
336
        return date($this->getTimestampFormat(), strtotime($this->attributes['updated_at']));
337
    }
338
339
    /**
340
     * Retrieve the 'updated_for_humans' attribute mutated to a timestamp for humans string.
341
     *
342
     *  - 'updated_for_humans' attribute accessor
343
     *
344
     * @return string
345
     */
346
    public function updatedForHumans(): string
347
    {
348
        return $this->getDatetimeForHumans(strtotime($this->attributes['updated_at']));
349
    }
350
351
    /**
352
     * Retrieve the 'updated_at' attribute mutated to difference for humans string.
353
     *
354
     *  - 'updated_for_humans' attribute accessor
355
     *
356
     * @return string
357
     */
358
    public function updatedDiffForHumans(): string
359
    {
360
        return (new Carbon($this->attributes['updated_at']))->diffForHumans();
361
    }
362
363
    /**
364
     * Access the 'updated_date' attribute that returns the attribute in 'Y-m-d' format.
365
     *
366
     * @return string
367
     */
368
    public function updatedDate(): string
369
    {
370
        return date('Y-m-d', strtotime($this->attributes['updated_at']));
371
    }
372
373
    /**
374
     * Access the 'updated_time' attribute that returns the attribute in 'h:i A' format.
375
     *
376
     * @return string
377
     */
378
    public function updatedTime(): string
379
    {
380
        return date('h:i A', strtotime($this->attributes['updated_at']));
381
    }
382
}
383