|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Rinvex\Testimonials\Traits; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
8
|
|
|
use Rinvex\Testimonials\Models\Testimonial; |
|
9
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
|
10
|
|
|
|
|
11
|
|
|
trait TakesTestimonials |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Register a deleted model event with the dispatcher. |
|
15
|
|
|
* |
|
16
|
|
|
* @param \Closure|string $callback |
|
17
|
|
|
* |
|
18
|
|
|
* @return void |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract public static function deleted($callback); |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Define a polymorphic one-to-many relationship. |
|
24
|
|
|
* |
|
25
|
|
|
* @param string $related |
|
26
|
|
|
* @param string $name |
|
27
|
|
|
* @param string $type |
|
|
|
|
|
|
28
|
|
|
* @param string $id |
|
|
|
|
|
|
29
|
|
|
* @param string $localKey |
|
|
|
|
|
|
30
|
|
|
* |
|
31
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
|
32
|
|
|
*/ |
|
33
|
|
|
abstract public function morphMany($related, $name, $type = null, $id = null, $localKey = null); |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Boot the TakesTestimonials trait for the model. |
|
37
|
|
|
* |
|
38
|
|
|
* @return void |
|
39
|
|
|
*/ |
|
40
|
|
|
public static function bootTakesTestimonials() |
|
41
|
|
|
{ |
|
42
|
|
|
static::deleted(function (self $model) { |
|
43
|
|
|
$model->testimonials()->delete(); |
|
44
|
|
|
}); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Get all attached testimonials to the model. |
|
49
|
|
|
* |
|
50
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
|
51
|
|
|
*/ |
|
52
|
|
|
public function testimonials(): MorphMany |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->morphMany(config('rinvex.testimonials.models.testimonial'), 'attestant'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get testimonials for the given attestant. |
|
59
|
|
|
* |
|
60
|
|
|
* @param \Illuminate\Database\Eloquent\Model $attestant |
|
61
|
|
|
* |
|
62
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
|
63
|
|
|
*/ |
|
64
|
|
|
public function testimonialsOf(Model $attestant): MorphMany |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->testimonials()->where('attestant_type', $attestant->getMorphClass())->where('attestant_id', $attestant->getKey()); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Add new testimonial for the given attestant. |
|
71
|
|
|
* |
|
72
|
|
|
* @param \Illuminate\Database\Eloquent\Model $attestant |
|
73
|
|
|
* @param string $body |
|
74
|
|
|
* |
|
75
|
|
|
* @return \Rinvex\Testimonials\Models\Testimonial |
|
76
|
|
|
*/ |
|
77
|
|
|
public function newTestimonial(Model $attestant, string $body): Testimonial |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->testimonials()->create([ |
|
80
|
|
|
'body' => $body, |
|
81
|
|
|
'subject_id' => static::getKey(), |
|
82
|
|
|
'subject_type' => static::getMorphClass(), |
|
83
|
|
|
'attestant_id' => $attestant->getKey(), |
|
84
|
|
|
'attestant_type' => $attestant->getMorphClass(), |
|
85
|
|
|
]); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.