1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pbmedia\Specifications\Laravel\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
6
|
|
|
use Pbmedia\Specifications\Specifications; |
7
|
|
|
|
8
|
|
|
trait HasSpecificationsTrait |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* ScoreModel class name. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $scoreModelClass = ScoreModel::class; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Instance of Specifications. |
19
|
|
|
* |
20
|
|
|
* @var \Pbmedia\Specifications\Specifications |
21
|
|
|
*/ |
22
|
|
|
protected $specifications; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Returns a Specifications object with the Scores and Attributes |
26
|
|
|
* loaded from the database. |
27
|
|
|
* |
28
|
|
|
* @return \Pbmedia\Specifications\Specifications |
29
|
|
|
*/ |
30
|
|
|
public function specifications(): Specifications |
31
|
|
|
{ |
32
|
|
|
if (!$this->specifications) { |
33
|
|
|
$this->specifications = new Specifications; |
34
|
|
|
|
35
|
|
|
$this->getRelationValue('Scores')->load('Attribute')->each(function ($scoreModel) { |
|
|
|
|
36
|
|
|
$this->specifications->add($scoreModel->getAttributeScore()); |
37
|
|
|
}); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $this->specifications; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Defines the relationship with the Scores attached to this model. |
45
|
|
|
* |
46
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
47
|
|
|
*/ |
48
|
|
|
public function Scores(): MorphMany |
49
|
|
|
{ |
50
|
|
|
return $this->morphMany($this->scoreModelClass, 'specifiable'); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Binds a callback to the 'saved' event of the model which syncs the |
55
|
|
|
* AttributeScore objects in the Specifications instance with |
56
|
|
|
* ones in the database. |
57
|
|
|
*/ |
58
|
|
|
public static function bootHasSpecificationsTrait() |
59
|
|
|
{ |
60
|
|
|
static::saved(function ($canBeSpecified) { |
61
|
|
|
$scoreIds = $canBeSpecified->Scores->pluck('id'); |
62
|
|
|
|
63
|
|
|
if ($scoreIds->count() > 0) { |
64
|
|
|
app($this->scoreModelClass)->whereIn('id', $scoreIds)->delete(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$attributScoreCollection = $canBeSpecified->specifications()->all(); |
68
|
|
|
|
69
|
|
|
if ($attributScoreCollection->isEmpty()) { |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$attributScoreCollection->map(function ($attributeScore) { |
74
|
|
|
return [ |
75
|
|
|
'attribute_id' => $attributeScore->getAttribute()->id, |
76
|
|
|
'value' => $attributeScore->getScoreValue(), |
77
|
|
|
]; |
78
|
|
|
})->each(function ($scoreModelAttributes) use ($canBeSpecified) { |
79
|
|
|
$canBeSpecified->Scores()->create($scoreModelAttributes); |
80
|
|
|
}); |
81
|
|
|
}); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.