1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2018 SURFnet B.V. |
4
|
|
|
* |
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
6
|
|
|
* you may not use this file except in compliance with the License. |
7
|
|
|
* You may obtain a copy of the License at |
8
|
|
|
* |
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
10
|
|
|
* |
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14
|
|
|
* See the License for the specific language governing permissions and |
15
|
|
|
* limitations under the License. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace Surfnet\Stepup\Configuration\Value; |
19
|
|
|
|
20
|
|
|
use JsonSerializable; |
21
|
|
|
use Surfnet\Stepup\Exception\InvalidArgumentException; |
22
|
|
|
|
23
|
|
|
final class InstitutionAuthorizationOption implements JsonSerializable |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var InstitutionRole |
27
|
|
|
*/ |
28
|
|
|
private $institutionRole; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var InstitutionSet |
32
|
|
|
*/ |
33
|
|
|
private $institutionSet; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var boolean |
37
|
|
|
*/ |
38
|
|
|
private $isDefault; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* If the default is set to true then the object will use the old default behaviour. That behaviour is that it |
42
|
|
|
* will take the current institution into account when returning institutions. |
43
|
|
|
* |
44
|
|
|
* AbstractRoleOption constructor. |
45
|
|
|
* @param InstitutionRole $role |
46
|
|
|
* @param InstitutionSet $institutionSet |
47
|
|
|
* @param bool $isDefault |
48
|
|
|
*/ |
49
|
|
|
private function __construct(InstitutionRole $role, InstitutionSet $institutionSet, $isDefault) |
50
|
|
|
{ |
51
|
|
|
$this->institutionRole = $role; |
52
|
|
|
$this->institutionSet = $institutionSet; |
53
|
|
|
$this->isDefault = (bool)$isDefault; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param InstitutionRole $role |
58
|
|
|
* @param string[]|null |
59
|
|
|
* @return InstitutionAuthorizationOption |
60
|
|
|
*/ |
61
|
|
|
public static function fromInstitutionConfig(InstitutionRole $role, $institutions = null) |
62
|
|
|
{ |
63
|
|
|
if (is_null($institutions)) { |
64
|
|
|
return self::getDefault($role); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (!is_array($institutions)) { |
68
|
|
|
throw InvalidArgumentException::invalidType( |
69
|
|
|
'array', |
70
|
|
|
'institutions', |
71
|
|
|
$institutions |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
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
|
|
|
$institutionSet = InstitutionSet::create($set); |
94
|
|
|
|
95
|
|
|
return new self($role, $institutionSet, false); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param InstitutionRole $role |
100
|
|
|
* @param Institution $institution |
101
|
|
|
* @param Institution[] $institutions |
102
|
|
|
* @return InstitutionAuthorizationOption |
103
|
|
|
*/ |
104
|
|
|
public static function fromInstitutions(InstitutionRole $role, Institution $institution, array $institutions) |
105
|
|
|
{ |
106
|
|
|
if (count($institutions) == 1 && current($institutions)->getInstitution() === $institution->getInstitution()) { |
107
|
|
|
return new self($role, InstitutionSet::create([]), true); |
108
|
|
|
} |
109
|
|
|
return new self($role, InstitutionSet::create($institutions), false); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param InstitutionRole $role |
114
|
|
|
* @param string[]|null |
115
|
|
|
* @return InstitutionAuthorizationOption |
116
|
|
|
*/ |
117
|
|
|
public static function getDefault(InstitutionRole $role) |
118
|
|
|
{ |
119
|
|
|
return new self($role, InstitutionSet::create([]), true); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param InstitutionRole $role |
124
|
|
|
* @param string[]|null |
125
|
|
|
* @return InstitutionAuthorizationOption |
126
|
|
|
*/ |
127
|
|
|
public static function getEmpty(InstitutionRole $role) |
128
|
|
|
{ |
129
|
|
|
return new self($role, InstitutionSet::create([]), false); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return null |
134
|
|
|
*/ |
135
|
|
|
public static function blank() |
136
|
|
|
{ |
137
|
|
|
return null; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param InstitutionAuthorizationOption $option |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
|
|
public function equals(InstitutionAuthorizationOption $option) |
145
|
|
|
{ |
146
|
|
|
return |
147
|
|
|
$this->institutionRole->equals($option->getInstitutionRole()) && |
148
|
|
|
$this->institutionSet->equals($option->getInstitutionSet()) && |
149
|
|
|
$this->isDefault === $option->isDefault(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return InstitutionRole |
154
|
|
|
*/ |
155
|
|
|
public function getInstitutionRole() |
156
|
|
|
{ |
157
|
|
|
return $this->institutionRole; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return InstitutionSet |
162
|
|
|
*/ |
163
|
|
|
public function getInstitutionSet() |
164
|
|
|
{ |
165
|
|
|
return $this->institutionSet; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* If the default is set to true then the object will use the old default behaviour. That behaviour is that it |
170
|
|
|
* will take the current institution into account and this method will return the current institution. |
171
|
|
|
* |
172
|
|
|
* @param Institution $institution |
173
|
|
|
* @return Institution[] |
174
|
|
|
*/ |
175
|
|
|
public function getInstitutions(Institution $institution) |
176
|
|
|
{ |
177
|
|
|
if ($this->isDefault) { |
178
|
|
|
return [$institution]; |
179
|
|
|
} |
180
|
|
|
return $this->institutionSet->getInstitutions(); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @return bool |
185
|
|
|
*/ |
186
|
|
|
public function isDefault() |
187
|
|
|
{ |
188
|
|
|
return $this->isDefault; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function jsonSerialize() |
192
|
|
|
{ |
193
|
|
|
if ($this->isDefault) { |
194
|
|
|
return null; |
195
|
|
|
} |
196
|
|
|
return $this->institutionSet->toScalarArray(); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|