|
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
|
|
|
$index = array_search($institution, $this->elements, true); |
|
90
|
|
|
unset($this->elements[$index]); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Removes all Institutions in the given collection from this collection |
|
95
|
|
|
* |
|
96
|
|
|
* @param InstitutionCollection $institutionCollection |
|
97
|
|
|
*/ |
|
98
|
|
|
public function removeAllIn(InstitutionCollection $institutionCollection) |
|
99
|
|
|
{ |
|
100
|
|
|
foreach ($institutionCollection as $institution) { |
|
101
|
|
|
$this->remove($institution); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function jsonSerialize() |
|
106
|
|
|
{ |
|
107
|
|
|
return ['institutions' => $this->elements]; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public static function deserialize(array $data) |
|
111
|
|
|
{ |
|
112
|
|
|
$institutions = array_map(function ($institution) { |
|
113
|
|
|
return new Institution($institution); |
|
114
|
|
|
}, $data); |
|
115
|
|
|
|
|
116
|
|
|
return new self($institutions); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function serialize() |
|
120
|
|
|
{ |
|
121
|
|
|
return array_map(function (Institution $institution) { |
|
122
|
|
|
return (string) $institution; |
|
123
|
|
|
}, $this->elements); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function getIterator() |
|
127
|
|
|
{ |
|
128
|
|
|
return new ArrayIterator($this->elements); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|