Completed
Pull Request — master (#249)
by Luc
04:41
created

LabelCollection::filter()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
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);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_values($labels) of type array<integer,?> is incompatible with the declared type array<integer,object<CultuurNet\UDB3\Label>> of property $labels.

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..

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
        $filteredLabels = [];
142
        foreach ($this->labels as $label) {
143
            if ($filterFunction($label)) {
144
                $filteredLabels[] = $label;
145
            }
146
        }
147
148
        return new LabelCollection($filteredLabels);
149
    }
150
151
    /**
152
     * @param string[] $strings
153
     * @return LabelCollection
154
     */
155
    public static function fromStrings(array $strings)
156
    {
157
        $labelCollection = new LabelCollection();
158
159
        foreach ($strings as $string) {
160
            try {
161
                $label = new Label($string);
162
                $labelCollection = $labelCollection->with($label);
163
            } catch (\InvalidArgumentException $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
164
            }
165
        }
166
167
        return $labelCollection;
168
    }
169
170
    /**
171
     * @param CultureFeed_Cdb_Data_Keyword[] $keywords
172
     * @return LabelCollection
173
     */
174
    public static function fromKeywords($keywords)
175
    {
176
        $labelCollection = new LabelCollection();
177
178
        foreach ($keywords as $keyword) {
179
            try {
180
                $label = new Label($keyword->getValue(), $keyword->isVisible());
181
                $labelCollection = $labelCollection->with($label);
182
            } catch (\InvalidArgumentException $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
183
            }
184
        }
185
186
        return $labelCollection;
187
    }
188
189
    /**
190
     * @return string[]
191
     */
192
    public function toStrings()
193
    {
194
        $labels = array_map(
195
            function (Label $label) {
196
                return (string) $label;
197
            },
198
            $this->labels
199
        );
200
201
        return $labels;
202
    }
203
}
204