|
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
|
|
|
public static function deserialize(array $data) |
|
91
|
|
|
{ |
|
92
|
|
|
$secondFactorTypes = array_map( |
|
93
|
|
|
function ($secondFactorString) { |
|
94
|
|
|
return new SecondFactorType($secondFactorString); |
|
95
|
|
|
}, |
|
96
|
|
|
$data['allowed_second_factors'] |
|
97
|
|
|
); |
|
98
|
|
|
|
|
99
|
|
|
return new self($secondFactorTypes); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function serialize() |
|
103
|
|
|
{ |
|
104
|
|
|
$allowedSecondFactors = array_map( |
|
105
|
|
|
function (SecondFactorType $secondFactorType) { |
|
106
|
|
|
return $secondFactorType->getSecondFactorType(); |
|
107
|
|
|
}, |
|
108
|
|
|
$this->allowedSecondFactors |
|
109
|
|
|
); |
|
110
|
|
|
|
|
111
|
|
|
return [ |
|
112
|
|
|
'allowed_second_factors' => $allowedSecondFactors, |
|
113
|
|
|
]; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function getIterator() |
|
117
|
|
|
{ |
|
118
|
|
|
return new ArrayIterator($this->allowedSecondFactors); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function jsonSerialize() |
|
122
|
|
|
{ |
|
123
|
|
|
return [ |
|
124
|
|
|
'allowed_second_factors' => $this->allowedSecondFactors |
|
125
|
|
|
]; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
private function initializeWith(SecondFactorType $allowedSecondFactor) |
|
129
|
|
|
{ |
|
130
|
|
|
if (!$this->contains($allowedSecondFactor)) { |
|
131
|
|
|
$this->allowedSecondFactors[] = $allowedSecondFactor; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
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: