Passed
Push — master ( faa376...c7ca4c )
by Tim
17:29 queued 14:52
created

AbstractAnySimpleType   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 133
rs 10
c 0
b 0
f 0
wmc 11
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSchema\Type\Interface;
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 AbstractAnySimpleType implements ValueTypeInterface
18
{
19
    public const string SCHEMA_TYPE = 'anySimpleType';
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 19 at column 24
Loading history...
20
21
    public const string SCHEMA_NAMESPACEURI = 'http://www.w3.org/2001/XMLSchema';
22
23
    public const string SCHEMA_NAMESPACE_PREFIX = 'xs';
24
25
26
    /**
27
     * Set the value for this type.
28
     *
29
     * @param string $value
30
     */
31
    final protected function __construct(
32
        protected string $value,
33
    ) {
34
        $this->validateValue($value);
35
    }
36
37
38
    /**
39
     * Get the value.
40
     *
41
     * @return string
42
     */
43
    public function getValue(): string
44
    {
45
        return $this->sanitizeValue($this->getRawValue());
46
    }
47
48
49
    /**
50
     * Get the raw unsanitized value.
51
     *
52
     * @return string
53
     */
54
    public function getRawValue(): string
55
    {
56
        return $this->value;
57
    }
58
59
60
    /**
61
     * Sanitize the value.
62
     *
63
     * @param string $value  The value
64
     * @return string
65
     */
66
    protected function sanitizeValue(string $value): string
67
    {
68
        /**
69
         * Perform no sanitation by default.
70
         * Override this method on the implementing class to perform content sanitation.
71
         */
72
        return $value;
73
    }
74
75
76
    /**
77
     * Validate the value.
78
     *
79
     * @param string $value  The value
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
134
    /**
135
     * Compare the value to another one
136
     *
137
     * @param \SimpleSAML\XMLSchema\Type\Interface\ValueTypeInterface|string $other
138
     * @return bool
139
     */
140
    public function equals(ValueTypeInterface|string $other): bool
141
    {
142
        if (is_string($other)) {
143
            $other = static::fromString($other);
144
        }
145
146
        return strcmp($this->getValue(), $other->getValue()) === 0;
147
    }
148
}
149