1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pbmedia\Specifications\Laravel\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
6
|
|
|
use Pbmedia\Specifications\AttributeScore; |
7
|
|
|
use Pbmedia\Specifications\Interfaces\CanBeSpecified; |
8
|
|
|
use Pbmedia\Specifications\Specifications; |
9
|
|
|
|
10
|
|
|
trait HasSpecificationsTrait |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* ScoreModel class name. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected static $scoreModelClass = ScoreModel::class; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Instance of Specifications. |
21
|
|
|
* |
22
|
|
|
* @var \Pbmedia\Specifications\Specifications |
23
|
|
|
*/ |
24
|
|
|
protected $specifications; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Returns a Specifications object with the Scores and Attributes |
28
|
|
|
* loaded from the database. |
29
|
|
|
* |
30
|
|
|
* @return \Pbmedia\Specifications\Specifications |
31
|
|
|
*/ |
32
|
|
|
public function specifications(): Specifications |
33
|
|
|
{ |
34
|
|
|
if (!$this->specifications) { |
35
|
|
|
$this->specifications = new Specifications; |
36
|
|
|
|
37
|
|
|
$this->getRelationValue('Scores')->load('Attribute')->each(function ($scoreModel) { |
|
|
|
|
38
|
|
|
$this->specifications->add($scoreModel->getAttributeScore()); |
39
|
|
|
}); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return $this->specifications; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Defines the relationship with the Scores attached to this model. |
47
|
|
|
* |
48
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
49
|
|
|
*/ |
50
|
|
|
public function Scores(): MorphMany |
51
|
|
|
{ |
52
|
|
|
return $this->morphMany(static::$scoreModelClass, 'specifiable'); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Binds a callback to the 'saved' event of the model which syncs the |
57
|
|
|
* AttributeScore objects in the Specifications instance with |
58
|
|
|
* ones in the database. |
59
|
|
|
*/ |
60
|
|
|
public static function bootHasSpecificationsTrait() |
61
|
|
|
{ |
62
|
|
|
static::saved(function (CanBeSpecified $canBeSpecified) { |
63
|
|
|
$scoreIds = $canBeSpecified->getRelationValue('Scores')->pluck('id'); |
|
|
|
|
64
|
|
|
|
65
|
|
|
if ($scoreIds->count() > 0) { |
66
|
|
|
app(static::$scoreModelClass)->whereIn('id', $scoreIds)->delete(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$attributScoreCollection = $canBeSpecified->specifications()->all(); |
70
|
|
|
|
71
|
|
|
if ($attributScoreCollection->isEmpty()) { |
72
|
|
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$attributScoreCollection->map(function (AttributeScore $attributeScore) { |
76
|
|
|
return [ |
77
|
|
|
'attribute_id' => $attributeScore->getAttribute()->id, |
|
|
|
|
78
|
|
|
'value' => $attributeScore->getScoreValue(), |
79
|
|
|
]; |
80
|
|
|
})->each(function (array $scoreModelAttributes) use ($canBeSpecified) { |
81
|
|
|
$canBeSpecified->Scores()->create($scoreModelAttributes); |
|
|
|
|
82
|
|
|
}); |
83
|
|
|
}); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
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.