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