ContactPersonList   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 3
dl 0
loc 152
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A add() 0 4 1
A contains() 0 10 3
A indexOf() 0 10 3
A get() 0 14 3
A find() 0 12 3
A equals() 0 14 4
A toArray() 0 4 1
A getIterator() 0 4 1
A count() 0 4 1
A deserialize() 0 10 1
A serialize() 0 6 1
A __toString() 0 4 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 Callable $predicate
89
     * @return null|ContactPerson
90
     */
91
    public function find($predicate)
92
    {
93
        Assertion::isCallable($predicate, null, 'predicate');
94
95
        foreach ($this->contactPersons as $contactPerson) {
96
            if (call_user_func($predicate, $contactPerson) === true) {
97
                return $contactPerson;
98
            }
99
        }
100
101
        return null;
102
    }
103
104
    /**
105
     * @param ContactPersonList $other
106
     * @return bool
107
     */
108
    public function equals(ContactPersonList $other)
109
    {
110
        if (count($this->contactPersons) !== count($other->contactPersons)) {
111
            return false;
112
        }
113
114
        foreach ($this->contactPersons as $index => $contactPerson) {
115
            if (!$contactPerson->equals($other->contactPersons[$index])) {
116
                return false;
117
            }
118
        }
119
120
        return true;
121
    }
122
123
    /**
124
     * @return ContactPerson[]
125
     */
126
    public function toArray()
127
    {
128
        return $this->contactPersons;
129
    }
130
131
    public function getIterator()
132
    {
133
        return new ArrayIterator($this->contactPersons);
134
    }
135
136
    public function count()
137
    {
138
        return count($this->contactPersons);
139
    }
140
141
    public static function deserialize($data)
142
    {
143
        Assertion::isArray($data);
144
145
        $contactPersons = array_map(function ($contactPerson) {
146
            return ContactPerson::deserialize($contactPerson);
147
        }, $data);
148
149
        return new self($contactPersons);
150
    }
151
152
    public function serialize()
153
    {
154
        return array_map(function (ContactPerson $contactPerson) {
155
            return $contactPerson->serialize();
156
        }, $this->contactPersons);
157
    }
158
159
    public function __toString()
160
    {
161
        return sprintf('ContactPersonList[%s]', implode(', ', $this->contactPersons));
162
    }
163
}
164