Passed
Push — master ( 93d364...d97c69 )
by Stephen
07:30
created

AbstractModel::getCreatedTimestampAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Sfneal\Models;
4
5
use DateTimeInterface;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\SoftDeletes;
9
use Sfneal\Builders\QueryBuilder;
10
use Sfneal\Models\Traits\UploadDirectory;
11
12
abstract class AbstractModel extends Model
13
{
14
    use SoftDeletes,
15
        UploadDirectory;
16
17
    /**
18
     * Retrieve the datetime format used in the timestamp attribute accessors
19
     *
20
     * @return string
21
     */
22
    protected $timestampFormat = 'm/d/Y 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 $time 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 false|float
76
     */
77
    public function howNew()
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 $time 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->attributes) &&
108
            ($is_fillable ? array_key_exists($attr, $this->fillable) : true);
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()
128
    {
129
        return crc32($this->getKey());
130
    }
131
132
    /**
133
     * Retrieve the 'created_at' attribute mutated to human readable datetime.
134
     *
135
     * @return string
136
     */
137
    public function getDatetimeAttribute(): string
138
    {
139
        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 $time 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

139
        return date('Y-m-d h:i a', strtotime(/** @scrutinizer ignore-type */ $this->created_at));
Loading history...
140
    }
141
142
    /**
143
     * Returns the default 'label' for a model instance.
144
     *
145
     * @return mixed
146
     */
147
    public function getLabel()
148
    {
149
        return $this->getKey();
150
    }
151
152
    /**
153
     * Retrieve a Model's table name statically.
154
     *
155
     * @return mixed
156
     */
157
    public static function getTableName()
158
    {
159
        return with(new static)->getTable();
160
    }
161
162
    /**
163
     * Retrieve a Model's primary key statically.
164
     *
165
     * @return mixed
166
     */
167
    public static function getPrimaryKeyName()
168
    {
169
        return with(new static)->getKeyName();
170
    }
171
172
    /**
173
     * Determine if a Model was recently 'created'.
174
     *
175
     * @return bool
176
     */
177
    public function wasCreated(): bool
178
    {
179
        return $this->wasRecentlyCreated;
180
    }
181
182
    /**
183
     * Determine if a Model was recently 'updated'.
184
     *
185
     * @return bool
186
     */
187
    public function wasUpdated(): bool
188
    {
189
        return $this->wasChanged();
190
    }
191
192
    /**
193
     * Determine if a Model was recently 'deleted'.
194
     *
195
     * @return bool
196
     */
197
    public function wasDeleted(): bool
198
    {
199
        return ! $this->exists || ! is_null($this->deleted_at);
200
    }
201
202
    /**
203
     * Retrieve the most recent Model change.
204
     *
205
     * @return string
206
     */
207
    public function mostRecentChange(): string
208
    {
209
        if ($this->wasCreated()) {
210
            return 'created';
211
        } elseif ($this->wasUpdated()) {
212
            return 'updated';
213
        } elseif ($this->wasDeleted()) {
214
            return 'deleted';
215
        } else {
216
            return 'unchanged';
217
        }
218
    }
219
220
    /**
221
     * Retrieve the 'updated_at' attribute mutated to timestamp string
222
     *
223
     *  - 'updated_timestamp' attribute accessor
224
     *
225
     * @return string
226
     */
227
    public function getUpdatedTimestampAttribute(): string
228
    {
229
        return date($this->timestampFormat, 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 $time 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

229
        return date($this->timestampFormat, strtotime(/** @scrutinizer ignore-type */ $this->updated_at));
Loading history...
230
    }
231
232
    /**
233
     * Retrieve the 'created_at' attribute mutated to timestamp string
234
     *
235
     *  - 'created_timestamp' attribute accessor
236
     *
237
     * @return string
238
     */
239
    public function getCreatedTimestampAttribute(): string
240
    {
241
        return date($this->timestampFormat, 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 $time 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

241
        return date($this->timestampFormat, strtotime(/** @scrutinizer ignore-type */ $this->created_at));
Loading history...
242
    }
243
244
    /**
245
     * Retrieve the 'updated_at' attribute mutated to difference for humans string
246
     *
247
     *  - 'updated_for_humans' attribute accessor
248
     *
249
     * @return string
250
     */
251
    public function getUpdatedForHumansAttribute(): string
252
    {
253
        return $this->updated_at->diffForHumans();
0 ignored issues
show
introduced by
The method diffForHumans() does not exist on DateTime. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

253
        return $this->updated_at->/** @scrutinizer ignore-call */ diffForHumans();
Loading history...
254
    }
255
256
    /**
257
     * Retrieve the 'created_at' attribute mutated to difference for humans string
258
     *
259
     *  - 'created_for_humans' attribute accessor
260
     *
261
     * @return string
262
     */
263
    public function getCreatedForHumansAttribute(): string
264
    {
265
        return $this->created_at->diffForHumans();
266
    }
267
}
268