Completed
Push — feature/more-metadata-values ( 480338...88cb5d )
by Daan van
03:11 queued 12s
created

ShibbolethMetadataScopeList::initializeWith()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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