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