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