1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Rinvex\Testimonials\Models; |
6
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Rinvex\Cacheable\CacheableEloquent; |
9
|
|
|
use Illuminate\Database\Eloquent\Builder; |
10
|
|
|
use Rinvex\Support\Traits\ValidatingTrait; |
11
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo; |
12
|
|
|
use Rinvex\Testimonials\Contracts\TestimonialContract; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Rinvex\Testimonials\Models\Testimonial. |
16
|
|
|
* |
17
|
|
|
* @property int $id |
18
|
|
|
* @property int $subject_id |
19
|
|
|
* @property string $subject_type |
20
|
|
|
* @property int $attestant_id |
21
|
|
|
* @property string $attestant_type |
22
|
|
|
* @property bool $is_approved |
23
|
|
|
* @property string $body |
24
|
|
|
* @property \Carbon\Carbon|null $created_at |
25
|
|
|
* @property \Carbon\Carbon|null $updated_at |
26
|
|
|
* @property \Carbon\Carbon|null $deleted_at |
27
|
|
|
* @property-read \Illuminate\Database\Eloquent\Model|\Eloquent $attestant |
28
|
|
|
* @property-read \Illuminate\Database\Eloquent\Model|\Eloquent $subject |
29
|
|
|
* |
30
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Testimonials\Models\Testimonial approved() |
31
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Testimonials\Models\Testimonial disapproved() |
32
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Testimonials\Models\Testimonial whereAttestantId($value) |
33
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Testimonials\Models\Testimonial whereAttestantType($value) |
|
|
|
|
34
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Testimonials\Models\Testimonial whereBody($value) |
35
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Testimonials\Models\Testimonial whereCreatedAt($value) |
36
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Testimonials\Models\Testimonial whereDeletedAt($value) |
37
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Testimonials\Models\Testimonial whereId($value) |
38
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Testimonials\Models\Testimonial whereIsApproved($value) |
39
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Testimonials\Models\Testimonial whereSubjectId($value) |
40
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Testimonials\Models\Testimonial whereSubjectType($value) |
41
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Testimonials\Models\Testimonial whereUpdatedAt($value) |
42
|
|
|
* @mixin \Eloquent |
43
|
|
|
*/ |
44
|
|
|
class Testimonial extends Model implements TestimonialContract |
45
|
|
|
{ |
46
|
|
|
use ValidatingTrait; |
47
|
|
|
use CacheableEloquent; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
protected $fillable = [ |
53
|
|
|
'subject_id', |
54
|
|
|
'subject_type', |
55
|
|
|
'attestant_id', |
56
|
|
|
'attestant_type', |
57
|
|
|
'is_approved', |
58
|
|
|
'body', |
59
|
|
|
]; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
protected $casts = [ |
65
|
|
|
'subject_id' => 'integer', |
66
|
|
|
'subject_type' => 'string', |
67
|
|
|
'attestant_id' => 'integer', |
68
|
|
|
'attestant_type' => 'string', |
69
|
|
|
'is_approved' => 'boolean', |
70
|
|
|
'body' => 'string', |
71
|
|
|
'deleted_at' => 'datetime', |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
protected $observables = [ |
78
|
|
|
'validating', |
79
|
|
|
'validated', |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* The default rules that the model will validate against. |
84
|
|
|
* |
85
|
|
|
* @var array |
86
|
|
|
*/ |
87
|
|
|
protected $rules = [ |
88
|
|
|
'subject_id' => 'required|integer', |
89
|
|
|
'subject_type' => 'required|string|max:150', |
90
|
|
|
'attestant_id' => 'required|integer', |
91
|
|
|
'attestant_type' => 'required|string|max:150', |
92
|
|
|
'is_approved' => 'sometimes|boolean', |
93
|
|
|
'body' => 'required|string|max:150', |
94
|
|
|
]; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Whether the model should throw a |
98
|
|
|
* ValidationException if it fails validation. |
99
|
|
|
* |
100
|
|
|
* @var bool |
101
|
|
|
*/ |
102
|
|
|
protected $throwValidationExceptions = true; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Create a new Eloquent model instance. |
106
|
|
|
* |
107
|
|
|
* @param array $attributes |
108
|
|
|
*/ |
109
|
|
|
public function __construct(array $attributes = []) |
110
|
|
|
{ |
111
|
|
|
parent::__construct($attributes); |
112
|
|
|
|
113
|
|
|
$this->setTable(config('rinvex.testimonials.tables.testimonials')); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get testimonials of the given subject. |
118
|
|
|
* |
119
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
120
|
|
|
* @param \Illuminate\Database\Eloquent\Model $subject |
121
|
|
|
* |
122
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
123
|
|
|
*/ |
124
|
|
|
public function scopeOfSubject(Builder $builder, Model $subject): Builder |
125
|
|
|
{ |
126
|
|
|
return $builder->where('subject_type', $subject->getMorphClass())->where('subject_id', $subject->getKey()); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get testimonials of the given attestant. |
131
|
|
|
* |
132
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
133
|
|
|
* @param \Illuminate\Database\Eloquent\Model $attestant |
134
|
|
|
* |
135
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
136
|
|
|
*/ |
137
|
|
|
public function scopeOfAttestant(Builder $builder, Model $attestant): Builder |
138
|
|
|
{ |
139
|
|
|
return $builder->where('attestant_type', $attestant->getMorphClass())->where('attestant_id', $attestant->getKey()); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Get the approved testimonials. |
144
|
|
|
* |
145
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
146
|
|
|
* |
147
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
148
|
|
|
*/ |
149
|
|
|
public function scopeApproved(Builder $builder): Builder |
150
|
|
|
{ |
151
|
|
|
return $builder->where('is_approved', true); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Get the disapproved testimonials. |
156
|
|
|
* |
157
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
158
|
|
|
* |
159
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
160
|
|
|
*/ |
161
|
|
|
public function scopeDisapproved(Builder $builder): Builder |
162
|
|
|
{ |
163
|
|
|
return $builder->where('is_approved', false); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Get the subject model of the testimonial. |
168
|
|
|
* |
169
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo |
170
|
|
|
*/ |
171
|
|
|
public function subject(): MorphTo |
172
|
|
|
{ |
173
|
|
|
return $this->morphTo(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Get the attestant model of the testimonial. |
178
|
|
|
* |
179
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo |
180
|
|
|
*/ |
181
|
|
|
public function attestant(): MorphTo |
182
|
|
|
{ |
183
|
|
|
return $this->morphTo(); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Approve the testimonial. |
188
|
|
|
* |
189
|
|
|
* @return $this |
190
|
|
|
*/ |
191
|
|
|
public function approve() |
192
|
|
|
{ |
193
|
|
|
$this->update(['is_approved' => true]); |
194
|
|
|
|
195
|
|
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Disapprove the testimonial. |
200
|
|
|
* |
201
|
|
|
* @return $this |
202
|
|
|
*/ |
203
|
|
|
public function disapprove() |
204
|
|
|
{ |
205
|
|
|
$this->update(['is_approved' => false]); |
206
|
|
|
|
207
|
|
|
return $this; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.