1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OpenConext\Value\Saml\Metadata; |
4
|
|
|
|
5
|
|
|
use OpenConext\Value\Assert\Assertion; |
6
|
|
|
use OpenConext\Value\RegularExpression; |
7
|
|
|
use OpenConext\Value\Serializable; |
8
|
|
|
|
9
|
|
|
final class ShibbolethMetadataScope implements Serializable |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
private $scope; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var bool |
18
|
|
|
*/ |
19
|
|
|
private $isRegexp; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param string $literal |
23
|
|
|
* @return ShibbolethMetadataScope |
24
|
|
|
*/ |
25
|
|
|
public static function literal($literal) |
26
|
|
|
{ |
27
|
|
|
Assertion::nonEmptyString($literal, 'literal'); |
28
|
|
|
|
29
|
|
|
return new self($literal); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $regexp |
34
|
|
|
* @return ShibbolethMetadataScope |
35
|
|
|
*/ |
36
|
|
|
public static function regexp($regexp) |
37
|
|
|
{ |
38
|
|
|
Assertion::nonEmptyString($regexp, 'regexp'); |
39
|
|
|
|
40
|
|
|
return new self($regexp, true); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $scope the scope as defined |
45
|
|
|
* @param bool $isRegexp whether or not the scope is a regular expression as identified by the regexp attribute |
46
|
|
|
*/ |
47
|
|
|
public function __construct($scope, $isRegexp = false) |
48
|
|
|
{ |
49
|
|
|
Assertion::nonEmptyString($scope, 'scope'); |
50
|
|
|
Assertion::boolean($isRegexp); |
51
|
|
|
|
52
|
|
|
if ($isRegexp) { |
53
|
|
|
Assertion::validRegularExpression($scope, 'scope'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->scope = $scope; |
57
|
|
|
$this->isRegexp = $isRegexp; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $string |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
|
|
public function allows($string) |
65
|
|
|
{ |
66
|
|
|
Assertion::string($string, 'Scope to check should be a string, "%s" given'); |
67
|
|
|
|
68
|
|
|
if (!$this->isRegexp) { |
69
|
|
|
return $this->scope === $string; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$regexp = new RegularExpression($this->scope); |
73
|
|
|
return $regexp->matches($string); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param ShibbolethMetadataScope $other |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
public function equals(ShibbolethMetadataScope $other) |
81
|
|
|
{ |
82
|
|
|
return ($this->scope === $other->scope && $this->isRegexp === $other->isRegexp); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public static function deserialize($data) |
86
|
|
|
{ |
87
|
|
|
Assertion::isArray($data); |
88
|
|
|
Assertion::keysExist($data, array('is_regexp', 'scope')); |
89
|
|
|
|
90
|
|
|
return new self($data['scope'], $data['is_regexp']); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function serialize() |
94
|
|
|
{ |
95
|
|
|
return array( |
96
|
|
|
'scope' => $this->scope, |
97
|
|
|
'is_regexp' => $this->isRegexp |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function __toString() |
102
|
|
|
{ |
103
|
|
|
return sprintf( |
104
|
|
|
'ShibbolethMetadataScope(scope=%s, regexp=%s)', |
105
|
|
|
$this->scope, |
106
|
|
|
$this->isRegexp ? 'true' : 'false' |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|