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

AbstractLocalizedURI::validateArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
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
     * Validate an array
82
     *
83
     * @param array $data
84
     * @return void
85
     */
86
    public static function validateArray(array $data): void
87
    {
88
        Assert::count($data, 1);
89
        $lang = array_key_first($data);
90
        Assert::stringNotEmpty($lang);
91
        Assert::validURI($data[$lang]);
92
    }
93
}
94