Completed
Pull Request — develop (#9)
by Daan van
24:12 queued 15:23
created

TelephoneNumberList::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
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 TelephoneNumberList $other
89
     * @return bool
90
     */
91
    public function equals(TelephoneNumberList $other)
92
    {
93
        if (count($this->telephoneNumbers) !== count($other->telephoneNumbers)) {
94
            return false;
95
        }
96
97
        foreach ($this->telephoneNumbers as $index => $telephoneNumber) {
98
            if (!$telephoneNumber->equals($other->telephoneNumbers[$index])) {
99
                return false;
100
            }
101
        }
102
103
        return true;
104
    }
105
106
    /**
107
     * @return TelephoneNumber[]
108
     */
109
    public function toArray()
110
    {
111
        return $this->telephoneNumbers;
112
    }
113
114
    public function getIterator()
115
    {
116
        return new ArrayIterator($this->telephoneNumbers);
117
    }
118
119
    public function count()
120
    {
121
        return count($this->telephoneNumbers);
122
    }
123
124
    public static function deserialize($data)
125
    {
126
        Assertion::isArray($data);
127
128
        $telephoneNumbers = array_map(function ($telephoneNumber) {
129
            return TelephoneNumber::deserialize($telephoneNumber);
130
        }, $data);
131
132
        return new self($telephoneNumbers);
133
    }
134
135
    public function serialize()
136
    {
137
        return array_map(function (TelephoneNumber $telephoneNumber) {
138
            return $telephoneNumber->serialize();
139
        }, $this->telephoneNumbers);
140
    }
141
142
    public function __toString()
143
    {
144
        return sprintf('TelephoneNumberList[%s]', implode(', ', $this->telephoneNumbers));
145
    }
146
}
147