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