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
|
|
|
/** |
69
|
|
|
* @param array $institutions |
70
|
|
|
* @return InstitutionSet |
71
|
|
|
*/ |
72
|
|
|
public static function createFromStringArray(array $institutions) |
73
|
|
|
{ |
74
|
|
|
// Verify only strings are collected in the set |
75
|
|
|
array_walk( |
76
|
|
|
$institutions, |
77
|
|
|
function ($institution, $key) use ($institutions) { |
78
|
|
|
if (!is_string($institution) || strlen(trim($institution)) === 0) { |
79
|
|
|
throw InvalidArgumentException::invalidType( |
80
|
|
|
'string', |
81
|
|
|
'institutions', |
82
|
|
|
$institutions[$key] |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$set = []; |
89
|
|
|
foreach ($institutions as $institutionTitle) { |
90
|
|
|
$set[] = new Institution($institutionTitle); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return self::create($set); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function equals(InstitutionSet $other) |
97
|
|
|
{ |
98
|
|
|
return $this->toScalarArray() === $other->toScalarArray(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param Institution $institution |
103
|
|
|
* @return bool |
104
|
|
|
*/ |
105
|
|
|
public function isOption(Institution $institution) |
106
|
|
|
{ |
107
|
|
|
return in_array($institution->getInstitution(), $this->institutions); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return Institution[] |
112
|
|
|
*/ |
113
|
|
|
public function getInstitutions() |
114
|
|
|
{ |
115
|
|
|
return $this->institutions; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function toScalarArray() |
119
|
|
|
{ |
120
|
|
|
return array_map('strval', $this->institutions); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function jsonSerialize() |
124
|
|
|
{ |
125
|
|
|
return $this->institutions; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param Institution[] $institutions |
130
|
|
|
* @return Institution[] |
131
|
|
|
*/ |
132
|
|
|
private function sort(array $institutions) |
133
|
|
|
{ |
134
|
|
|
usort($institutions, function(Institution $a, Institution $b) |
135
|
|
|
{ |
136
|
|
|
return strcmp($a->getInstitution(), $b->getInstitution()); |
137
|
|
|
}); |
138
|
|
|
|
139
|
|
|
return $institutions; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|