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 GivesTestimonials |
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 GivesTestimonials trait for the model. |
37
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
public static function bootGivesTestimonials() |
41
|
|
|
{ |
42
|
|
|
static::deleted(function (self $model) { |
43
|
|
|
$model->recommendations()->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 recommendations(): MorphMany |
53
|
|
|
{ |
54
|
|
|
return $this->morphMany(config('rinvex.testimonials.models.testimonial'), 'attestant'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get recommendations for the given subject. |
59
|
|
|
* |
60
|
|
|
* @param \Illuminate\Database\Eloquent\Model $subject |
61
|
|
|
* |
62
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
63
|
|
|
*/ |
64
|
|
|
public function recommendationsOf(Model $subject): MorphMany |
65
|
|
|
{ |
66
|
|
|
return $this->recommendations()->where('subject_type', $subject->getMorphClass())->where('subject_id', $subject->getKey()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Add new testimonial for the given subject. |
71
|
|
|
* |
72
|
|
|
* @param \Illuminate\Database\Eloquent\Model $subject |
73
|
|
|
* @param string $body |
74
|
|
|
* |
75
|
|
|
* @return \Rinvex\Testimonials\Models\Testimonial |
76
|
|
|
*/ |
77
|
|
|
public function newTestimonial(Model $subject, string $body): Testimonial |
78
|
|
|
{ |
79
|
|
|
return $this->recommendations()->create([ |
80
|
|
|
'body' => $body, |
81
|
|
|
'subject_id' => $subject->getKey(), |
82
|
|
|
'subject_type' => $subject->getMorphClass(), |
83
|
|
|
'attestant_id' => static::getKey(), |
84
|
|
|
'attestant_type' => static::getMorphClass(), |
85
|
|
|
]); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
This check looks for
@param
annotations 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.