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

AbstractLocalizedURI   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 81
rs 10
c 1
b 0
f 0
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A sanitizeContent() 0 4 1
A getContent() 0 3 1
A setContent() 0 4 1
A getRawContent() 0 3 1
A validateContent() 0 3 1
A fromArray() 0 12 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\md;
6
7
use SimpleSAML\Assert\Assert;
8
use SimpleSAML\XML\Exception\SchemaViolationException;
9
10
use function filter_var;
11
12
/**
13
 * Abstract class implementing LocalizedURIType.
14
 *
15
 * @package simplesamlphp/saml2
16
 */
17
abstract class AbstractLocalizedURI extends AbstractLocalizedName
18
{
19
    /**
20
     * Set the content of the element.
21
     *
22
     * @param string $content  The value to go in the XML textContent
23
     */
24
    protected function setContent(string $content): void
25
    {
26
        $this->validateContent($content);
27
        $this->content = $content;
28
    }
29
30
31
    /**
32
     * Get the content of the element.
33
     *
34
     * @return string
35
     */
36
    public function getContent(): string
37
    {
38
        return $this->sanitizeContent($this->getRawContent());
39
    }
40
41
42
    /**
43
     * Get the raw and unsanitized content of the element.
44
     *
45
     * @return string
46
     */
47
    public function getRawContent(): string
48
    {
49
        return $this->content;
50
    }
51
52
53
    /**
54
     * Sanitize the content of the element.
55
     *
56
     * @param string $content  The unsanitized textContent
57
     * @throws \Exception on failure
58
     * @return string
59
     */
60
    protected function sanitizeContent(string $content): string
61
    {
62
        // We've seen metadata in the wild that had stray whitespace around URIs, causing assertions to fail
63
        return trim($content);
64
    }
65
66
67
    /**
68
     * Validate the content of the element.
69
     *
70
     * @param string $content  The value to go in the XML textContent
71
     * @throws \Exception on failure
72
     * @return void
73
     */
74
    protected function validateContent(string $content): void
75
    {
76
        Assert::validURI($this->sanitizeContent($content), SchemaViolationException::class); // Covers the empty string
77
    }
78
79
80
    /**
81
     * Create a class from an array
82
     *
83
     * @param array $data
84
     * @return static
85
     */
86
    public static function fromArray(array $data): static
87
    {
88
        Assert::count($data, 1);
89
90
        $lang = array_key_first($data);
91
        Assert::stringNotEmpty($lang);
92
93
        $value = $data[$lang];
94
        Assert::stringNotEmpty($value);
95
        Assert::validURI($value);
96
97
        return new static($lang, $value);
98
    }
99
}
100