Passed
Push — master ( 2b592d...3aa34a )
by Stephen
54s queued 11s
created

Model::getTimestampFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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');
0 ignored issues
show
Bug introduced by
$this->created_at of type DateTime is incompatible with the type string expected by parameter $datetime of strtotime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
        return strtotime(/** @scrutinizer ignore-type */ $this->created_at) >= strtotime('-'.self::IS_NEW_MAX_HOURS.' hours');
Loading history...
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));
0 ignored issues
show
Bug introduced by
$this->created_at of type DateTime is incompatible with the type string expected by parameter $datetime of strtotime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
        return round((time() - strtotime(/** @scrutinizer ignore-type */ $this->created_at)) / (60 * 60 * 24));
Loading history...
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());
0 ignored issues
show
Bug introduced by
It seems like $this->getKey() can also be of type boolean and null; however, parameter $string of crc32() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

129
        return crc32(/** @scrutinizer ignore-type */ $this->getKey());
Loading history...
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 'created_at' attribute mutated to human readable datetime.
222
     *
223
     * @return string
224
     */
225
    public function getDatetimeAttribute(): string
226
    {
227
        return date('Y-m-d h:i a', strtotime($this->created_at));
0 ignored issues
show
Bug introduced by
$this->created_at of type DateTime is incompatible with the type string expected by parameter $datetime of strtotime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

227
        return date('Y-m-d h:i a', strtotime(/** @scrutinizer ignore-type */ $this->created_at));
Loading history...
228
    }
229
230
    /**
231
     * Retrieve the 'created_at' attribute mutated to timestamp string.
232
     *
233
     *  - 'created_timestamp' attribute accessor
234
     *
235
     * @return string
236
     */
237
    public function getCreatedTimestampAttribute(): string
238
    {
239
        return date($this->getTimestampFormat(), strtotime($this->created_at));
0 ignored issues
show
Bug introduced by
$this->created_at of type DateTime is incompatible with the type string expected by parameter $datetime of strtotime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

239
        return date($this->getTimestampFormat(), strtotime(/** @scrutinizer ignore-type */ $this->created_at));
Loading history...
240
    }
241
242
    /**
243
     * Retrieve the 'created_at' attribute mutated to difference for humans string.
244
     *
245
     *  - 'created_for_humans' attribute accessor
246
     *
247
     * @return string
248
     */
249
    public function getCreatedForHumansAttribute(): string
250
    {
251
        return $this->created_at->diffForHumans();
0 ignored issues
show
Bug introduced by
The method diffForHumans() does not exist on DateTime. It seems like you code against a sub-type of said class. However, the method does not exist in pq\DateTime. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

251
        return $this->created_at->/** @scrutinizer ignore-call */ diffForHumans();
Loading history...
252
    }
253
254
    /**
255
     * Access the 'creation_date' attribute that returns the attribute in 'Y-m-d' format.
256
     *
257
     * @return string
258
     */
259
    public function getCreatedDateAttribute(): string
260
    {
261
        return date('Y-m-d', strtotime($this->created_at));
0 ignored issues
show
Bug introduced by
$this->created_at of type DateTime is incompatible with the type string expected by parameter $datetime of strtotime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

261
        return date('Y-m-d', strtotime(/** @scrutinizer ignore-type */ $this->created_at));
Loading history...
262
    }
263
264
    /**
265
     * Access the 'creation_time' attribute that returns the attribute in 'h:i A' format.
266
     *
267
     * @return string
268
     */
269
    public function getCreatedTimeAttribute(): string
270
    {
271
        return date('h:i A', strtotime($this->created_at));
0 ignored issues
show
Bug introduced by
$this->created_at of type DateTime is incompatible with the type string expected by parameter $datetime of strtotime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

271
        return date('h:i A', strtotime(/** @scrutinizer ignore-type */ $this->created_at));
Loading history...
272
    }
273
274
    /**
275
     * Retrieve the 'updated_at' attribute mutated to timestamp string.
276
     *
277
     *  - 'updated_timestamp' attribute accessor
278
     *
279
     * @return string
280
     */
281
    public function getUpdatedTimestampAttribute(): string
282
    {
283
        return date($this->getTimestampFormat(), strtotime($this->updated_at));
0 ignored issues
show
Bug introduced by
$this->updated_at of type DateTime is incompatible with the type string expected by parameter $datetime of strtotime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

283
        return date($this->getTimestampFormat(), strtotime(/** @scrutinizer ignore-type */ $this->updated_at));
Loading history...
284
    }
285
286
    /**
287
     * Retrieve the 'updated_at' attribute mutated to difference for humans string.
288
     *
289
     *  - 'updated_for_humans' attribute accessor
290
     *
291
     * @return string
292
     */
293
    public function getUpdatedForHumansAttribute(): string
294
    {
295
        return $this->updated_at->diffForHumans();
296
    }
297
298
    /**
299
     * Access the 'updated_date' attribute that returns the attribute in 'Y-m-d' format.
300
     *
301
     * @return string
302
     */
303
    public function getUpdatedDateAttribute(): string
304
    {
305
        return date('Y-m-d', strtotime($this->updated_at));
0 ignored issues
show
Bug introduced by
$this->updated_at of type DateTime is incompatible with the type string expected by parameter $datetime of strtotime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

305
        return date('Y-m-d', strtotime(/** @scrutinizer ignore-type */ $this->updated_at));
Loading history...
306
    }
307
308
    /**
309
     * Access the 'updated_time' attribute that returns the attribute in 'h:i A' format.
310
     *
311
     * @return string
312
     */
313
    public function getUpdatedTimeAttribute(): string
314
    {
315
        return date('h:i A', strtotime($this->updated_at));
0 ignored issues
show
Bug introduced by
$this->updated_at of type DateTime is incompatible with the type string expected by parameter $datetime of strtotime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

315
        return date('h:i A', strtotime(/** @scrutinizer ignore-type */ $this->updated_at));
Loading history...
316
    }
317
}
318