Completed
Push — feature/authz-service ( 32c4b7...df9aff )
by
unknown
03:22
created

InstitutionSet::getInstitutions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Copyright 2018 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 Surfnet\Stepup\Configuration\Value;
20
21
use JsonSerializable;
22
use Surfnet\Stepup\Exception\InvalidArgumentException;
23
24
final class InstitutionSet implements JsonSerializable
25
{
26
    /**
27
     * @var Institution[]
28
     */
29
    private $institutions;
30
31
    /**
32
     * @param Institution[]
33
     */
34
    private function __construct(array $institutions)
35
    {
36
        // Normalize (lowercase) the institutions for the test on unique entries below.
37
        $institutionsLowerCased = array_map('strtolower', $institutions);
38
        if ($institutionsLowerCased !== array_unique($institutionsLowerCased)) {
39
            throw new InvalidArgumentException('Duplicate entries are not allowed in the InstitutionSet');
40
        }
41
42
        $this->institutions = $this->sort($institutions);
43
    }
44
45
    /**
46
     * @param Institution[]
47
     * @return InstitutionSet
48
     */
49
    public static function create(array $institutions)
50
    {
51
        // Verify only institutions are collected in the set
52
        array_walk(
53
            $institutions,
54
            function ($institution, $key) use ($institutions) {
55
                if (!$institution instanceof Institution) {
56
                    throw InvalidArgumentException::invalidType(
57
                        Institution::class,
58
                        'institutions',
59
                        $institutions[$key]
60
                    );
61
                }
62
            }
63
        );
64
65
        return new self($institutions);
66
    }
67
68
    public function equals(InstitutionSet $other)
69
    {
70
        return $this->toScalarArray() === $other->toScalarArray();
71
    }
72
73
    /**
74
     * @param Institution $institution
75
     * @return bool
76
     */
77
    public function isOption(Institution $institution)
78
    {
79
        return in_array($institution->getInstitution(), $this->institutions);
80
    }
81
82
    /**
83
     * @return Institution[]
84
     */
85
    public function getInstitutions()
86
    {
87
        return $this->institutions;
88
    }
89
90
    public function toScalarArray()
91
    {
92
        return array_map('strval', $this->institutions);
93
    }
94
95
    public function jsonSerialize()
96
    {
97
        return $this->institutions;
98
    }
99
100
    /**
101
     * @param Institution[] $institutions
102
     * @return Institution[]
103
     */
104
    private function sort(array $institutions)
105
    {
106
        usort($institutions, function (Institution $a, Institution $b) {
107
            return strcmp($a->getInstitution(), $b->getInstitution());
108
        });
109
110
        return $institutions;
111
    }
112
}
113