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
|
|
|
final class InstitutionCollection implements IteratorAggregate, JsonSerializable, SerializableInterface |
29
|
|
|
{ |
30
|
|
|
private $elements = []; |
31
|
|
|
|
32
|
|
|
public function __construct(array $institutions = []) |
33
|
|
|
{ |
34
|
|
|
foreach ($institutions as $institution) { |
35
|
|
|
$this->add($institution); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function contains(Institution $institution) |
40
|
|
|
{ |
41
|
|
|
return in_array($institution, $this->elements); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Adds the institution to this collection |
46
|
|
|
* |
47
|
|
|
* @param Institution $institution |
48
|
|
|
* @throws RuntimeException when the institution already exists in this collection |
49
|
|
|
*/ |
50
|
|
|
public function add(Institution $institution) |
51
|
|
|
{ |
52
|
|
|
if (in_array($institution, $this->elements)) { |
53
|
|
|
throw new RuntimeException(sprintf( |
54
|
|
|
'Institution "%s" is already in this collection', |
55
|
|
|
$institution |
56
|
|
|
)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->elements[] = $institution; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Adds all institutions from the given collection to this collection |
64
|
|
|
* |
65
|
|
|
* @param InstitutionCollection $institutionCollection |
66
|
|
|
*/ |
67
|
|
|
public function addAllFrom(InstitutionCollection $institutionCollection) |
68
|
|
|
{ |
69
|
|
|
foreach ($institutionCollection as $institution) { |
70
|
|
|
$this->add($institution); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Removes an institution from this collection |
76
|
|
|
* |
77
|
|
|
* @param Institution $institution |
78
|
|
|
* @throws RuntimeException when the institution to remove is not in this collection |
79
|
|
|
*/ |
80
|
|
|
public function remove(Institution $institution) |
81
|
|
|
{ |
82
|
|
|
if (!in_array($institution, $this->elements)) { |
83
|
|
|
throw new RuntimeException(sprintf( |
84
|
|
|
'Cannot remove Institution "%s" from the collection as it is not in the collection', |
85
|
|
|
$institution |
86
|
|
|
)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$elements = array_filter($this->elements, function ($inst) use ($institution) { |
90
|
|
|
return !$institution->equals($inst); |
91
|
|
|
}); |
92
|
|
|
$this->elements = $elements; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Removes all Institutions in the given collection from this collection |
97
|
|
|
* |
98
|
|
|
* @param InstitutionCollection $institutionCollection |
99
|
|
|
*/ |
100
|
|
|
public function removeAllIn(InstitutionCollection $institutionCollection) |
101
|
|
|
{ |
102
|
|
|
foreach ($institutionCollection as $institution) { |
103
|
|
|
$this->remove($institution); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function jsonSerialize() |
108
|
|
|
{ |
109
|
|
|
return ['institutions' => $this->elements]; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public static function deserialize(array $data) |
113
|
|
|
{ |
114
|
|
|
$institutions = array_map(function ($institution) { |
115
|
|
|
return new Institution($institution); |
116
|
|
|
}, $data); |
117
|
|
|
|
118
|
|
|
return new self($institutions); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function serialize() |
122
|
|
|
{ |
123
|
|
|
return array_map(function (Institution $institution) { |
124
|
|
|
return (string) $institution; |
125
|
|
|
}, $this->elements); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function getIterator() |
129
|
|
|
{ |
130
|
|
|
return new ArrayIterator($this->elements); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|