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

ContactPersonList::get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
3
namespace OpenConext\Value\Saml\Metadata;
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 ContactPersonList implements Countable, IteratorAggregate, Serializable
13
{
14
    /**
15
     * @var ContactPerson[]
16
     */
17
    private $contactPersons;
18
19
    /**
20
     * @param ContactPerson[] $contactPersons
21
     */
22
    public function __construct(array $contactPersons)
23
    {
24
        Assertion::allIsInstanceOf($contactPersons, '\OpenConext\Value\Saml\Metadata\ContactPerson');
25
26
        $this->contactPersons = array_values($contactPersons);
27
    }
28
29
    /**
30
     * @param ContactPerson $contactPerson
31
     * @return ContactPersonList
32
     */
33
    public function add(ContactPerson $contactPerson)
34
    {
35
        return new self(array_merge($this->contactPersons, array($contactPerson)));
36
    }
37
38
    /**
39
     * @param ContactPerson $contactPerson
40
     * @return bool
41
     */
42
    public function contains(ContactPerson $contactPerson)
43
    {
44
        foreach ($this->contactPersons as $person) {
45
            if ($person->equals($contactPerson)) {
46
                return true;
47
            }
48
        }
49
50
        return false;
51
    }
52
53
    /**
54
     * @param ContactPerson $contactPerson
55
     * @return int
56
     */
57
    public function indexOf(ContactPerson $contactPerson)
58
    {
59
        foreach ($this->contactPersons as $index => $person) {
60
            if ($person->equals($contactPerson)) {
61
                return $index;
62
            }
63
        }
64
65
        return -1;
66
    }
67
68
    /**
69
     * @param int $index
70
     * @return ContactPerson
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->contactPersons) - 1) {
81
            throw IndexOutOfBoundsException::tooHigh($index, count($this->contactPersons) - 1);
82
        }
83
84
        return $this->contactPersons[$index];
85
    }
86
87
    /**
88
     * @param ContactPersonList $other
89
     * @return bool
90
     */
91
    public function equals(ContactPersonList $other)
92
    {
93
        if (count($this->contactPersons) !== count($other->contactPersons)) {
94
            return false;
95
        }
96
97
        foreach ($this->contactPersons as $index => $contactPerson) {
98
            if (!$contactPerson->equals($other->contactPersons[$index])) {
99
                return false;
100
            }
101
        }
102
103
        return true;
104
    }
105
106
    /**
107
     * @return ContactPerson[]
108
     */
109
    public function toArray()
110
    {
111
        return $this->contactPersons;
112
    }
113
114
    public function getIterator()
115
    {
116
        return new ArrayIterator($this->contactPersons);
117
    }
118
119
    public function count()
120
    {
121
        return count($this->contactPersons);
122
    }
123
124
    public static function deserialize($data)
125
    {
126
        Assertion::isArray($data);
127
128
        $contactPersons = array_map(function ($contactPerson) {
129
            return ContactPerson::deserialize($contactPerson);
130
        }, $data);
131
132
        return new self($contactPersons);
133
    }
134
135
    public function serialize()
136
    {
137
        return array_map(function (ContactPerson $contactPerson) {
138
            return $contactPerson->serialize();
139
        }, $this->contactPersons);
140
    }
141
142
    public function __toString()
143
    {
144
        return sprintf('ContactPersonList[%s]', implode(', ', $this->contactPersons));
145
    }
146
}
147