1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\XML\Type; |
6
|
|
|
|
7
|
|
|
use function is_string; |
8
|
|
|
use function preg_replace; |
9
|
|
|
use function strcmp; |
10
|
|
|
use function trim; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Abstract class to be implemented by all types |
14
|
|
|
* |
15
|
|
|
* @package simplesamlphp/xml-common |
16
|
|
|
*/ |
17
|
|
|
abstract class AbstractValueType implements ValueTypeInterface |
18
|
|
|
{ |
19
|
|
|
/** @var string */ |
20
|
|
|
public const SCHEMA_TYPE = 'anySimpleType'; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
public const SCHEMA_NAMESPACEURI = 'http://www.w3.org/2001/XMLSchema'; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
public const SCHEMA_NAMESPACE_PREFIX = 'xs'; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Set the value for this type. |
31
|
|
|
* |
32
|
|
|
* @param string $value |
33
|
|
|
*/ |
34
|
|
|
final protected function __construct( |
35
|
|
|
protected string $value, |
36
|
|
|
) { |
37
|
|
|
$this->validateValue($value); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get the value. |
43
|
|
|
* |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
public function getValue(): string |
47
|
|
|
{ |
48
|
|
|
return $this->sanitizeValue($this->getRawValue()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get the raw unsanitized value. |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public function getRawValue(): string |
58
|
|
|
{ |
59
|
|
|
return $this->value; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Sanitize the value. |
65
|
|
|
* |
66
|
|
|
* @param string $value The value |
67
|
|
|
* @throws \Exception on failure |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
protected function sanitizeValue(string $value): string |
71
|
|
|
{ |
72
|
|
|
/** |
73
|
|
|
* Perform no sanitation by default. |
74
|
|
|
* Override this method on the implementing class to perform content sanitation. |
75
|
|
|
*/ |
76
|
|
|
return $value; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Validate the value. |
82
|
|
|
* |
83
|
|
|
* @param string $value The value |
84
|
|
|
* @throws \Exception on failure |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
protected function validateValue(/** @scrutinizer-ignore */string $value): void |
88
|
|
|
{ |
89
|
|
|
/** |
90
|
|
|
* Perform no validation by default. |
91
|
|
|
* Override this method on the implementing class to perform validation. |
92
|
|
|
*/ |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Normalize whitespace in the value |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
protected static function normalizeWhitespace(string $value): string |
102
|
|
|
{ |
103
|
|
|
return preg_replace('/\s/', ' ', $value); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Collapse whitespace |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
protected static function collapseWhitespace(string $value): string |
113
|
|
|
{ |
114
|
|
|
return trim(preg_replace('/\s+/', ' ', $value)); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $value |
120
|
|
|
* @return static |
121
|
|
|
*/ |
122
|
|
|
public static function fromString(string $value): static |
123
|
|
|
{ |
124
|
|
|
return new static($value); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Output the value as a string |
130
|
|
|
* |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
public function __toString(): string |
134
|
|
|
{ |
135
|
|
|
return $this->getValue(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Compare the value to another one |
141
|
|
|
* |
142
|
|
|
* @param \SimpleSAML\XML\Type\ValueTypeInterface|string $other |
143
|
|
|
* @return bool |
144
|
|
|
*/ |
145
|
|
|
public function equals(ValueTypeInterface|string $other): bool |
146
|
|
|
{ |
147
|
|
|
if (is_string($other)) { |
148
|
|
|
$other = static::fromString($other); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return strcmp($this->getValue(), $other->getValue()) === 0; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|