Completed
Pull Request — develop (#80)
by
unknown
02:05
created

AttributeAggregationAttributesList::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * Copyright 2017 SURFnet B.V.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace OpenConext\Profile\Value\AttributeAggregation;
20
21
use OpenConext\AttributeAggregationApiClientBundle\Exception\InvalidArgumentException;
22
23
/**
24
 * AttributeAggregationAttributesList as returned from the AA API
25
 */
26
final class AttributeAggregationAttributesList
27
{
28
    /**
29
     * @var AttributeAggregationAttribute[]
30
     */
31
    private $attributes;
32
33
    /**
34
     * @param AttributeAggregationAttribute[] $attributes
35
     */
36
    public function __construct(array $attributes)
37
    {
38
        $this->attributes = $attributes;
39
    }
40
41
    /**
42
     * @return AttributeAggregationAttribute[]
43
     */
44
    public function getAttributes()
45
    {
46
        return $this->attributes;
47
    }
48
49
    /**
50
     * @param $identifier
51
     * @return bool
52
     */
53
    public function hasAttribute($identifier)
54
    {
55
        foreach ($this->attributes as $attribute) {
56
            if ($attribute->getIdentifier() === $identifier) {
57
                return true;
58
            }
59
        }
60
        return false;
61
    }
62
63
    public function filterEnabledAttributes(AttributeAggregationEnabledAttributes $enabledAttributes)
64
    {
65
        $this->attributes = array_filter(
66
            $this->attributes,
67
            function (AttributeAggregationAttribute $attribute) use ($enabledAttributes) {
68
                return $enabledAttributes->isEnabled($attribute->getIdentifier());
69
            }
70
        );
71
    }
72
73
    /**
74
     * @param $identifier
75
     * @return AttributeAggregationAttribute
76
     * @throws InvalidArgumentException
77
     */
78
    public function getAttribute($identifier)
79
    {
80
        foreach ($this->attributes as $attribute) {
81
            if ($attribute->getIdentifier() === $identifier) {
82
                return $attribute;
83
            }
84
        }
85
86
        throw new InvalidArgumentException(
87
            sprintf(
88
                'The requested attribute with identifier "%s" could not be found',
89
                $identifier
90
            )
91
        );
92
    }
93
}
94