Passed
Pull Request — master (#55)
by Tim
02:02
created

AbstractValueType::collapseWhitespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
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
    /** @var string */
18
    public const SCHEMA_TYPE = 'xs:anySimpleType';
19
20
    /** @var string */
21
    public const SCHEMA_NAMESPACE = 'http://www.w3.org/2001/XMLSchema';
22
23
24
    /**
25
     * Set the value for this type.
26
     *
27
     * @param string $value
28
     */
29
    final protected function __construct(
30
        protected string $value,
31
    ) {
32
        $this->validateValue($value);
33
    }
34
35
36
    /**
37
     * Get the value.
38
     *
39
     * @return string
40
     */
41
    public function getValue(): string
42
    {
43
        return $this->sanitizeValue($this->getRawValue());
44
    }
45
46
47
    /**
48
     * Get the raw unsanitized value.
49
     *
50
     * @return string
51
     */
52
    public function getRawValue(): string
53
    {
54
        return $this->value;
55
    }
56
57
58
    /**
59
     * Sanitize the value.
60
     *
61
     * @param string $value  The value
62
     * @throws \Exception on failure
63
     * @return string
64
     */
65
    protected function sanitizeValue(string $value): string
66
    {
67
        /**
68
         * Perform no sanitation by default.
69
         * Override this method on the implementing class to perform content sanitation.
70
         */
71
        return $value;
72
    }
73
74
75
    /**
76
     * Validate the value.
77
     *
78
     * @param string $value  The value
79
     * @throws \Exception on failure
80
     * @return void
81
     */
82
    protected function validateValue(/** @scrutinizer-ignore */string $value): void
83
    {
84
        /**
85
         * Perform no validation by default.
86
         * Override this method on the implementing class to perform validation.
87
         */
88
    }
89
90
91
    /**
92
     * Normalize whitespace in the value
93
     *
94
     * @return string
95
     */
96
    protected static function normalizeWhitespace(string $value): string
97
    {
98
        return preg_replace('/\s/', ' ', $value);
99
    }
100
101
102
    /**
103
     * Collapse whitespace
104
     *
105
     * @return string
106
     */
107
    protected static function collapseWhitespace(string $value): string
108
    {
109
        return trim(preg_replace('/\s+/', ' ', $value));
110
    }
111
112
113
    /**
114
     * @param string $value
115
     * @return static
116
     */
117
    public static function fromString(string $value): static
118
    {
119
        return new static($value);
120
    }
121
122
123
    /**
124
     * Output the value as a string
125
     *
126
     * @return string
127
     */
128
    public function __toString(): string
129
    {
130
        return $this->getValue();
131
    }
132
}
133