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

AbstractValueType::validateValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 2
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 Stringable;
8
9
/**
10
 * Abstract class to be implemented by all types
11
 *
12
 * @package simplesamlphp/xml-common
13
 */
14
abstract class AbstractValueType implements Stringable, ValueTypeInterface
15
{
16
    /**
17
     * Set the value for this type.
18
     *
19
     * @param string $value
20
     */
21
    final protected function __construct(
22
        protected string $value,
23
    ) {
24
        $this->validateValue($value);
25
    }
26
27
28
    /**
29
     * Get the value.
30
     *
31
     * @return string
32
     */
33
    public function getValue(): string
34
    {
35
        return $this->sanitizeValue($this->getRawValue());
36
    }
37
38
39
    /**
40
     * Get the raw unsanitized value.
41
     *
42
     * @return string
43
     */
44
    public function getRawValue(): string
45
    {
46
        return $this->value;
47
    }
48
49
50
    /**
51
     * Sanitize the value.
52
     *
53
     * @param string $value  The value
54
     * @throws \Exception on failure
55
     * @return string
56
     */
57
    protected function sanitizeValue(string $value): string
58
    {
59
        /**
60
         * Perform no sanitation by default.
61
         * Override this method on the implementing class to perform content sanitation.
62
         */
63
        return $value;
64
    }
65
66
67
    /**
68
     * Validate the value.
69
     *
70
     * @param string $value  The value
71
     * @throws \Exception on failure
72
     * @return void
73
     */
74
    protected function validateValue(/** @scrutinizer ignore-unused */ string $value): void
75
    {
76
        /**
77
         * Perform no validation by default.
78
         * Override this method on the implementing class to perform validation.
79
         */
80
    }
81
82
83
    /**
84
     * @param string $value
85
     * @return \SimpleSAML\XML\Type\ValueTypeInterface
86
     */
87
    public static function fromString(string $value): ValueTypeInterface
88
    {
89
        return new static($value);
90
    }
91
92
93
    /**
94
     * Output the value as a string
95
     *
96
     * @return string
97
     */
98
    public function __toString(): string
99
    {
100
        return $this->getValue();
101
    }
102
}
103