1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OpenConext\Value\Saml\Metadata\ContactPerson; |
4
|
|
|
|
5
|
|
|
use ArrayIterator; |
6
|
|
|
use Countable; |
7
|
|
|
use IteratorAggregate; |
8
|
|
|
use OpenConext\Value\Assert\Assertion; |
9
|
|
|
use OpenConext\Value\Exception\IndexOutOfBoundsException; |
10
|
|
|
|
11
|
|
|
final class EmailAddressList implements Countable, IteratorAggregate |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var EmailAddress[] |
15
|
|
|
*/ |
16
|
|
|
private $emailAddresses; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param EmailAddress[] $emailAddresses |
20
|
|
|
*/ |
21
|
|
|
public function __construct(array $emailAddresses) |
22
|
|
|
{ |
23
|
|
|
Assertion::allIsInstanceOf($emailAddresses, '\OpenConext\Value\Saml\Metadata\ContactPerson\EmailAddress'); |
24
|
|
|
|
25
|
|
|
$this->emailAddresses = array_values($emailAddresses); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param EmailAddress $emailAddress |
30
|
|
|
* @return EmailAddressList |
31
|
|
|
*/ |
32
|
|
|
public function add(EmailAddress $emailAddress) |
33
|
|
|
{ |
34
|
|
|
return new self(array_merge($this->emailAddresses, array($emailAddress))); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param EmailAddress $emailAddress |
39
|
|
|
* @return bool |
40
|
|
|
*/ |
41
|
|
|
public function contains(EmailAddress $emailAddress) |
42
|
|
|
{ |
43
|
|
|
foreach ($this->emailAddresses as $address) { |
44
|
|
|
if ($address->equals($emailAddress)) { |
45
|
|
|
return true; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return false; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param EmailAddress $emailAddress |
54
|
|
|
* @return int |
55
|
|
|
*/ |
56
|
|
|
public function indexOf(EmailAddress $emailAddress) |
57
|
|
|
{ |
58
|
|
|
foreach ($this->emailAddresses as $index => $address) { |
59
|
|
|
if ($address->equals($emailAddress)) { |
60
|
|
|
return $index; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return -1; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param int $index |
69
|
|
|
* @return EmailAddress |
70
|
|
|
*/ |
71
|
|
|
public function get($index) |
72
|
|
|
{ |
73
|
|
|
Assertion::integer($index); |
74
|
|
|
|
75
|
|
|
if ($index < 0) { |
76
|
|
|
throw IndexOutOfBoundsException::tooLow($index, 0); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if ($index > count($this->emailAddresses) - 1) { |
80
|
|
|
throw IndexOutOfBoundsException::tooHigh($index, count($this->emailAddresses) - 1); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this->emailAddresses[$index]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param EmailAddressList $other |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
|
|
public function equals(EmailAddressList $other) |
91
|
|
|
{ |
92
|
|
|
if (count($this->emailAddresses) !== count($other->emailAddresses)) { |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
foreach ($this->emailAddresses as $index => $emailAddress) { |
97
|
|
|
if (!$emailAddress->equals($other->emailAddresses[$index])) { |
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return true; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return EmailAddress[] |
107
|
|
|
*/ |
108
|
|
|
public function toArray() |
109
|
|
|
{ |
110
|
|
|
return $this->emailAddresses; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function getIterator() |
114
|
|
|
{ |
115
|
|
|
return new ArrayIterator($this->emailAddresses); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function count() |
119
|
|
|
{ |
120
|
|
|
return count($this->emailAddresses); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function __toString() |
124
|
|
|
{ |
125
|
|
|
return sprintf('EmailAddressList[%s]', implode(', ', $this->emailAddresses)); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|