1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2014 SURFnet bv |
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\Identity\Collection; |
20
|
|
|
|
21
|
|
|
use ArrayIterator; |
22
|
|
|
use Broadway\Serializer\SerializableInterface; |
23
|
|
|
use IteratorAggregate; |
24
|
|
|
use JsonSerializable; |
25
|
|
|
use Surfnet\Stepup\Exception\RuntimeException; |
26
|
|
|
use Surfnet\Stepup\Identity\Value\Institution; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
30
|
|
|
*/ |
31
|
|
|
final class InstitutionCollection implements IteratorAggregate, JsonSerializable, SerializableInterface |
32
|
|
|
{ |
33
|
|
|
private $elements = []; |
34
|
|
|
|
35
|
|
|
public function __construct(array $institutions = []) |
36
|
|
|
{ |
37
|
|
|
foreach ($institutions as $institution) { |
38
|
|
|
$this->add($institution); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function contains(Institution $institution) |
43
|
|
|
{ |
44
|
|
|
return in_array($institution, $this->elements); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Adds the institution to this collection |
49
|
|
|
* |
50
|
|
|
* @param Institution $institution |
51
|
|
|
* @throws RuntimeException when the institution already exists in this collection |
52
|
|
|
*/ |
53
|
|
|
public function add(Institution $institution) |
54
|
|
|
{ |
55
|
|
|
if (in_array($institution, $this->elements)) { |
56
|
|
|
throw new RuntimeException(sprintf( |
57
|
|
|
'Institution "%s" is already in this collection', |
58
|
|
|
$institution |
59
|
|
|
)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->elements[] = $institution; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param $institution |
67
|
|
|
*/ |
68
|
|
|
private function addIgnoringDuplicates($institution) |
69
|
|
|
{ |
70
|
|
|
if (in_array($institution, $this->elements)) { |
71
|
|
|
return; |
72
|
|
|
} |
73
|
|
|
$this->elements[] = $institution; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Adds all institutions from the given collection to this collection |
78
|
|
|
* |
79
|
|
|
* @param InstitutionCollection $institutionCollection |
80
|
|
|
*/ |
81
|
|
|
public function addAllFrom(InstitutionCollection $institutionCollection) |
82
|
|
|
{ |
83
|
|
|
foreach ($institutionCollection as $institution) { |
84
|
|
|
$this->add($institution); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Merges the two collections, ignores duplicates |
90
|
|
|
* @param InstitutionCollection $institutionCollection |
91
|
|
|
*/ |
92
|
|
|
public function merge(InstitutionCollection $institutionCollection) |
93
|
|
|
{ |
94
|
|
|
foreach ($institutionCollection as $institution) { |
95
|
|
|
$this->addIgnoringDuplicates($institution); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Removes an institution from this collection |
101
|
|
|
* |
102
|
|
|
* @param Institution $institution |
103
|
|
|
* @throws RuntimeException when the institution to remove is not in this collection |
104
|
|
|
*/ |
105
|
|
|
public function remove(Institution $institution) |
106
|
|
|
{ |
107
|
|
|
if (!in_array($institution, $this->elements)) { |
108
|
|
|
throw new RuntimeException(sprintf( |
109
|
|
|
'Cannot remove Institution "%s" from the collection as it is not in the collection', |
110
|
|
|
$institution |
111
|
|
|
)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$elements = array_filter($this->elements, function ($inst) use ($institution) { |
115
|
|
|
return !$institution->equals($inst); |
116
|
|
|
}); |
117
|
|
|
$this->elements = $elements; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Removes all Institutions in the given collection from this collection |
122
|
|
|
* |
123
|
|
|
* @param InstitutionCollection $institutionCollection |
124
|
|
|
*/ |
125
|
|
|
public function removeAllIn(InstitutionCollection $institutionCollection) |
126
|
|
|
{ |
127
|
|
|
foreach ($institutionCollection as $institution) { |
128
|
|
|
$this->remove($institution); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function jsonSerialize() |
133
|
|
|
{ |
134
|
|
|
return ['institutions' => $this->elements]; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public static function deserialize(array $data) |
138
|
|
|
{ |
139
|
|
|
$institutions = array_map(function ($institution) { |
140
|
|
|
return new Institution($institution); |
141
|
|
|
}, $data); |
142
|
|
|
|
143
|
|
|
return new self($institutions); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function serialize() |
147
|
|
|
{ |
148
|
|
|
return array_map(function (Institution $institution) { |
149
|
|
|
return (string) $institution; |
150
|
|
|
}, $this->elements); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function getIterator() |
154
|
|
|
{ |
155
|
|
|
return new ArrayIterator($this->elements); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function isEmpty() |
159
|
|
|
{ |
160
|
|
|
return count($this->elements) > 0; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|