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
|
|
|
namespace Cog\Laravel\Ban\Models; |
13
|
|
|
|
14
|
|
|
use Carbon\Carbon; |
15
|
|
|
use Cog\Contracts\Ban\Ban as BanContract; |
16
|
|
|
use Cog\Contracts\Ban\Bannable as BannableContract; |
17
|
|
|
use Illuminate\Database\Eloquent\Builder; |
18
|
|
|
use Illuminate\Database\Eloquent\Model; |
19
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class Ban. |
23
|
|
|
* |
24
|
|
|
* @package Cog\Laravel\Ban\Models |
25
|
|
|
*/ |
26
|
|
|
class Ban extends Model implements BanContract |
27
|
|
|
{ |
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
|
|
|
]; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The attributes that should be cast to native types. |
49
|
|
|
* |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
protected $casts = [ |
53
|
|
|
'expired_at' => 'datetime', |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Expired timestamp mutator. |
58
|
|
|
* |
59
|
|
|
* @param \Carbon\Carbon|string $value |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
|
|
public function setExpiredAtAttribute($value) |
63
|
|
|
{ |
64
|
|
|
if (!is_null($value) && !$value instanceof Carbon) { |
65
|
|
|
$value = Carbon::parse($value); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->attributes['expired_at'] = $value; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Determine if Ban is permanent. |
73
|
|
|
* |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
public function isPermanent() |
77
|
|
|
{ |
78
|
|
|
return !isset($this->attributes['expired_at']) || is_null($this->attributes['expired_at']); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Determine if Ban is temporary. |
83
|
|
|
* |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
public function isTemporary() |
87
|
|
|
{ |
88
|
|
|
return !$this->isPermanent(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Entity responsible for ban. |
93
|
|
|
* |
94
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo |
95
|
|
|
*/ |
96
|
|
|
public function createdBy() |
97
|
|
|
{ |
98
|
|
|
return $this->morphTo('created_by'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Bannable model. |
103
|
|
|
* |
104
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo |
105
|
|
|
*/ |
106
|
|
|
public function bannable() |
107
|
|
|
{ |
108
|
|
|
return $this->morphTo('bannable'); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Scope a query to only include models by owner. |
113
|
|
|
* |
114
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
115
|
|
|
* @param \Cog\Contracts\Ban\Bannable $bannable |
116
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
117
|
|
|
*/ |
118
|
|
|
public function scopeWhereBannable(Builder $query, BannableContract $bannable) |
119
|
|
|
{ |
120
|
|
|
return $query->where([ |
121
|
|
|
'bannable_type' => $bannable->getMorphClass(), |
122
|
|
|
'bannable_id' => $bannable->getKey(), |
123
|
|
|
]); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: