Completed
Pull Request — develop (#9)
by Daan van
79:41 queued 49:00
created

ShibbolethMetadataScopeList::deserialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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