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