Passed
Pull Request — master (#359)
by Tim
02:53
created

NameIDFormat   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 9
c 2
b 0
f 1
dl 0
loc 78
rs 10
wmc 6

6 Methods

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