1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2017 SURFnet B.V. |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Surfnet\Stepup\Configuration\Value; |
20
|
|
|
|
21
|
|
|
use ArrayIterator; |
22
|
|
|
use Broadway\Serializer\SerializableInterface; |
23
|
|
|
use IteratorAggregate; |
24
|
|
|
use JsonSerializable; |
25
|
|
|
use Surfnet\StepupBundle\Value\SecondFactorType; |
26
|
|
|
|
27
|
|
|
final class AllowedSecondFactorList implements JsonSerializable, IteratorAggregate, SerializableInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var SecondFactorType[] |
31
|
|
|
*/ |
32
|
|
|
private $allowedSecondFactors = []; |
33
|
|
|
|
34
|
|
|
private function __construct(array $allowedSecondFactors) |
35
|
|
|
{ |
36
|
|
|
foreach ($allowedSecondFactors as $allowedSecondFactor) { |
37
|
|
|
$this->initializeWith($allowedSecondFactor); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return AllowedSecondFactorList |
43
|
|
|
*/ |
44
|
|
|
public static function blank() |
45
|
|
|
{ |
46
|
|
|
return new self([]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param $allowedSecondFactors |
51
|
|
|
* @return AllowedSecondFactorList |
52
|
|
|
*/ |
53
|
|
|
public static function ofTypes($allowedSecondFactors) |
54
|
|
|
{ |
55
|
|
|
return new self($allowedSecondFactors); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param SecondFactorType $secondFactor |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
public function allows(SecondFactorType $secondFactor) |
63
|
|
|
{ |
64
|
|
|
return $this->isBlank() || $this->contains($secondFactor); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
public function isBlank() |
71
|
|
|
{ |
72
|
|
|
return empty($this->allowedSecondFactors); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param SecondFactorType $secondFactor |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
|
|
public function contains(SecondFactorType $secondFactor) |
80
|
|
|
{ |
81
|
|
|
foreach ($this->allowedSecondFactors as $allowedSecondFactor) { |
82
|
|
|
if ($allowedSecondFactor->equals($secondFactor)) { |
|
|
|
|
83
|
|
|
return true; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
public function equals(AllowedSecondFactorList $other) |
92
|
|
|
{ |
93
|
|
|
if (count($other->allowedSecondFactors) !== count($this->allowedSecondFactors)) { |
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
foreach ($other->allowedSecondFactors as $allowedSecondFactor) { |
98
|
|
|
if (!$this->contains($allowedSecondFactor)) { |
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return true; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public static function deserialize(array $data) |
107
|
|
|
{ |
108
|
|
|
$secondFactorTypes = array_map( |
109
|
|
|
function ($secondFactorString) { |
110
|
|
|
return new SecondFactorType($secondFactorString); |
111
|
|
|
}, |
112
|
|
|
$data['allowed_second_factors'] |
113
|
|
|
); |
114
|
|
|
|
115
|
|
|
return new self($secondFactorTypes); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function serialize() |
119
|
|
|
{ |
120
|
|
|
$allowedSecondFactors = array_map( |
121
|
|
|
function (SecondFactorType $secondFactorType) { |
122
|
|
|
return $secondFactorType->getSecondFactorType(); |
123
|
|
|
}, |
124
|
|
|
$this->allowedSecondFactors |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
return [ |
128
|
|
|
'allowed_second_factors' => $allowedSecondFactors, |
129
|
|
|
]; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function getIterator() |
133
|
|
|
{ |
134
|
|
|
return new ArrayIterator($this->allowedSecondFactors); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function jsonSerialize() |
138
|
|
|
{ |
139
|
|
|
return $this->allowedSecondFactors; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
private function initializeWith(SecondFactorType $allowedSecondFactor) |
143
|
|
|
{ |
144
|
|
|
if (!$this->contains($allowedSecondFactor)) { |
145
|
|
|
$this->allowedSecondFactors[] = $allowedSecondFactor; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: