TelephoneNumberList::add()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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
use OpenConext\Value\Serializable;
11
12
final class TelephoneNumberList implements Countable, IteratorAggregate, Serializable
13
{
14
    /**
15
     * @var TelephoneNumber[]
16
     */
17
    private $telephoneNumbers;
18
19
    /**
20
     * @param TelephoneNumber[] $telephoneNumbers
21
     */
22
    public function __construct(array $telephoneNumbers)
23
    {
24
        Assertion::allIsInstanceOf($telephoneNumbers, '\OpenConext\Value\Saml\Metadata\ContactPerson\TelephoneNumber');
25
26
        $this->telephoneNumbers = array_values($telephoneNumbers);
27
    }
28
29
    /**
30
     * @param TelephoneNumber $telephoneNumber
31
     * @return TelephoneNumberList
32
     */
33
    public function add(TelephoneNumber $telephoneNumber)
34
    {
35
        return new self(array_merge($this->telephoneNumbers, array($telephoneNumber)));
36
    }
37
38
    /**
39
     * @param TelephoneNumber $telephoneNumber
40
     * @return bool
41
     */
42
    public function contains(TelephoneNumber $telephoneNumber)
43
    {
44
        foreach ($this->telephoneNumbers as $number) {
45
            if ($number->equals($telephoneNumber)) {
46
                return true;
47
            }
48
        }
49
50
        return false;
51
    }
52
53
    /**
54
     * @param TelephoneNumber $telephoneNumber
55
     * @return int
56
     */
57
    public function indexOf(TelephoneNumber $telephoneNumber)
58
    {
59
        foreach ($this->telephoneNumbers as $index => $number) {
60
            if ($number->equals($telephoneNumber)) {
61
                return $index;
62
            }
63
        }
64
65
        return -1;
66
    }
67
68
    /**
69
     * @param int $index
70
     * @return TelephoneNumber
71
     */
72
    public function get($index)
73
    {
74
        Assertion::integer($index);
75
76
        if ($index < 0) {
77
            throw IndexOutOfBoundsException::tooLow($index, 0);
78
        }
79
80
        if ($index > count($this->telephoneNumbers) - 1) {
81
            throw IndexOutOfBoundsException::tooHigh($index, count($this->telephoneNumbers) - 1);
82
        }
83
84
        return $this->telephoneNumbers[$index];
85
    }
86
87
    /**
88
     * @param Callable $predicate
89
     * @return null|TelephoneNumber
90
     */
91
    public function find($predicate)
92
    {
93
        Assertion::isCallable($predicate, null, 'predicate');
94
95
        foreach ($this->telephoneNumbers as $telephoneNumber) {
96
            if (call_user_func($predicate, $telephoneNumber) === true) {
97
                return $telephoneNumber;
98
            }
99
        }
100
101
        return null;
102
    }
103
104
    /**
105
     * @param TelephoneNumberList $other
106
     * @return bool
107
     */
108
    public function equals(TelephoneNumberList $other)
109
    {
110
        if (count($this->telephoneNumbers) !== count($other->telephoneNumbers)) {
111
            return false;
112
        }
113
114
        foreach ($this->telephoneNumbers as $index => $telephoneNumber) {
115
            if (!$telephoneNumber->equals($other->telephoneNumbers[$index])) {
116
                return false;
117
            }
118
        }
119
120
        return true;
121
    }
122
123
    /**
124
     * @return TelephoneNumber[]
125
     */
126
    public function toArray()
127
    {
128
        return $this->telephoneNumbers;
129
    }
130
131
    public function getIterator()
132
    {
133
        return new ArrayIterator($this->telephoneNumbers);
134
    }
135
136
    public function count()
137
    {
138
        return count($this->telephoneNumbers);
139
    }
140
141
    public static function deserialize($data)
142
    {
143
        Assertion::isArray($data);
144
145
        $telephoneNumbers = array_map(function ($telephoneNumber) {
146
            return TelephoneNumber::deserialize($telephoneNumber);
147
        }, $data);
148
149
        return new self($telephoneNumbers);
150
    }
151
152
    public function serialize()
153
    {
154
        return array_map(function (TelephoneNumber $telephoneNumber) {
155
            return $telephoneNumber->serialize();
156
        }, $this->telephoneNumbers);
157
    }
158
159
    public function __toString()
160
    {
161
        return sprintf('TelephoneNumberList[%s]', implode(', ', $this->telephoneNumbers));
162
    }
163
}
164