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

AbstractValueType   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 11
c 1
b 0
f 0
dl 0
loc 119
rs 10

9 Methods

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