1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pbmedia\Specifications; |
4
|
|
|
|
5
|
|
|
use Countable; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Pbmedia\Specifications\AttributeScore; |
8
|
|
|
use Pbmedia\Specifications\Interfaces\Attribute; |
9
|
|
|
use Pbmedia\Specifications\Interfaces\Score; |
10
|
|
|
use Pbmedia\Specifications\Interfaces\Specifications as SpecificationsInterface; |
11
|
|
|
|
12
|
|
|
class Specifications implements SpecificationsInterface, Countable |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Collection instance. |
16
|
|
|
* |
17
|
|
|
* @var \Illuminate\Support\Collection |
18
|
|
|
*/ |
19
|
|
|
protected $specifications; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Create a new Specifications instance and instantiates a new Collection. |
23
|
|
|
*/ |
24
|
|
|
public function __construct() |
25
|
|
|
{ |
26
|
|
|
$this->specifications = new Collection; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritDoc} |
31
|
|
|
*/ |
32
|
|
|
public function add(AttributeScore $attributeScore): SpecificationsInterface |
33
|
|
|
{ |
34
|
|
|
$key = $attributeScore->getAttribute()->getIdentifier(); |
35
|
|
|
|
36
|
|
|
$this->specifications[$key] = $attributeScore; |
37
|
|
|
|
38
|
|
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritDoc} |
43
|
|
|
*/ |
44
|
|
|
public function addMany(array $attributeScores = []): SpecificationsInterface |
45
|
|
|
{ |
46
|
|
|
Collection::make($attributeScores)->each(function (AttributeScore $attributeScore) { |
47
|
|
|
$this->add($attributeScore); |
48
|
|
|
}); |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritDoc} |
55
|
|
|
*/ |
56
|
|
|
public function set(Attribute $attribute, Score $score): SpecificationsInterface |
57
|
|
|
{ |
58
|
|
|
$this->add(new AttributeScore($attribute, $score)); |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritDoc} |
65
|
|
|
*/ |
66
|
|
|
public function has(Attribute $attribute): bool |
67
|
|
|
{ |
68
|
|
|
$key = $attribute->getIdentifier(); |
69
|
|
|
|
70
|
|
|
return $this->specifications->has($key); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritDoc} |
75
|
|
|
*/ |
76
|
|
|
public function get(Attribute $attribute): AttributeScore |
77
|
|
|
{ |
78
|
|
|
$key = $attribute->getIdentifier(); |
79
|
|
|
|
80
|
|
|
return $this->specifications->get($key); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritDoc} |
85
|
|
|
*/ |
86
|
|
|
public function forget(Attribute $attribute): SpecificationsInterface |
87
|
|
|
{ |
88
|
|
|
$key = $attribute->getIdentifier(); |
89
|
|
|
|
90
|
|
|
$this->specifications->forget($key); |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritDoc} |
97
|
|
|
*/ |
98
|
|
|
public function all(): Collection |
99
|
|
|
{ |
100
|
|
|
return $this->specifications; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritDoc} |
105
|
|
|
*/ |
106
|
|
|
public function count(): int |
107
|
|
|
{ |
108
|
|
|
return $this->specifications->count(); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|