1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3; |
4
|
|
|
|
5
|
|
|
use CultureFeed_Cdb_Data_Keyword; |
6
|
|
|
|
7
|
|
|
class LabelCollection implements \Countable |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var Label[] |
11
|
|
|
*/ |
12
|
|
|
private $labels; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @param Label[] $labels |
16
|
|
|
*/ |
17
|
|
|
public function __construct(array $labels = []) |
18
|
|
|
{ |
19
|
|
|
array_walk( |
20
|
|
|
$labels, |
21
|
|
|
function ($item) { |
22
|
|
|
if (!$item instanceof Label) { |
23
|
|
|
throw new \InvalidArgumentException( |
24
|
|
|
'Argument $labels should only contain members of type Label' |
25
|
|
|
); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
); |
29
|
|
|
|
30
|
|
|
$this->labels = array_values($labels); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param Label $label |
35
|
|
|
* @return LabelCollection |
36
|
|
|
*/ |
37
|
|
|
public function with(Label $label) |
38
|
|
|
{ |
39
|
|
|
if (!$this->contains($label)) { |
40
|
|
|
$labels = array_merge($this->labels, [$label]); |
41
|
|
|
} else { |
42
|
|
|
$labels = $this->labels; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return new LabelCollection($labels); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param Label $label |
50
|
|
|
* @return LabelCollection |
51
|
|
|
*/ |
52
|
|
View Code Duplication |
public function without(Label $label) |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
$labels = array_filter( |
55
|
|
|
$this->labels, |
56
|
|
|
function (Label $existingLabel) use ($label) { |
57
|
|
|
return !$existingLabel->equals($label); |
58
|
|
|
} |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
return new LabelCollection($labels); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @inheritdoc |
66
|
|
|
*/ |
67
|
|
|
public function count() |
68
|
|
|
{ |
69
|
|
|
return count($this->labels); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param Label $label |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
View Code Duplication |
public function contains(Label $label) |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$equalLabels = array_filter( |
79
|
|
|
$this->labels, |
80
|
|
|
function (Label $existingLabel) use ($label) { |
81
|
|
|
return $label->equals($existingLabel); |
82
|
|
|
} |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
return !empty($equalLabels); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return Label[] |
90
|
|
|
*/ |
91
|
|
|
public function asArray() |
92
|
|
|
{ |
93
|
|
|
return $this->labels; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param LabelCollection $labelCollectionToMerge |
98
|
|
|
* @return LabelCollection |
99
|
|
|
*/ |
100
|
|
|
public function merge(LabelCollection $labelCollectionToMerge) |
101
|
|
|
{ |
102
|
|
|
$mergedLabels = []; |
103
|
|
|
|
104
|
|
|
// Keep labels from the original collection that are not in the |
105
|
|
|
// collection to merge. |
106
|
|
|
foreach ($this->labels as $label) { |
107
|
|
|
if (!$labelCollectionToMerge->contains($label)) { |
108
|
|
|
$mergedLabels[] = $label; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// Add all labels from the collection to merge. |
113
|
|
|
$add = $labelCollectionToMerge->asArray(); |
114
|
|
|
$mergedLabels = array_merge($mergedLabels, $add); |
115
|
|
|
|
116
|
|
|
return new LabelCollection($mergedLabels); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param LabelCollection $labelCollection |
121
|
|
|
* @return LabelCollection |
122
|
|
|
*/ |
123
|
|
|
public function intersect(LabelCollection $labelCollection) |
124
|
|
|
{ |
125
|
|
|
$intersectLabels = array_filter( |
126
|
|
|
$this->labels, |
127
|
|
|
function ($label) use ($labelCollection) { |
128
|
|
|
return $labelCollection->contains($label); |
129
|
|
|
} |
130
|
|
|
); |
131
|
|
|
|
132
|
|
|
return new LabelCollection($intersectLabels); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param callable $filterFunction |
137
|
|
|
* @return LabelCollection |
138
|
|
|
*/ |
139
|
|
|
public function filter(callable $filterFunction) |
140
|
|
|
{ |
141
|
|
|
return new LabelCollection( |
142
|
|
|
array_filter($this->labels, $filterFunction) |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string[] $strings |
148
|
|
|
* @return LabelCollection |
149
|
|
|
*/ |
150
|
|
|
public static function fromStrings(array $strings) |
151
|
|
|
{ |
152
|
|
|
$labelCollection = new LabelCollection(); |
153
|
|
|
|
154
|
|
|
foreach ($strings as $string) { |
155
|
|
|
try { |
156
|
|
|
$label = new Label($string); |
157
|
|
|
$labelCollection = $labelCollection->with($label); |
158
|
|
|
} catch (\InvalidArgumentException $exception) { |
|
|
|
|
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $labelCollection; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param CultureFeed_Cdb_Data_Keyword[] $keywords |
167
|
|
|
* @return LabelCollection |
168
|
|
|
*/ |
169
|
|
|
public static function fromKeywords($keywords) |
170
|
|
|
{ |
171
|
|
|
$labelCollection = new LabelCollection(); |
172
|
|
|
|
173
|
|
|
foreach ($keywords as $keyword) { |
174
|
|
|
try { |
175
|
|
|
$label = new Label($keyword->getValue(), $keyword->isVisible()); |
176
|
|
|
$labelCollection = $labelCollection->with($label); |
177
|
|
|
} catch (\InvalidArgumentException $exception) { |
|
|
|
|
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $labelCollection; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return string[] |
186
|
|
|
*/ |
187
|
|
|
public function toStrings() |
188
|
|
|
{ |
189
|
|
|
$labels = array_map( |
190
|
|
|
function (Label $label) { |
191
|
|
|
return (string) $label; |
192
|
|
|
}, |
193
|
|
|
$this->labels |
194
|
|
|
); |
195
|
|
|
|
196
|
|
|
return $labels; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..