1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of laravel.su package. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace App\Models; |
12
|
|
|
|
13
|
|
|
use Carbon\Carbon; |
14
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo; |
15
|
|
|
use Illuminate\Support\Str; |
16
|
|
|
use Illuminate\Database\Eloquent\Model; |
17
|
|
|
use Illuminate\Database\Eloquent\Builder; |
18
|
|
|
use Illuminate\Contracts\Auth\Authenticatable; |
19
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
20
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Article. |
24
|
|
|
*/ |
25
|
|
|
class Article extends Model |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Published date and time field name. |
29
|
|
|
*/ |
30
|
|
|
private const PUBLISHED_AT = 'published_at'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Directory of article images. |
34
|
|
|
*/ |
35
|
|
|
public const DEFAULT_IMAGE_PATH = '/static/articles/'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $dates = [ |
41
|
|
|
self::PUBLISHED_AT, |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $table = 'articles'; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
protected $fillable = [ |
53
|
|
|
'user_id', 'title', 'image', 'content_source', |
54
|
|
|
'status', 'published_at', |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param Builder $builder |
59
|
|
|
* @return Builder |
60
|
|
|
*/ |
61
|
|
|
public static function scopeLatestPublished(Builder $builder): Builder |
62
|
|
|
{ |
63
|
|
|
return $builder |
64
|
|
|
->with('user') |
65
|
|
|
->with('tags') |
66
|
|
|
->latest('published_at') |
67
|
|
|
->published(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param Builder $builder |
72
|
|
|
* @return Builder |
73
|
|
|
*/ |
74
|
|
|
public static function scopeLatest(Builder $builder): Builder |
75
|
|
|
{ |
76
|
|
|
return $builder->orderBy('published_at', 'desc'); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param Builder $builder |
81
|
|
|
* @return Builder |
82
|
|
|
*/ |
83
|
|
|
public static function scopePublished(Builder $builder): Builder |
84
|
|
|
{ |
85
|
|
|
return $builder |
86
|
|
|
->where('published_at', '<=', Carbon::now()) |
87
|
|
|
->where('status', Article\Status::PUBLISHED); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param Builder $builder |
92
|
|
|
* @return Builder |
93
|
|
|
*/ |
94
|
|
|
public static function scopePublishedByBot(Builder $builder): Builder |
95
|
|
|
{ |
96
|
|
|
return $builder |
97
|
|
|
->where('user_type', Bot::class); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param Authenticatable|User|null $user |
102
|
|
|
* @return bool |
103
|
|
|
*/ |
104
|
|
|
public function isAllowedForUser(?Authenticatable $user): bool |
105
|
|
|
{ |
106
|
|
|
$isAuthor = $user === null ? false : ($this->user->id === $user->getAuthIdentifier()); |
|
|
|
|
107
|
|
|
|
108
|
|
|
$isPublished = $this->status === Article\Status::PUBLISHED; |
|
|
|
|
109
|
|
|
|
110
|
|
|
$isAllowPublishedTime = $this->published_at <= Carbon::now(); |
|
|
|
|
111
|
|
|
|
112
|
|
|
return $isAuthor || ($isPublished && $isAllowPublishedTime); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public function getImageUrlAttribute(): string |
119
|
|
|
{ |
120
|
|
|
if (Str::startsWith($this->image, ['http:', 'https:', '//'])) { |
|
|
|
|
121
|
|
|
return $this->image; |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return static::DEFAULT_IMAGE_PATH . $this->image; |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
public function getCapitalizeTitleAttribute(): string |
131
|
|
|
{ |
132
|
|
|
return Str::ucfirst($this->title); |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
public function getNicePublishedDateAttribute(): string |
139
|
|
|
{ |
140
|
|
|
if ($this->published_at > Carbon::now()->subMonth()) { |
|
|
|
|
141
|
|
|
return $this->published_at->diffForHumans(); |
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $this->published_at->toDateTimeString(); |
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return MorphTo |
149
|
|
|
*/ |
150
|
|
|
public function user(): MorphTo |
151
|
|
|
{ |
152
|
|
|
return $this->morphTo(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return BelongsToMany |
157
|
|
|
*/ |
158
|
|
|
public function tags(): BelongsToMany |
159
|
|
|
{ |
160
|
|
|
return $this->belongsToMany(Tag::class, 'article_tags'); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.