Completed
Pull Request — develop (#80)
by
unknown
03:24 queued 01:23
created

getAttributes()   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 0
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
     * @param array $attributes
43
     * @return AttributeAggregationAttributesList
44
     */
45
    public static function fromApiResponse(array $attributes)
46
    {
47
        $attributeCollection = [];
48
        foreach ($attributes as $attributeData) {
49
            $attributeCollection[] = AttributeAggregationAttribute::fromApiResponse($attributeData);
50
        }
51
        return new self($attributeCollection);
52
    }
53
54
    /**
55
     * @return AttributeAggregationAttribute[]
56
     */
57
    public function getAttributes()
58
    {
59
        return $this->attributes;
60
    }
61
62
    /**
63
     * @param $identifier
64
     * @return bool
65
     */
66
    public function hasAttribute($identifier)
67
    {
68
        foreach ($this->attributes as $attribute) {
69
            if ($attribute->getIdentifier() === $identifier) {
70
                return true;
71
            }
72
        }
73
        return false;
74
    }
75
76
    public function filterEnabledAttributes(AttributeAggregationEnabledAttributes $enabledAttributes)
77
    {
78
        $this->attributes = array_filter(
79
            $this->attributes,
80
            function (AttributeAggregationAttribute $attribute) use ($enabledAttributes) {
81
                return $enabledAttributes->isEnabled($attribute->getIdentifier());
82
            }
83
        );
84
    }
85
86
    /**
87
     * @param $identifier
88
     * @return AttributeAggregationAttribute
89
     * @throws InvalidArgumentException
90
     */
91
    public function getAttribute($identifier)
92
    {
93
        foreach ($this->attributes as $attribute) {
94
            if ($attribute->getIdentifier() === $identifier) {
95
                return $attribute;
96
            }
97
        }
98
99
        throw new InvalidArgumentException(
100
            sprintf(
101
                'The requested attribute with identifier "%s" could not be found',
102
                $identifier
103
            )
104
        );
105
    }
106
}
107