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

AbstractValueType   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 7
c 1
b 0
f 0
dl 0
loc 87
rs 10

7 Methods

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