1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Laravel Ban. |
5
|
|
|
* |
6
|
|
|
* (c) Anton Komarev <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Cog\Laravel\Ban\Models; |
15
|
|
|
|
16
|
|
|
use Cog\Contracts\Ban\Ban as BanContract; |
17
|
|
|
use Cog\Contracts\Ban\Bannable as BannableContract; |
18
|
|
|
use Illuminate\Database\Eloquent\Builder; |
19
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory; |
20
|
|
|
use Illuminate\Database\Eloquent\Model; |
21
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo; |
22
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
23
|
|
|
use Illuminate\Support\Carbon; |
24
|
|
|
|
25
|
|
|
class Ban extends Model implements BanContract |
26
|
|
|
{ |
27
|
|
|
use HasFactory; |
28
|
|
|
use SoftDeletes; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The table associated with the model. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $table = 'bans'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The attributes that are mass assignable. |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $fillable = [ |
43
|
|
|
'comment', |
44
|
|
|
'expired_at', |
45
|
|
|
'created_by_type', |
46
|
|
|
'created_by_id', |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The attributes that should be cast to native types. |
51
|
|
|
* |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
protected $casts = [ |
55
|
|
|
'expired_at' => 'datetime', |
56
|
|
|
'deleted_at' => 'datetime', |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Expired timestamp mutator. |
61
|
|
|
* |
62
|
|
|
* @param \Illuminate\Support\Carbon|string $value |
63
|
|
|
* @return void |
64
|
|
|
*/ |
65
|
|
|
public function setExpiredAtAttribute($value): void |
66
|
|
|
{ |
67
|
|
|
if (!is_null($value) && !$value instanceof Carbon) { |
68
|
|
|
$value = Carbon::parse($value); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$this->attributes['expired_at'] = $value; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Entity responsible for ban. |
76
|
|
|
* |
77
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo |
78
|
|
|
*/ |
79
|
|
|
public function createdBy(): MorphTo |
80
|
|
|
{ |
81
|
|
|
return $this->morphTo('created_by'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Bannable model. |
86
|
|
|
* |
87
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo |
88
|
|
|
*/ |
89
|
|
|
public function bannable(): MorphTo |
90
|
|
|
{ |
91
|
|
|
return $this->morphTo('bannable'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Determine if Ban is permanent. |
96
|
|
|
* |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
public function isPermanent(): bool |
100
|
|
|
{ |
101
|
|
|
return !isset($this->attributes['expired_at']) || is_null($this->attributes['expired_at']); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Determine if Ban is temporary. |
106
|
|
|
* |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
|
|
public function isTemporary(): bool |
110
|
|
|
{ |
111
|
|
|
return !$this->isPermanent(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Scope a query to only include models by owner. |
116
|
|
|
* |
117
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
118
|
|
|
* @param \Cog\Contracts\Ban\Bannable $bannable |
119
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
120
|
|
|
*/ |
121
|
|
|
public function scopeWhereBannable(Builder $query, BannableContract $bannable): Builder |
122
|
|
|
{ |
123
|
|
|
return $query->where([ |
124
|
|
|
'bannable_type' => $bannable->getMorphClass(), |
|
|
|
|
125
|
|
|
'bannable_id' => $bannable->getKey(), |
|
|
|
|
126
|
|
|
]); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|